[red-knot] Disallow more invalid type expressions #16427
Merged
+131
−40
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
As part of working on #16302, I'm auditing all uses of
Type::to_instance()
. I noticed that one use inType::in_type_expression
has a pretty clear bug, in that it treatsType::SubclassOf()
the same asType::ClassLiteral
. That's incorrect: an object can only have atype[]
type if it's a dynamic variable of some kind, and we should reject such variables in type expressions. The practical effect of this is that onmain
, we have this incorrect behaviour, where we should be rejecty
's type annotation altogether, but instead we accept it:This PR fixes the bug by making
Type::in_type_expression()
exhaustive over allType
variants and rejecting most of them when they appear in type expressions. (The method is not yet exhaustive over allKnownInstanceType
variants; some of these would require special error messages, and it seemed out of the scope of this bugfix PR.)Test Plan
Added mdtests.