← 返回首页
gh-89547: Support for nesting special forms like Final by hmc-cs-mdrissi · Pull Request #116096 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-89547: Support for nesting special forms like Final#116096

Merged
JelleZijlstra merged 2 commits into
python:mainfrom
hmc-cs-mdrissi:support_nesting_special_forms
Mar 12, 2024
Merged

gh-89547: Support for nesting special forms like Final#116096
JelleZijlstra merged 2 commits into
python:mainfrom
hmc-cs-mdrissi:support_nesting_special_forms

Conversation

hmc-cs-mdrissi commented Feb 29, 2024
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

Resolves this issue. The main goal was to permit ClassVar[Final[int]] and Final[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 so Union[ClassVar[int], int] remains forbidden and list[ClassVar[int]] is also forbidden. There already was a test case that list[ClassVar[int]] fails.

I also added couple tests for Annotated to ensure it can nest freely with ClassVar/Final.

ghost commented Feb 29, 2024
edited by ghost
Loading

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

AlexWaygood 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

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?

Comment thread Lib/test/test_typing.py Show resolved Hide resolved

JelleZijlstra 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

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".

carljm commented Mar 1, 2024

Copy link
Copy Markdown
Member

@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.

Copy link
Copy Markdown
Member

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 ClassVar[Final...] in its code and tests only on 3.12.3+, they would break all users who are still on 3.12.2 without realizing it.

Users on earlier versions can work around the problem by using quoted annotations.

JelleZijlstra merged commit d308d33 into python:main Mar 12, 2024

Copy link
Copy Markdown
Member

It's probably worth a versionchanged note in the typing docs, e.g. saying "ClassVar can now be nested inside Final".

This still needs to be done

Copy link
Copy Markdown
Member

Thanks, done in #116686

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Footer

© 2026 GitHub, Inc.