René's URL Explorer Experiment


Title: spec: clarify interaction of Final and dataclass by carljm · Pull Request #1669 · python/typing · GitHub

Open Graph Title: spec: clarify interaction of Final and dataclass by carljm · Pull Request #1669 · python/typing

X Title: spec: clarify interaction of Final and dataclass by carljm · Pull Request #1669 · python/typing

Description: See discussion thread at https://discuss.python.org/t/treatment-of-final-attributes-in-dataclass-likes/47154 and issue at python/cpython#89547 Consider a dataclass with a Final-annotated initialized assignment in the class body: @dataclass class C: x: Final[int] = 3 This is currently under-specified in the typing spec. Neither PEP 591 or PEP 681 clearly specified this composition. There are two possible consistent interpretations: It could be a class-only variable (implicit ClassVar) C.x with final value 3. In this interpretation, it should not be a dataclass field, because dataclass fields are inherently set on instances, they cannot be ClassVar. This is the interpretation suggested by PEP 591 (and thus also the current typing spec for Final), which say that Final with an assigned value in a class body is always implicitly a ClassVar. It could be a dataclass field x with default value 3, which cannot be reassigned on an instance after it is initialized in the generated __init__ method. This pull request specifies option 2. I will also provide an update to the conformance suite for this, if the Typing Council accepts the spec change. Current runtime behavior The stdlib dataclasses module considers x in this example to be a dataclass field. If the assigned value is a default value (as in the example above), dataclasses leaves that default value in place as a class attribute (so x is a class-and-instance variable, and C.x == 3.) If the assigned value is a field(...) call, dataclasses does not leave any attribute x on the class at all, so x is purely a dataclass field / instance variable. Current type-checker behavior Playground links: mypy pyre pyright All three agree with the runtime behavior of dataclasses, considering x to be a dataclass field which is included in the dataclass __init__ method and set on instances in __init__. All three prohibit further assignments to x on instances, noting that it is a final attribute. Pyright also mentions that it is a ClassVar, which is confusing given that "ClassVar" and "dataclass field" are (or should be) mutually exclusive. All three assume that C.x is available and of type int, whether we have x: Final[int] = 3 or x: Final[int] = field(default_value=3). That is, none of the type checkers model the actual runtime behavior that field() objects are removed from the class and not replaced with anything. (Arguably this behavior is just a bug/inconsistency in the dataclasses runtime implementation.) Considerations Option 2 (proposed in this PR) is clearly less disruptive to existing code, as it effectively just specifies the de facto current behavior of the runtime and all type checkers. The only practical change to type checker behavior in the given examples that would occur as a result of this spec change is that Pyright should not claim in its error message that x is a ClassVar, but otherwise behavior would remain as it is; errors would still be raised on exactly the same lines by all three type checkers. Option 2 has the downside that one cannot assume as an invariant that the syntax x: Final[int] = 3 always means that x will always have value exactly 3. In dataclass bodies this will be more nuanced: the class attribute will always be 3, but it will be shadowed by an instance attribute that may have a different value on any given instance. On the other hand, it is already the case that the semantics of annotated assignments in dataclass bodies differ from other contexts: x: int = field(...) when field(...) does not return int would obviously be a type error anywhere else. This example illustrates that in dataclasses, the annotation generally does not apply to the immediate assignment, but to the type of the dataclass field (instance var) specified by the annotated assignment. If we select Option 1, it will be quite cumbersome to achieve a Final dataclass field (one would have to write out the __init__ method manually instead of allowing dataclass to generate it, eliminating a key benefit of dataclasses.) If we select Option 2, it remains quite easy to have a final classvar on a dataclass, using a ClassVar[Final[...]] annotation.

Open Graph Description: See discussion thread at https://discuss.python.org/t/treatment-of-final-attributes-in-dataclass-likes/47154 and issue at python/cpython#89547 Consider a dataclass with a Final-annotated initialize...

X Description: See discussion thread at https://discuss.python.org/t/treatment-of-final-attributes-in-dataclass-likes/47154 and issue at python/cpython#89547 Consider a dataclass with a Final-annotated initialize...

