Skip to content
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

Support type[a.X] with qualified class names #14825

Merged
merged 2 commits into from
Dec 6, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# type special form

## Class literal

```py
class A: ...

def f() -> type[A]:
return A

reveal_type(f()) # revealed: type[A]
```

## Nested class literal

```py
class A:
class B: ...

def f() -> type[A.B]:
return A.B

reveal_type(f()) # revealed: type[B]
```

## Deeply nested class literal

```py
class A:
class B:
class C: ...

def f() -> type[A.B.C]:
return A.B.C

reveal_type(f()) # revealed: type[C]
```

## Class literal from another module

```py
from a import A

def f() -> type[A]:
return A

reveal_type(f()) # revealed: type[A]
```

```py path=a.py
class A: ...
```

## Qualified class literal from another module

```py
import a

def f() -> type[a.B]:
return a.B

reveal_type(f()) # revealed: type[B]
```

```py path=a.py
class B: ...
```

## Deeply qualified class literal from another module

```py path=a/test.py
import a.b

# TODO: no diagnostic
# error: [unresolved-attribute]
def f() -> type[a.b.C]:
# TODO: no diagnostic
# error: [unresolved-attribute]
return a.b.C
Comment on lines +74 to +79
Copy link
Member Author

@dcreager dcreager Dec 6, 2024

Choose a reason for hiding this comment

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

I left this as a TODO because I think this is not specific to type[]. The unresolved-attribute error I get is:

Type <module 'System("/src/a/b.py")'> has no attribute b

which suggests that our import logic is binding a as a.b instead of a. Am I interpreting that right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it looks like you are! I think this is a previously-not-known bug, unless @AlexWaygood was already aware of it. Want to file an issue (labeled red-knot)? Could also be an interesting one for you to look into, would get you some exposure to the module resolver, which I don't think any of the other tasks do.

Copy link
Member

Choose a reason for hiding this comment

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

I think I already knew about it, and I think we already have a TODO about it... whether we have an issue is another question... it's late here, so I won't look into whether we do or don't this evening :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened #14826, can close it as a dup if it turns out we have another one already open


reveal_type(f()) # revealed: @Todo(unsupported type[X] special form)
```

```py path=a/__init__.py
```

```py path=a/b.py
class C: ...
```
8 changes: 4 additions & 4 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4552,18 +4552,18 @@ impl<'db> TypeInferenceBuilder<'db> {
/// Given the slice of a `type[]` annotation, return the type that the annotation represents
fn infer_subclass_of_type_expression(&mut self, slice: &ast::Expr) -> Type<'db> {
match slice {
ast::Expr::Name(_) => {
ast::Expr::Name(_) | ast::Expr::Attribute(_) => {
let name_ty = self.infer_expression(slice);
if let Some(ClassLiteralType { class }) = name_ty.into_class_literal() {
Type::subclass_of(class)
} else {
todo_type!()
todo_type!("unsupported type[X] special form")
}
}
// TODO: attributes, unions, subscripts, etc.
// TODO: unions, subscripts, etc.
_ => {
self.infer_type_expression(slice);
todo_type!()
todo_type!("unsupported type[X] special form")
}
}
}
Expand Down
Loading