Skip to content

Fix invalid-name false positive in if __name__ == "__main__": block#11080

Open
Labib-Bin-Salam wants to merge 1 commit into
pylint-dev:mainfrom
Labib-Bin-Salam:fix-invalid-name-dunder-main
Open

Fix invalid-name false positive in if __name__ == "__main__": block#11080
Labib-Bin-Salam wants to merge 1 commit into
pylint-dev:mainfrom
Labib-Bin-Salam:fix-invalid-name-dunder-main

Conversation

@Labib-Bin-Salam

Copy link
Copy Markdown
Contributor

Type of Changes

Type
🐛 Bug fix

Description

Names assigned in an if __name__ == "__main__": block were checked against the
constant naming style, so an ordinary variable produced a false
invalid-name (C0103):

def main() -> int:
    ...

if __name__ == "__main__":
    exit_code = main()  # C0103: Constant name "exit_code" doesn't conform to UPPER_CASE
    raise SystemExit(exit_code)

The __main__ guard reads like a script body, where a name can reasonably be a
one-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:

if __name__ == "__main__":
    exit_code = main()  # ok (variable style)
    EXIT_CODE = main()  # ok (constant style)
    BadMix = main()     # still C0103 (matches neither)

Module-level names outside the block are unaffected. Detection only matches the
body of the guard (not its else) and accepts both __name__ == "__main__" and
the reversed comparison.

Closes #10766

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>
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on music21:

The following messages are now emitted:

Details
  1. invalid-name:
    Variable name "te" doesn't conform to '[a-z_][A-Za-z0-9_]{2,30}$' pattern
    https://github.com/cuthbertLab/music21/blob/65fe74f6584bdeed8421dc57b0452b4d364e4159/music21/configure.py#L1628

The following messages are no longer emitted:

Details
  1. invalid-name:
    Constant name "te" doesn't conform to '(([A-Z_][A-Z0-9_])|(.*))$' pattern*
    https://github.com/cuthbertLab/music21/blob/65fe74f6584bdeed8421dc57b0452b4d364e4159/music21/configure.py#L1628
  2. invalid-name:
    Constant name "unused_returnCode" doesn't conform to '(([A-Z_][A-Z0-9_])|(.*))$' pattern*
    https://github.com/cuthbertLab/music21/blob/65fe74f6584bdeed8421dc57b0452b4d364e4159/music21/test/testSingleCoreAll.py#L150
  3. invalid-name:
    Constant name "unused_returnCode" doesn't conform to '(([A-Z_][A-Z0-9_])|(.*))$' pattern*
    https://github.com/cuthbertLab/music21/blob/65fe74f6584bdeed8421dc57b0452b4d364e4159/music21/test/testSingleCoreAll.py#L152

This comment was generated for commit 3f1db62

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXIT_CODE = main() # constant style is accepted
EXIT_CODE = main() # constant style is accepted
unused_returnCode = main() # mixed style is refused

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jun 7, 2026
@Labib-Bin-Salam

Copy link
Copy Markdown
Contributor Author

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, pylint self-lint, pre-commit, changelog, docs).

The only red check, process / coverage, is not a code issue and is not a required check — the Codecov uploader failed during its own GPG self-verification before any coverage was processed:

gpg: Can't check signature: No public key
==> Could not verify signature. Please contact Codecov if problem continues
    Exiting...

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 if __name__ == "__main__": block are now accepted if they match either the constant or the variable naming style (and still flagged otherwise), matching the direction discussed in #10700. Closes #10766.

@Pierre-Sassoulas

Copy link
Copy Markdown
Member

Hey @Labib-Bin-Salam, I reviewed yesterday #11080 (comment). There's a new false negative when using this code imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

False Positive 🦟 A message is emitted but nothing is wrong with the code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive for constant naming style inside __main__ block

2 participants