-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ty] Bare ClassVar
annotations
#15768
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
Conversation
538b8ff
to
5b6d2d7
Compare
5b6d2d7
to
0580abf
Compare
ClassVar
annotation as ClassVar[Unknown]
ClassVar
annotations
|
What's our behaviour for, e.g. from typing import Final, ClassVar
class Foo:
X: Final[ClassVar] = 1 Do we have any tests for that? Does the spec say anything about it? What are mypy/pyright's behaviour there? It would be pretty unusual to see something like that since a |
Regarding dataclasses: this is what the spec says:
|
ClassVar
annotationsClassVar
annotations
Mypy infers When we change that to We infer
No, added! Thanks.
According to the type annotation syntax, both forms should be allowed.
... because you would expect to see the assignment right there in the class body? Right..
Interesting! This behavior is certainly not yet implemented, so this will require some more changes. I'll add tests for this (and an implementation, if it's easy) in another PR. It's not really related to the change here. |
I know -- but this PR reminded me of it, and it's certainly related to the theme of your work today ;) |
…c_tokens * 'main' of https://github.com/astral-sh/ruff: (27 commits) [ty] First cut at semantic token provider (astral-sh#19108) [`flake8-simplify`] Make example error out-of-the-box (`SIM116`) (astral-sh#19111) [`flake8-use-pathlib`] Make example error out-of-the-box (`PTH210`) (astral-sh#19189) [`flake8-use-pathlib`] Add autofixes for `PTH203`, `PTH204`, `PTH205` (astral-sh#18922) [`flake8-type-checking`] Fix syntax error introduced by fix (`TC008`) (astral-sh#19150) [`flake8-pyi`] Make example error out-of-the-box (`PYI007`, `PYI008`) (astral-sh#19103) Update Rust crate indicatif to 0.18.0 (astral-sh#19165) [ty] Add separate CI job for memory usage stats (astral-sh#19134) [ty] Add documentation for server traits (astral-sh#19137) Rename to `SessionSnapshot`, move unwind assertion closer (astral-sh#19177) [`flake8-type-checking`] Make example error out-of-the-box (`TC001`) (astral-sh#19151) [ty] Bare `ClassVar` annotations (astral-sh#15768) [ty] Re-enable multithreaded pydantic benchmark (astral-sh#19176) [ty] Implement equivalence for protocols with method members (astral-sh#18659) [ty] Use RHS inferred type for bare `Final` symbols (astral-sh#19142) [ty] Support declaration-only attributes (astral-sh#19048) [ty] Sync vendored typeshed stubs (astral-sh#19174) Update dependency pyodide to ^0.28.0 (astral-sh#19164) Update NPM Development dependencies (astral-sh#19170) Update taiki-e/install-action action to v2.56.7 (astral-sh#19169) ...
I don't know what I read yesterday, but now that I read it again, that all seems very logical — and in fact we already implement everything correctly. Added some tests here: #19202 |
## Summary Adds some tests for dataclass fields that are annotated with `Final` (see comment [here](#15768 (comment))). Turns out that nothing is needed here, everything already works as expected (apart from the fact that we can assign to `Final` fields, which is tracked in astral-sh/ty#158 ## Test Plan New Markdown tests
Summary
It was recently clarified in the typing spec that bare
ClassVar
annotations are allowed. For annotated assignments with a right hand side value, the spec requires type checkers to infer the type as something "to which [the] value is assignable". For a value of2
, the spec suggestsint
,Literal[2]
, orAny
as examples. Here, we chooseUnknown | Literal[2]
instead, conforming with out usual treatment of attribute types.closes astral-sh/ty#211