[ty] Represent InitVar as a special form internally, not a class#24248
Merged
AlexWaygood merged 1 commit intomainfrom Mar 30, 2026
Merged
[ty] Represent InitVar as a special form internally, not a class#24248AlexWaygood merged 1 commit intomainfrom
InitVar as a special form internally, not a class#24248AlexWaygood merged 1 commit intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 86.61%. The percentage of expected errors that received a diagnostic held steady at 81.56%. The number of fully passing files held steady at 70/132. |
Memory usage reportMemory usage unchanged ✅ |
|
9ea82c4 to
0df65a0
Compare
Member
Author
|
#24251 is what the "further generalisation" I have in mind would look like, but I don't want to open that PR until the functional |
sharkdp
approved these changes
Mar 30, 2026
Contributor
sharkdp
left a comment
There was a problem hiding this comment.
This makes sense to me, thank you.
0df65a0 to
825d237
Compare
carljm
added a commit
that referenced
this pull request
Mar 31, 2026
* main: (35 commits) Store definition indexes as u32 (#24307) Avoid re-using symbol in RUF024 fix (#24316) [ty] Add materialization to `Divergent` type (#24255) [ty] Make `Divergent` a top-level type variant (#24252) [ty] Fix nested global and nonlocal lookups through forwarding scopes (#24279) Fetch the cargo-dist binary directly instead of using the installer (#24258) [ty] Fix panic on `list[Annotated[()]]` (#24303) Don't measure the AST deallocation time in parser benchmarks (#24301) Enable CodSpeed's memory benchmarks for simulation benchmarks (#24298) Upgrade imara-diff to 0.2.0 (#24299) [ty] Represent `InitVar` as a special form internally, not a class (#24248) `RUF067`: Allow dunder-named assignments in non-strict mode [`pyupgrade`] UP018 should detect more unnecessarily wrapped literals (UP018) (#24093) [ty] Remove unused `system.glob` method (#24300) [ty] Reject functional TypedDict with mismatched name (#24295) Update Rust crate arc-swap to v1.9.0 (#24292) [ty] Remove unused `@Todo(Functional TypedDicts)` (#24297) Update CodSpeedHQ/action action to v4.12.1 (#24290) Update taiki-e/install-action action to v2.69.6 (#24293) Update Rust crate toml to v1.0.7 (#24289) ...
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
All type qualifiers are represented internally as variants of the
special_form::TypeQualifierenum. All type qualifiers, that is, exceptdataclasses.InitVar, which typeshed (accurately) tells us is a class rather than an instance oftyping._SpecialForm. This unfortunate fact means that we have quite a bit of special casing for specificallyInitVar, which is scattered around the ty codebase.This PR overrides typeshed to represent
InitVaras a special form internally, like all our other type qualifiers, rather than a class. While this is strictly more lines of code overall, it's a decrease in complexity and an increase in robustness: all our handling forInitVars now takes the same codepaths as our handling for other type qualifiers. There's significantly less ad-hoc special casing. This is similar to the way we already override typeshed forNamedTupleandAny-- typeshed tells us that both of these symbols are classes, too.There is a disadvantage to this PR, which is that we no longer have a way of representing "instance of the class
InitVar" internally (because we no longer recognizeInitVaras being a class at all). This means that we do have to introduce some new special casing to handle subscriptingInitVar, callingInitVar, and usingInitVaras the second argument toisinstance(). I still think this PR is worth it overall, though: runtime uses ofInitVarare very rare, and having all our type qualifiers be listed in theTypeQualifierenum opens the door to further generalisations in some of our checks, on top of the ones I've implemented here.Test Plan
mdtests extended