[red-knot] Add a new Type::KnownInstanceType variant#14155
Merged
Conversation
Type::KnownInstanceType variant
Contributor
|
carljm
approved these changes
Nov 7, 2024
Comment on lines
+356
to
+358
| Type::Instance(InstanceType { | ||
| class: class_literal.class, | ||
| }) |
Contributor
There was a problem hiding this comment.
We do this a lot in this PR, I wonder about a Class::to_instance_type() method for it?
Contributor
There was a problem hiding this comment.
This can be considered separately as a follow-up if it's worth it
Contributor
|
I think I might resolve conflicts and land this, if that's OK with you? I'm realizing that I think I want to represent a TypeVar (not as a type, but the type of the typevar's own symbol, e.g. |
Member
Author
|
Go for it! It's late here and I'm still travelling back from choir 👍 |
dc9d2c3 to
de09935
Compare
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
Fixes #14114. I don't think I can really describe the problems with our current architecture (and therefore the motivations for this PR) any better than @carljm did in that issue, so I'll just copy it out here!
We currently represent "known instances" (e.g. special forms like
typing.Literal, which are an instance oftyping._SpecialForm, but need to be handled differently from other instances oftyping._SpecialForm) as anInstanceTypewith aknownfield that isSome(...).This makes it easy to handle a known instance as if it were a regular instance type (by ignoring the
knownfield), and in some cases (e.g.Type::member) that is correct and convenient. But in other cases (e.g.Type::is_equivalent_to) it is not correct, and we currently have a bug that we would consider the known-instance type oftyping.Literalas equivalent to the general instance type fortyping._SpecialForm, and we would fail to consider it a singleton type or a single-valued type (even though it is both.)An instance type with
known.is_some()is semantically quite different from an instance type withknown.is_none(). The former is a singleton type that represents exactly one runtime object; the latter is an open type that represents many runtime objects, including instances of unknown subclasses. It is too error-prone to represent these very-different types as a singleTypevariant. We should instead introduce a dedicatedType::KnownInstancevariant and force ourselves to handle these explicitly in allTypevariant matches.Possible followups
There is still a little bit of awkwardness in our current design in some places, in that we first infer the symbol
typing.Literalas a_SpecialForminstance, and then later convert that instance-type into a known-instance-type. We could also use thisKnownInstanceTypeenum to account for other special runtime symbols such asbuiltins.Ellipsisorbuiltins.NotImplemented.I think these might be worth pursuing, but I didn't do them here as they didn't seem essential right now, and I wanted to keep the diff relatively minimal.
Test Plan
cargo test -p red_knot_python_semantic. New unit tests added forType::is_subtype_of.