Opengraph URL: https://github.com/python/typing/pull/1669

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:734dba8b-9873-361d-6bf5-8da58d07f730
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id8B0E:1E9EE2:9A8DC:C6A72:6AFF9C0B
html-safe-nonce2092f606fde5262388e72399809cb0795b687ba605d32997130a125f1ccd0112
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjBFOjFFOUVFMjo5QThEQzpDNkE3Mjo2QUZGOUMwQiIsInZpc2l0b3JfaWQiOiIzMTg4MDU2MTQ1NzQyNzY1MDY3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac5a4017d05412a80a6ebbdd803de66cc7d25edd1be6bf23c43786974117c0c249
hovercard-subject-tagpull_request:1798213664
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/typing/pull/1669/files
twitter:imagehttps://avatars.githubusercontent.com/u/61586?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/61586?s=400&v=4
og:image:altSee discussion thread at https://discuss.python.org/t/treatment-of-final-attributes-in-dataclass-likes/47154 and issue at python/cpython#89547 Consider a dataclass with a Final-annotated initialize...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None2c75d045e8c84b51e2ac680086a6dd1eeaf2bc15b82137d22ee5106835d6256c
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/typing git https://github.com/python/typing.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id24604008
octolytics-dimension-repository_nwopython/typing
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24604008
octolytics-dimension-repository_network_root_nwopython/typing
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release525b66d22c4f7e6bb8b62934bec198964f7e8b93
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/typing/pull/1669/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fpull%2F1669%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fpull%2F1669%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Ftyping
Reloadhttps://github.com/python/typing/pull/1669/files
Reloadhttps://github.com/python/typing/pull/1669/files
Reloadhttps://github.com/python/typing/pull/1669/files
pythonhttps://github.com/python
typinghttps://github.com/python/typing
Codehttps://github.com/python/typing
Issues206 (206)https://github.com/python/typing/issues
Pull requests27 (27)https://github.com/python/typing/pulls
Discussionshttps://github.com/python/typing/discussions
Actionshttps://github.com/python/typing/actions
Security and qualityhttps://github.com/python/typing/security
Insightshttps://github.com/python/typing/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Ftyping%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Ftyping%2Fissues%2Fnew%2Fchoose
JelleZijlstrahttps://github.com/JelleZijlstra
python:mainhttps://github.com/python/typing/tree/main
carljm:dcfinalhttps://github.com/carljm/typing/tree/dcfinal
Conversation 5 https://github.com/python/typing/pull/1669
Commits 2 https://github.com/python/typing/pull/1669/commits
Checks 0 https://github.com/python/typing/pull/1669/checks
Files changed https://github.com/python/typing/pull/1669/files
Please reload this pagehttps://github.com/python/typing/pull/1669/files
spec: clarify interaction of Final and dataclass https://github.com/python/typing/pull/1669/files#top
Show all changes 2 commits https://github.com/python/typing/pull/1669/files
8a6f51b clarify interaction of Final and dataclass carljm Mar 29, 2024 https://github.com/python/typing/pull/1669/commits/8a6f51bf268eaf99d587abb2985fb9b944f17444
0e3f3a7 Jelle comments carljm Apr 3, 2024 https://github.com/python/typing/pull/1669/commits/0e3f3a7f4a2a22e5f69a3fb52f7a173711bf9f69
Clear filters https://github.com/python/typing/pull/1669/files
Please reload this pagehttps://github.com/python/typing/pull/1669/files
Please reload this pagehttps://github.com/python/typing/pull/1669/files
dataclasses.rst https://github.com/python/typing/pull/1669/files#diff-83af9220ffe7882e838baebf179875e0d66aa769707fb545738a1668f956b3d1
qualifiers.rst https://github.com/python/typing/pull/1669/files#diff-d63169ac083634a818bd58f3c0d305cc19ad8200a2a0363d5010e0dc998033ff
docs/spec/dataclasses.rsthttps://github.com/python/typing/pull/1669/files#diff-83af9220ffe7882e838baebf179875e0d66aa769707fb545738a1668f956b3d1
View file https://github.com/carljm/typing/blob/0e3f3a7f4a2a22e5f69a3fb52f7a173711bf9f69/docs/spec/dataclasses.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/typing/pull/1669/{{ revealButtonHref }}
https://github.com/python/typing/pull/1669/files#diff-83af9220ffe7882e838baebf179875e0d66aa769707fb545738a1668f956b3d1
https://github.com/python/typing/pull/1669/files#diff-83af9220ffe7882e838baebf179875e0d66aa769707fb545738a1668f956b3d1
docs/spec/qualifiers.rsthttps://github.com/python/typing/pull/1669/files#diff-d63169ac083634a818bd58f3c0d305cc19ad8200a2a0363d5010e0dc998033ff
View file https://github.com/carljm/typing/blob/0e3f3a7f4a2a22e5f69a3fb52f7a173711bf9f69/docs/spec/qualifiers.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/typing/pull/1669/{{ revealButtonHref }}
https://github.com/python/typing/pull/1669/files#diff-d63169ac083634a818bd58f3c0d305cc19ad8200a2a0363d5010e0dc998033ff
Please reload this pagehttps://github.com/python/typing/pull/1669/files
https://github.com/python/typing/pull/1669/files#diff-d63169ac083634a818bd58f3c0d305cc19ad8200a2a0363d5010e0dc998033ff
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.