-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-89547: Support for nesting special forms like Final #116096
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
gh-89547: Support for nesting special forms like Final #116096
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's an easy change to make! Not sure if we want to treat this as a new feature or a backportable bugfix — thoughts, @JelleZijlstra?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I feel this should be treated as a new feature and not backported.
It's probably worth a versionchanged
note in the typing docs, e.g. saying "ClassVar can now be nested inside Final".
@JelleZijlstra what do you see as the risk of backporting? It seems like arguably a "bug" that (given the current de facto behavior of type checkers) there is no way to specify a Final, non-field, ClassVar, on a dataclass. Backporting this would fix that "bug" for all users of Python 3.11 and Python 3.12, rather than requiring they wait until 3.13. |
It feels like a new feature to me; we're adding support for something that wasn't previously allowed. For example, if a library after the release of 3.12.3 starts using Users on earlier versions can work around the problem by using quoted annotations. |
This still needs to be done |
Thanks, done in #116686 |
Resolves this issue. The main goal was to permit
ClassVar[Final[int]]
andFinal[ClassVar[int]]
. This drops validation check that Final/ClassVar argument is not a special form.This does allow some silly cases like
ClassVar[ClassVar[int]]
but I think it's simpler to allow them then have more complex validation rules at runtime. I did not remove special form check for other forms soUnion[ClassVar[int], int]
remains forbidden andlist[ClassVar[int]]
is also forbidden. There already was a test case thatlist[ClassVar[int]]
fails.I also added couple tests for Annotated to ensure it can nest freely with
ClassVar
/Final
.