-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[red-knot] Add a new Type::KnownInstanceType variant
#14155
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
Conversation
Type::KnownInstanceType variant
|
carljm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thank you!!
| Type::Instance(InstanceType { | ||
| class: class_literal.class, | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do this a lot in this PR, I wonder about a Class::to_instance_type() method for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be considered separately as a follow-up if it's worth it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
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. |
|
Go for it! It's late here and I'm still travelling back from choir 👍 |
dc9d2c3 to
de09935
Compare
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.