Skip to content

Interpret bare ClassVar as inferred, not Any #19573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2025

Conversation

ilevkivskyi
Copy link
Member

Fixes #5587

Apparently, it is part of the spec now. cc @JukkaL

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

I checked the new errors are correct (previously bare ClassVar annotations resulted in implicit Anys).


class C:
x: ClassVar = 1
y: ClassVar
Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, can we reject at least this in --disallow-any-generics fashion? Doesn't look like something I want to see in typed code silently...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, good point. We should reject this with --disallow-any-generics.

Copy link
Contributor

github-actions bot commented Aug 3, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/ext/autodoc/_documenters.py: note: In member "_load_object_by_name" of class "Documenter":
+ sphinx/ext/autodoc/_documenters.py:247:21: error: Argument "objtype" to "_load_object_by_name" has incompatible type "str"; expected "Literal['module', 'class', 'exception', 'function', 'decorator', 'method', 'property', 'attribute', 'data']"  [arg-type]

black (https://github.com/psf/black)
+ src/black/trans.py:346:38: error: Variable "black.trans.CustomSplitMapMixin._Key" is not valid as a type  [valid-type]
+ src/black/trans.py:346:38: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ src/black/trans.py:351:2: error: Variable "black.trans.CustomSplitMapMixin._Key" is not valid as a type  [valid-type]
+ src/black/trans.py:351:2: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

trio (https://github.com/python-trio/trio)
+ src/trio/_tests/test_ssl.py:217: error: Item "None" of "Any | None" has no attribute "SSL_OP_NO_TLSv1_3"  [union-attr]

scipy-stubs (https://github.com/scipy/scipy-stubs)
+ scipy-stubs/_lib/_ccallback.pyi:54: error: All protocol members must have explicitly declared types  [misc]
+ scipy-stubs/_lib/_ccallback.pyi:55: error: All protocol members must have explicitly declared types  [misc]
+ scipy-stubs/_lib/_ccallback.pyi:76: error: All protocol members must have explicitly declared types  [misc]
+ scipy-stubs/_lib/_ccallback.pyi:83: error: All protocol members must have explicitly declared types  [misc]
+ scipy-stubs/_lib/_ccallback.pyi:84: error: All protocol members must have explicitly declared types  [misc]

@ilevkivskyi ilevkivskyi merged commit 68657d2 into python:master Aug 3, 2025
20 checks passed
@ilevkivskyi ilevkivskyi deleted the classvar-no-arg branch August 3, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bare ClassVar should infer type instead of defaulting to Any
2 participants