Fix invalid-name false positive in if __name__ == "__main__": block#11080
Fix invalid-name false positive in if __name__ == "__main__": block#11080Labib-Bin-Salam wants to merge 1 commit into
invalid-name false positive in if __name__ == "__main__": block#11080Conversation
Names assigned in an `if __name__ == "__main__":` block were checked against the constant naming style, emitting a false `invalid-name` (C0103) for ordinary variables such as `exit_code = main()`. Such a block reads like a script body, so a name there is now accepted if it matches either the constant or the variable naming style, as discussed in pylint-dev#10700. Closes pylint-dev#10766 Signed-off-by: Labib-Bin-Salam <Labibsalam0@gmail.com>
|
🤖 Effect of this PR on checked open source code: 🤖 Effect on music21: The following messages are now emitted: Details
The following messages are no longer emitted: Details
This comment was generated for commit 3f1db62 |
Pierre-Sassoulas
left a comment
There was a problem hiding this comment.
Thank you for your work on pylint. I feel like there's some new false negative considering the primer.
|
|
||
| if __name__ == "__main__": | ||
| exit_code = main() # variable style is accepted | ||
| EXIT_CODE = main() # constant style is accepted |
There was a problem hiding this comment.
| EXIT_CODE = main() # constant style is accepted | |
| EXIT_CODE = main() # constant style is accepted | |
| unused_returnCode = main() # mixed style is refused |
|
This is ready for review when a maintainer has a moment. 🙏 All required checks are green (functional tests on 3.10–3.14 + PyPy across Linux/macOS/Windows, The only red check, That's a transient Codecov-CLI infrastructure problem; a re-run of that single job should clear it (I don't have permission to re-run it myself). To recap the change: names assigned inside an |
|
Hey @Labib-Bin-Salam, I reviewed yesterday #11080 (comment). There's a new false negative when using this code imo. |
Type of Changes
Description
Names assigned in an
if __name__ == "__main__":block were checked against theconstant naming style, so an ordinary variable produced a false
invalid-name(C0103):The
__main__guard reads like a script body, where a name can reasonably be aone-off variable or a module constant. This PR special-cases that block so a
name is accepted if it matches either the constant or the variable naming
style (and is still reported otherwise). This follows the direction discussed in
#10700 ("let module-level names pass against either constant or variable regexes,
but not anything else").
Behaviour:
Module-level names outside the block are unaffected. Detection only matches the
body of the guard (not its
else) and accepts both__name__ == "__main__"andthe reversed comparison.
Closes #10766