Skip to content

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

Merged

Conversation

hmc-cs-mdrissi
Copy link
Contributor

@hmc-cs-mdrissi hmc-cs-mdrissi commented Feb 29, 2024

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
Copy link

ghost commented Feb 29, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

Copy link
Member

@AlexWaygood AlexWaygood left a 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?

Copy link
Member

@JelleZijlstra JelleZijlstra left a 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".

@carljm
Copy link
Member

carljm commented Mar 1, 2024

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

@JelleZijlstra
Copy link
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 JelleZijlstra merged commit d308d33 into python:main Mar 12, 2024
@AlexWaygood
Copy link
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

@JelleZijlstra
Copy link
Member

Thanks, done in #116686

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