-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
type[a.X]
with qualified class names (#14825)
This adds support for `type[a.X]`, where the `type` special form is applied to a qualified name that resolves to a class literal. This works for both nested classes and classes imported from another module. Closes #14545
- Loading branch information
Showing
2 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
crates/red_knot_python_semantic/resources/mdtest/type_of/basic.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
reveal_type(f()) # revealed: @Todo(unsupported type[X] special form) | ||
``` | ||
|
||
```py path=a/__init__.py | ||
``` | ||
|
||
```py path=a/b.py | ||
class C: ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters