Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions crates/ty_python_semantic/resources/mdtest/implicit_type_aliases.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ IntOrAnnotated = int | Annotated[str, "meta"]
AnnotatedOrInt = Annotated[str, "meta"] | int
IntOrOptional = int | Optional[str]
OptionalOrInt = Optional[str] | int
IntOrTypeOfStr = int | type[str]
TypeOfStrOrInt = type[str] | int

reveal_type(IntOrStr) # revealed: types.UnionType
reveal_type(IntOrStrOrBytes1) # revealed: types.UnionType
Expand Down Expand Up @@ -97,6 +99,8 @@ reveal_type(IntOrAnnotated) # revealed: types.UnionType
reveal_type(AnnotatedOrInt) # revealed: types.UnionType
reveal_type(IntOrOptional) # revealed: types.UnionType
reveal_type(OptionalOrInt) # revealed: types.UnionType
reveal_type(IntOrTypeOfStr) # revealed: types.UnionType
reveal_type(TypeOfStrOrInt) # revealed: types.UnionType

def _(
int_or_str: IntOrStr,
Expand Down Expand Up @@ -129,6 +133,8 @@ def _(
annotated_or_int: AnnotatedOrInt,
int_or_optional: IntOrOptional,
optional_or_int: OptionalOrInt,
int_or_type_of_str: IntOrTypeOfStr,
type_of_str_or_int: TypeOfStrOrInt,
):
reveal_type(int_or_str) # revealed: int | str
reveal_type(int_or_str_or_bytes1) # revealed: int | str | bytes
Expand Down Expand Up @@ -160,6 +166,8 @@ def _(
reveal_type(annotated_or_int) # revealed: str | int
reveal_type(int_or_optional) # revealed: int | str | None
reveal_type(optional_or_int) # revealed: str | None | int
reveal_type(int_or_type_of_str) # revealed: int | type[str]
reveal_type(type_of_str_or_int) # revealed: type[str] | int
```

If a type is unioned with itself in a value expression, the result is just that type. No
Expand Down Expand Up @@ -599,6 +607,158 @@ def _(
reveal_type(invalid) # revealed: str | Unknown
```

## `type[…]` and `Type[…]`

### `type[…]`

We support implicit type aliases using `type[…]`:

```py
from typing import Any, Union, Protocol, TypeVar, Generic

T = TypeVar("T")

class A: ...
class B: ...
class G(Generic[T]): ...

class P(Protocol):
def method(self) -> None: ...

SubclassOfA = type[A]
SubclassOfAny = type[Any]
SubclassOfAOrB1 = type[A | B]
SubclassOfAOrB2 = type[A] | type[B]
SubclassOfAOrB3 = Union[type[A], type[B]]
SubclassOfG = type[G]
SubclassOfGInt = type[G[int]]
SubclassOfP = type[P]

reveal_type(SubclassOfA) # revealed: GenericAlias
reveal_type(SubclassOfAny) # revealed: GenericAlias
reveal_type(SubclassOfAOrB1) # revealed: GenericAlias
reveal_type(SubclassOfAOrB2) # revealed: types.UnionType
reveal_type(SubclassOfAOrB3) # revealed: types.UnionType
reveal_type(SubclassOfG) # revealed: GenericAlias
reveal_type(SubclassOfGInt) # revealed: GenericAlias
reveal_type(SubclassOfP) # revealed: GenericAlias

def _(
subclass_of_a: SubclassOfA,
subclass_of_any: SubclassOfAny,
subclass_of_a_or_b1: SubclassOfAOrB1,
subclass_of_a_or_b2: SubclassOfAOrB2,
subclass_of_a_or_b3: SubclassOfAOrB3,
subclass_of_g: SubclassOfG,
subclass_of_g_int: SubclassOfGInt,
subclass_of_p: SubclassOfP,
):
reveal_type(subclass_of_a) # revealed: type[A]
reveal_type(subclass_of_a()) # revealed: A

reveal_type(subclass_of_any) # revealed: type[Any]
reveal_type(subclass_of_any()) # revealed: Any

reveal_type(subclass_of_a_or_b1) # revealed: type[A] | type[B]
reveal_type(subclass_of_a_or_b1()) # revealed: A | B

reveal_type(subclass_of_a_or_b2) # revealed: type[A] | type[B]
reveal_type(subclass_of_a_or_b2()) # revealed: A | B

reveal_type(subclass_of_a_or_b3) # revealed: type[A] | type[B]
reveal_type(subclass_of_a_or_b3()) # revealed: A | B

reveal_type(subclass_of_g) # revealed: type[G[Unknown]]
reveal_type(subclass_of_g()) # revealed: G[Unknown]

reveal_type(subclass_of_g_int) # revealed: type[G[int]]
reveal_type(subclass_of_g_int()) # revealed: G[int]

reveal_type(subclass_of_p) # revealed: type[P]
```

Invalid uses result in diagnostics:

```py
# error: [invalid-type-form]
InvalidSubclass = type[1]
```

### `Type[…]`

The same also works for `typing.Type[…]`:

```py
from typing import Any, Union, Protocol, TypeVar, Generic, Type

T = TypeVar("T")

class A: ...
class B: ...
class G(Generic[T]): ...

class P(Protocol):
def method(self) -> None: ...

SubclassOfA = Type[A]
SubclassOfAny = Type[Any]
SubclassOfAOrB1 = Type[A | B]
SubclassOfAOrB2 = Type[A] | Type[B]
SubclassOfAOrB3 = Union[Type[A], Type[B]]
SubclassOfG = Type[G]
SubclassOfGInt = Type[G[int]]
SubclassOfP = Type[P]

reveal_type(SubclassOfA) # revealed: GenericAlias
reveal_type(SubclassOfAny) # revealed: GenericAlias
reveal_type(SubclassOfAOrB1) # revealed: GenericAlias
reveal_type(SubclassOfAOrB2) # revealed: types.UnionType
reveal_type(SubclassOfAOrB3) # revealed: types.UnionType
reveal_type(SubclassOfG) # revealed: GenericAlias
reveal_type(SubclassOfGInt) # revealed: GenericAlias
reveal_type(SubclassOfP) # revealed: GenericAlias

def _(
subclass_of_a: SubclassOfA,
subclass_of_any: SubclassOfAny,
subclass_of_a_or_b1: SubclassOfAOrB1,
subclass_of_a_or_b2: SubclassOfAOrB2,
subclass_of_a_or_b3: SubclassOfAOrB3,
subclass_of_g: SubclassOfG,
subclass_of_g_int: SubclassOfGInt,
subclass_of_p: SubclassOfP,
):
reveal_type(subclass_of_a) # revealed: type[A]
reveal_type(subclass_of_a()) # revealed: A

reveal_type(subclass_of_any) # revealed: type[Any]
reveal_type(subclass_of_any()) # revealed: Any

reveal_type(subclass_of_a_or_b1) # revealed: type[A] | type[B]
reveal_type(subclass_of_a_or_b1()) # revealed: A | B

reveal_type(subclass_of_a_or_b2) # revealed: type[A] | type[B]
reveal_type(subclass_of_a_or_b2()) # revealed: A | B

reveal_type(subclass_of_a_or_b3) # revealed: type[A] | type[B]
reveal_type(subclass_of_a_or_b3()) # revealed: A | B

reveal_type(subclass_of_g) # revealed: type[G[Unknown]]
reveal_type(subclass_of_g()) # revealed: G[Unknown]

reveal_type(subclass_of_g_int) # revealed: type[G[int]]
reveal_type(subclass_of_g_int()) # revealed: G[int]

reveal_type(subclass_of_p) # revealed: type[P]
```

Invalid uses result in diagnostics:

```py
# error: [invalid-type-form]
InvalidSubclass = Type[1]
```

## Stringified annotations?

From the [typing spec on type aliases](https://typing.python.org/en/latest/spec/aliases.html):
Expand Down Expand Up @@ -633,15 +793,18 @@ from typing import Union

ListOfInts = list["int"]
StrOrStyle = Union[str, "Style"]
SubclassOfStyle = type["Style"]

class Style: ...

def _(
list_of_ints: ListOfInts,
str_or_style: StrOrStyle,
subclass_of_style: SubclassOfStyle,
):
reveal_type(list_of_ints) # revealed: list[int]
reveal_type(str_or_style) # revealed: str | Style
reveal_type(subclass_of_style) # revealed: type[Style]
```

## Recursive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ from ty_extensions import reveal_mro

class Foo(type[int]): ...

# TODO: should be `tuple[<class 'Foo'>, <class 'type'>, <class 'object'>]
reveal_mro(Foo) # revealed: (<class 'Foo'>, @Todo(GenericAlias instance), <class 'object'>)
reveal_mro(Foo) # revealed: (<class 'Foo'>, <class 'type'>, <class 'object'>)
```

## Display of generic `type[]` types
Expand Down
25 changes: 22 additions & 3 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6607,6 +6607,17 @@ impl<'db> Type<'db> {
.inner(db)
.in_type_expression(db, scope_id, typevar_binding_context)?)
}
KnownInstanceType::TypeGenericAlias(ty) => {
// When `type[…]` appears in a value position (e.g. in an implicit type alias),
// we infer its argument as a type expression. This ensures that we can emit
// diagnostics for invalid type expressions, and more importantly, that we can
// make use of stringified annotations. The drawback is that we need to turn
// instances back into the corresponding subclass-of types here. This process
// (`int` -> instance of `int` -> subclass of `int`) can be lossy, but it is
// okay for all valid arguments to `type[…]`.

Ok(ty.inner(db).to_meta_type(db))
}
},

Type::SpecialForm(special_form) => match special_form {
Expand Down Expand Up @@ -7847,6 +7858,9 @@ pub enum KnownInstanceType<'db> {
/// A single instance of `typing.Annotated`
Annotated(InternedType<'db>),

/// An instance of `typing.GenericAlias` representing a `type[...]` expression.
TypeGenericAlias(InternedType<'db>),

/// An identity callable created with `typing.NewType(name, base)`, which behaves like a
/// subtype of `base` in type expressions. See the `struct NewType` payload for an example.
NewType(NewType<'db>),
Expand Down Expand Up @@ -7881,7 +7895,9 @@ fn walk_known_instance_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
visitor.visit_type(db, *element);
}
}
KnownInstanceType::Literal(ty) | KnownInstanceType::Annotated(ty) => {
KnownInstanceType::Literal(ty)
| KnownInstanceType::Annotated(ty)
| KnownInstanceType::TypeGenericAlias(ty) => {
visitor.visit_type(db, ty.inner(db));
}
KnownInstanceType::NewType(newtype) => {
Expand Down Expand Up @@ -7928,6 +7944,7 @@ impl<'db> KnownInstanceType<'db> {
Self::UnionType(list) => Self::UnionType(list.normalized_impl(db, visitor)),
Self::Literal(ty) => Self::Literal(ty.normalized_impl(db, visitor)),
Self::Annotated(ty) => Self::Annotated(ty.normalized_impl(db, visitor)),
Self::TypeGenericAlias(ty) => Self::TypeGenericAlias(ty.normalized_impl(db, visitor)),
Self::NewType(newtype) => Self::NewType(
newtype
.map_base_class_type(db, |class_type| class_type.normalized_impl(db, visitor)),
Expand All @@ -7950,8 +7967,9 @@ impl<'db> KnownInstanceType<'db> {
Self::Field(_) => KnownClass::Field,
Self::ConstraintSet(_) => KnownClass::ConstraintSet,
Self::UnionType(_) => KnownClass::UnionType,
Self::Literal(_) => KnownClass::GenericAlias,
Self::Annotated(_) => KnownClass::GenericAlias,
Self::Literal(_) | Self::Annotated(_) | Self::TypeGenericAlias(_) => {
KnownClass::GenericAlias
}
Self::NewType(_) => KnownClass::NewType,
}
}
Expand Down Expand Up @@ -8037,6 +8055,7 @@ impl<'db> KnownInstanceType<'db> {
KnownInstanceType::Annotated(_) => {
f.write_str("<typing.Annotated special form>")
}
KnownInstanceType::TypeGenericAlias(_) => f.write_str("GenericAlias"),
KnownInstanceType::NewType(declaration) => {
write!(f, "<NewType pseudo-class '{}'>", declaration.name(self.db))
}
Expand Down
3 changes: 3 additions & 0 deletions crates/ty_python_semantic/src/types/class_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ impl<'db> ClassBase<'db> {
// wrappers are just identity callables at runtime, so this sort of inheritance
// doesn't work and isn't allowed.
| KnownInstanceType::NewType(_) => None,
KnownInstanceType::TypeGenericAlias(_) => {
Self::try_from_type(db, KnownClass::Type.to_class_literal(db), subclass)
Copy link
Contributor Author

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 what class Foo(type[int]): ... tries to achieve.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand what class Foo(type[int]): ... tries to achieve.

Nor do I. I think this is fine.

}
KnownInstanceType::Annotated(ty) => Self::try_from_type(db, ty.inner(db), subclass),
},

Expand Down
19 changes: 17 additions & 2 deletions crates/ty_python_semantic/src/types/infer/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..)
Expand All @@ -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() => {
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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 type[]. For example, on your branch:

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'>`

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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),
));
}
_ => {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
self.infer_type_expression(slice);
todo_type!("Generic specialization of types.UnionType")
}
KnownInstanceType::Literal(ty) => {
KnownInstanceType::Literal(ty) | KnownInstanceType::TypeGenericAlias(ty) => {
self.infer_type_expression(slice);
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
builder.into_diagnostic(format_args!(
Expand Down
Loading