Update class literal display to use <class 'Foo'> style (#17889)
## Summary Closes https://github.com/astral-sh/ruff/issues/17238.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
from b import C as D
|
||||
|
||||
E = D
|
||||
reveal_type(E) # revealed: Literal[C]
|
||||
reveal_type(E) # revealed: <class 'C'>
|
||||
```
|
||||
|
||||
`b.py`:
|
||||
@@ -21,7 +21,7 @@ class C: ...
|
||||
import b
|
||||
|
||||
D = b.C
|
||||
reveal_type(D) # revealed: Literal[C]
|
||||
reveal_type(D) # revealed: <class 'C'>
|
||||
```
|
||||
|
||||
`b.py`:
|
||||
@@ -35,7 +35,7 @@ class C: ...
|
||||
```py
|
||||
import a.b
|
||||
|
||||
reveal_type(a.b.C) # revealed: Literal[C]
|
||||
reveal_type(a.b.C) # revealed: <class 'C'>
|
||||
```
|
||||
|
||||
`a/__init__.py`:
|
||||
@@ -54,7 +54,7 @@ class C: ...
|
||||
```py
|
||||
import a.b.c
|
||||
|
||||
reveal_type(a.b.c.C) # revealed: Literal[C]
|
||||
reveal_type(a.b.c.C) # revealed: <class 'C'>
|
||||
```
|
||||
|
||||
`a/__init__.py`:
|
||||
@@ -78,7 +78,7 @@ class C: ...
|
||||
```py
|
||||
import a.b as b
|
||||
|
||||
reveal_type(b.C) # revealed: Literal[C]
|
||||
reveal_type(b.C) # revealed: <class 'C'>
|
||||
```
|
||||
|
||||
`a/__init__.py`:
|
||||
@@ -97,7 +97,7 @@ class C: ...
|
||||
```py
|
||||
import a.b.c as c
|
||||
|
||||
reveal_type(c.C) # revealed: Literal[C]
|
||||
reveal_type(c.C) # revealed: <class 'C'>
|
||||
```
|
||||
|
||||
`a/__init__.py`:
|
||||
|
||||
@@ -16,7 +16,7 @@ Or used implicitly:
|
||||
|
||||
```py
|
||||
reveal_type(chr) # revealed: def chr(i: SupportsIndex, /) -> str
|
||||
reveal_type(str) # revealed: Literal[str]
|
||||
reveal_type(str) # revealed: <class 'str'>
|
||||
```
|
||||
|
||||
## Builtin symbol from custom typeshed
|
||||
|
||||
@@ -95,6 +95,7 @@ from typing import Any, Literal
|
||||
`foo.pyi`:
|
||||
|
||||
```pyi
|
||||
|
||||
```
|
||||
|
||||
## Nested non-exports
|
||||
@@ -187,7 +188,7 @@ reveal_type(Foo) # revealed: Unknown
|
||||
```pyi
|
||||
from b import AnyFoo as Foo
|
||||
|
||||
reveal_type(Foo) # revealed: Literal[AnyFoo]
|
||||
reveal_type(Foo) # revealed: <class 'AnyFoo'>
|
||||
```
|
||||
|
||||
`b.pyi`:
|
||||
@@ -251,11 +252,13 @@ class Foo: ...
|
||||
`a/b/__init__.pyi`:
|
||||
|
||||
```pyi
|
||||
|
||||
```
|
||||
|
||||
`a/b/c.pyi`:
|
||||
|
||||
```pyi
|
||||
|
||||
```
|
||||
|
||||
## Conditional re-export in stub file
|
||||
@@ -281,7 +284,7 @@ def coinflip() -> bool: ...
|
||||
if coinflip():
|
||||
Foo: str = ...
|
||||
|
||||
reveal_type(Foo) # revealed: Literal[Foo] | str
|
||||
reveal_type(Foo) # revealed: <class 'Foo'> | str
|
||||
```
|
||||
|
||||
`b.pyi`:
|
||||
@@ -299,7 +302,7 @@ the other does not.
|
||||
# error: "Member `Foo` of module `a` is possibly unbound"
|
||||
from a import Foo
|
||||
|
||||
reveal_type(Foo) # revealed: Literal[Foo]
|
||||
reveal_type(Foo) # revealed: <class 'Foo'>
|
||||
```
|
||||
|
||||
`a.pyi`:
|
||||
@@ -312,7 +315,7 @@ if coinflip():
|
||||
else:
|
||||
from b import Foo as Foo
|
||||
|
||||
reveal_type(Foo) # revealed: Literal[Foo]
|
||||
reveal_type(Foo) # revealed: <class 'Foo'>
|
||||
```
|
||||
|
||||
`b.pyi`:
|
||||
@@ -327,7 +330,7 @@ class Foo: ...
|
||||
# error: "Member `Foo` of module `a` is possibly unbound"
|
||||
from a import Foo
|
||||
|
||||
reveal_type(Foo) # revealed: Literal[Foo]
|
||||
reveal_type(Foo) # revealed: <class 'Foo'>
|
||||
```
|
||||
|
||||
`a.pyi`:
|
||||
|
||||
@@ -69,12 +69,12 @@ x = "foo" # error: [invalid-assignment] "Object of type `Literal["foo"]"
|
||||
```py
|
||||
class A: ...
|
||||
|
||||
reveal_type(A.__mro__) # revealed: tuple[Literal[A], Literal[object]]
|
||||
reveal_type(A.__mro__) # revealed: tuple[<class 'A'>, <class 'object'>]
|
||||
import b
|
||||
|
||||
class C(b.B): ...
|
||||
|
||||
reveal_type(C.__mro__) # revealed: tuple[Literal[C], Literal[B], Literal[A], Literal[object]]
|
||||
reveal_type(C.__mro__) # revealed: tuple[<class 'C'>, <class 'B'>, <class 'A'>, <class 'object'>]
|
||||
```
|
||||
|
||||
`b.py`:
|
||||
@@ -84,5 +84,5 @@ from a import A
|
||||
|
||||
class B(A): ...
|
||||
|
||||
reveal_type(B.__mro__) # revealed: tuple[Literal[B], Literal[A], Literal[object]]
|
||||
reveal_type(B.__mro__) # revealed: tuple[<class 'B'>, <class 'A'>, <class 'object'>]
|
||||
```
|
||||
|
||||
@@ -255,7 +255,7 @@ python-version = "3.13"
|
||||
```py
|
||||
from foo import A
|
||||
|
||||
reveal_type(A) # revealed: Literal[A]
|
||||
reveal_type(A) # revealed: <class 'A'>
|
||||
```
|
||||
|
||||
`/src/.venv/<path-to-site-packages>/foo/__init__.py`:
|
||||
|
||||
@@ -1353,7 +1353,7 @@ are present due to `*` imports.
|
||||
```py
|
||||
import collections.abc
|
||||
|
||||
reveal_type(collections.abc.Sequence) # revealed: Literal[Sequence]
|
||||
reveal_type(collections.abc.Sequence) # revealed: <class 'Sequence'>
|
||||
reveal_type(collections.abc.Callable) # revealed: typing.Callable
|
||||
|
||||
# TODO: false positive as it's only re-exported from `_collections.abc` due to presence in `__all__`
|
||||
|
||||
@@ -27,7 +27,7 @@ has been imported.
|
||||
import a
|
||||
|
||||
# Would be an error with flow-sensitive tracking
|
||||
reveal_type(a.b.C) # revealed: Literal[C]
|
||||
reveal_type(a.b.C) # revealed: <class 'C'>
|
||||
|
||||
import a.b
|
||||
```
|
||||
@@ -53,10 +53,10 @@ submodule `b`, even though `a.b` is never imported in the main module.
|
||||
from q import a, b
|
||||
|
||||
reveal_type(b) # revealed: <module 'a.b'>
|
||||
reveal_type(b.C) # revealed: Literal[C]
|
||||
reveal_type(b.C) # revealed: <class 'C'>
|
||||
|
||||
reveal_type(a.b) # revealed: <module 'a.b'>
|
||||
reveal_type(a.b.C) # revealed: Literal[C]
|
||||
reveal_type(a.b.C) # revealed: <class 'C'>
|
||||
```
|
||||
|
||||
`a/__init__.py`:
|
||||
|
||||
Reference in New Issue
Block a user