mirror of https://github.com/astral-sh/ruff
[red-knot] MDTests: Do not depend on precise public-symbol type inference (#15691)
## Summary Another small PR to focus #15674 solely on the relevant changes. This makes our Markdown tests less dependent on precise types of public symbols, without actually changing anything semantically in these tests. Best reviewed using ignore-whitespace-mode. ## Test Plan Tested these changes on `main` and on the branch from #15674.
This commit is contained in:
parent
fc2ebea736
commit
15394a8028
|
|
@ -50,9 +50,7 @@ reveal_type(b | b) # revealed: Literal[False]
|
|||
## Arithmetic with a variable
|
||||
|
||||
```py
|
||||
a = True
|
||||
b = False
|
||||
|
||||
def _(a: bool):
|
||||
def lhs_is_int(x: int):
|
||||
reveal_type(x + a) # revealed: int
|
||||
reveal_type(x - a) # revealed: int
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ reveal_type(a.b) # revealed: <module 'a.b'>
|
|||
```
|
||||
|
||||
```py path=a/__init__.py
|
||||
b = 42
|
||||
b: int = 42
|
||||
```
|
||||
|
||||
```py path=a/b.py
|
||||
|
|
@ -20,11 +20,11 @@ b = 42
|
|||
```py
|
||||
from a import b
|
||||
|
||||
reveal_type(b) # revealed: Literal[42]
|
||||
reveal_type(b) # revealed: int
|
||||
```
|
||||
|
||||
```py path=a/__init__.py
|
||||
b = 42
|
||||
b: int = 42
|
||||
```
|
||||
|
||||
```py path=a/b.py
|
||||
|
|
@ -41,7 +41,7 @@ reveal_type(a.b) # revealed: <module 'a.b'>
|
|||
```
|
||||
|
||||
```py path=a/__init__.py
|
||||
b = 42
|
||||
b: int = 42
|
||||
```
|
||||
|
||||
```py path=a/b.py
|
||||
|
|
@ -60,13 +60,13 @@ sees the submodule as the value of `b` instead of the integer.
|
|||
from a import b
|
||||
import a.b
|
||||
|
||||
# Python would say `Literal[42]` for `b`
|
||||
# Python would say `int` for `b`
|
||||
reveal_type(b) # revealed: <module 'a.b'>
|
||||
reveal_type(a.b) # revealed: <module 'a.b'>
|
||||
```
|
||||
|
||||
```py path=a/__init__.py
|
||||
b = 42
|
||||
b: int = 42
|
||||
```
|
||||
|
||||
```py path=a/b.py
|
||||
|
|
|
|||
|
|
@ -20,12 +20,12 @@ from a import b.c
|
|||
|
||||
# TODO: Should these be inferred as Unknown?
|
||||
reveal_type(b) # revealed: <module 'a.b'>
|
||||
reveal_type(b.c) # revealed: Literal[1]
|
||||
reveal_type(b.c) # revealed: int
|
||||
```
|
||||
|
||||
```py path=a/__init__.py
|
||||
```
|
||||
|
||||
```py path=a/b.py
|
||||
c = 1
|
||||
c: int = 1
|
||||
```
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@ reveal_type(X) # revealed: Unknown
|
|||
```
|
||||
|
||||
```py path=package/foo.py
|
||||
X = 42
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
```py path=package/bar.py
|
||||
from .foo import X
|
||||
|
||||
reveal_type(X) # revealed: Literal[42]
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
## Dotted
|
||||
|
|
@ -32,25 +32,25 @@ reveal_type(X) # revealed: Literal[42]
|
|||
```
|
||||
|
||||
```py path=package/foo/bar/baz.py
|
||||
X = 42
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
```py path=package/bar.py
|
||||
from .foo.bar.baz import X
|
||||
|
||||
reveal_type(X) # revealed: Literal[42]
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
## Bare to package
|
||||
|
||||
```py path=package/__init__.py
|
||||
X = 42
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
```py path=package/bar.py
|
||||
from . import X
|
||||
|
||||
reveal_type(X) # revealed: Literal[42]
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
## Non-existent + bare to package
|
||||
|
|
@ -66,11 +66,11 @@ reveal_type(X) # revealed: Unknown
|
|||
```py path=package/__init__.py
|
||||
from .foo import X
|
||||
|
||||
reveal_type(X) # revealed: Literal[42]
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
```py path=package/foo.py
|
||||
X = 42
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
## Non-existent + dunder init
|
||||
|
|
@ -87,13 +87,13 @@ reveal_type(X) # revealed: Unknown
|
|||
```
|
||||
|
||||
```py path=package/foo.py
|
||||
X = 42
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
```py path=package/subpackage/subsubpackage/bar.py
|
||||
from ...foo import X
|
||||
|
||||
reveal_type(X) # revealed: Literal[42]
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
## Unbound symbol
|
||||
|
|
@ -117,13 +117,13 @@ reveal_type(x) # revealed: Unknown
|
|||
```
|
||||
|
||||
```py path=package/foo.py
|
||||
X = 42
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
```py path=package/bar.py
|
||||
from . import foo
|
||||
|
||||
reveal_type(foo.X) # revealed: Literal[42]
|
||||
reveal_type(foo.X) # revealed: int
|
||||
```
|
||||
|
||||
## Non-existent + bare to module
|
||||
|
|
@ -152,7 +152,7 @@ submodule via the attribute on its parent package.
|
|||
```
|
||||
|
||||
```py path=package/foo.py
|
||||
X = 42
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
```py path=package/bar.py
|
||||
|
|
|
|||
|
|
@ -10,10 +10,10 @@ def returns_bool() -> bool:
|
|||
return True
|
||||
|
||||
if returns_bool():
|
||||
chr = 1
|
||||
chr: int = 1
|
||||
|
||||
def f():
|
||||
reveal_type(chr) # revealed: Literal[chr] | Literal[1]
|
||||
reveal_type(chr) # revealed: Literal[chr] | int
|
||||
```
|
||||
|
||||
## Conditionally global or builtin, with annotation
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ version:
|
|||
import sys
|
||||
|
||||
if sys.version_info >= (3, 9):
|
||||
SomeFeature = "available"
|
||||
SomeFeature: str = "available"
|
||||
```
|
||||
|
||||
If we can statically determine that the condition is always true, then we can also understand that
|
||||
|
|
@ -21,7 +21,7 @@ If we can statically determine that the condition is always true, then we can al
|
|||
from module1 import SomeFeature
|
||||
|
||||
# SomeFeature is unconditionally available here, because we are on Python 3.9 or newer:
|
||||
reveal_type(SomeFeature) # revealed: Literal["available"]
|
||||
reveal_type(SomeFeature) # revealed: str
|
||||
```
|
||||
|
||||
Another scenario where this is useful is for `typing.TYPE_CHECKING` branches, which are often used
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ def _(n: int):
|
|||
## Slices
|
||||
|
||||
```py
|
||||
b = b"\x00abc\xff"
|
||||
b: bytes = b"\x00abc\xff"
|
||||
|
||||
reveal_type(b[0:2]) # revealed: Literal[b"\x00a"]
|
||||
reveal_type(b[-3:]) # revealed: Literal[b"bc\xff"]
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ def _(n: int):
|
|||
## Slices
|
||||
|
||||
```py
|
||||
def _(m: int, n: int, s2: str):
|
||||
s = "abcde"
|
||||
|
||||
reveal_type(s[0:0]) # revealed: Literal[""]
|
||||
|
|
@ -73,7 +74,6 @@ s[:4:0] # error: [zero-stepsize-in-slice]
|
|||
s[0::0] # error: [zero-stepsize-in-slice]
|
||||
s[::0] # error: [zero-stepsize-in-slice]
|
||||
|
||||
def _(m: int, n: int, s2: str):
|
||||
substring1 = s[m:n]
|
||||
# TODO: Support overloads... Should be `LiteralString`
|
||||
reveal_type(substring1) # revealed: @Todo(return type)
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ reveal_type(b) # revealed: Unknown
|
|||
## Slices
|
||||
|
||||
```py
|
||||
def _(m: int, n: int):
|
||||
t = (1, "a", None, b"b")
|
||||
|
||||
reveal_type(t[0:0]) # revealed: tuple[()]
|
||||
|
|
@ -67,7 +68,6 @@ t[:4:0] # error: [zero-stepsize-in-slice]
|
|||
t[0::0] # error: [zero-stepsize-in-slice]
|
||||
t[::0] # error: [zero-stepsize-in-slice]
|
||||
|
||||
def _(m: int, n: int):
|
||||
tuple_slice = t[m:n]
|
||||
# TODO: Support overloads... Should be `tuple[Literal[1, 'a', b"b"] | None, ...]`
|
||||
reveal_type(tuple_slice) # revealed: @Todo(return type)
|
||||
|
|
|
|||
Loading…
Reference in New Issue