-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Support type[…] and Type[…] in implicit type aliases
#21421
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9456,7 +9456,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { | |
| | Type::KnownInstance( | ||
| KnownInstanceType::UnionType(_) | ||
| | KnownInstanceType::Literal(_) | ||
| | KnownInstanceType::Annotated(_), | ||
| | KnownInstanceType::Annotated(_) | ||
| | KnownInstanceType::TypeGenericAlias(_), | ||
| ), | ||
| Type::ClassLiteral(..) | ||
| | Type::SubclassOf(..) | ||
|
|
@@ -9465,7 +9466,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { | |
| | Type::KnownInstance( | ||
| KnownInstanceType::UnionType(_) | ||
| | KnownInstanceType::Literal(_) | ||
| | KnownInstanceType::Annotated(_), | ||
| | KnownInstanceType::Annotated(_) | ||
| | KnownInstanceType::TypeGenericAlias(_), | ||
| ), | ||
| ast::Operator::BitOr, | ||
| ) if pep_604_unions_allowed() => { | ||
|
|
@@ -10626,7 +10628,13 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { | |
| // special cases, too. | ||
| if class.is_tuple(self.db()) { | ||
| return tuple_generic_alias(self.db(), self.infer_tuple_type_expression(slice)); | ||
| } else if class.is_known(self.db(), KnownClass::Type) { | ||
| let argument_ty = self.infer_type_expression(slice); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is fine for now, but note that it results in us being too permissive when parsing the argument to from typing import Literal
X = type[Literal[42]] # no error here, but there should be?
def f(
x: type[Literal[42]], # error: [invalid-type-form]
y: X # no error here either...
):
reveal_type(x) # revealed: @Todo(unsupported nested subscript in type[X])
reveal_type(y) # revealed: `<class 'int'>`
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I'll add a test with a TODO in my next PR. |
||
| return Type::KnownInstance(KnownInstanceType::TypeGenericAlias( | ||
| InternedType::new(self.db(), argument_ty), | ||
| )); | ||
| } | ||
|
|
||
| if let Some(generic_context) = class.generic_context(self.db()) { | ||
| return self.infer_explicit_class_specialization( | ||
| subscript, | ||
|
|
@@ -10763,6 +10771,13 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { | |
| } | ||
| } | ||
| } | ||
| Type::SpecialForm(SpecialFormType::Type) => { | ||
| // Similar to the branch above that handles `type[…]`, handle `typing.Type[…]` | ||
| let argument_ty = self.infer_type_expression(slice); | ||
| return Type::KnownInstance(KnownInstanceType::TypeGenericAlias( | ||
| InternedType::new(self.db(), argument_ty), | ||
| )); | ||
| } | ||
| _ => {} | ||
| } | ||
|
|
||
|
|
||
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 is mostly based on the TODO comment in
mdtest/type_of/basic.md. Do we need to preserve more information here? I don't really understand whatclass Foo(type[int]): ...tries to achieve.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.
Nor do I. I think this is fine.