[red-knot] Autoformat mdtest Python snippets using blacken-docs (#13809)

This commit is contained in:
Alex Waygood
2024-10-19 15:57:06 +01:00
committed by GitHub
parent 2ff36530c3
commit 36cb1199cc
45 changed files with 331 additions and 132 deletions

View File

@@ -3,8 +3,8 @@
## Simple
```py
reveal_type(b'red' b'knot') # revealed: Literal[b"redknot"]
reveal_type(b'hello') # revealed: Literal[b"hello"]
reveal_type(b'world' + b'!') # revealed: Literal[b"world!"]
reveal_type(b'\xff\x00') # revealed: Literal[b"\xff\x00"]
reveal_type(b"red" b"knot") # revealed: Literal[b"redknot"]
reveal_type(b"hello") # revealed: Literal[b"hello"]
reveal_type(b"world" + b"!") # revealed: Literal[b"world!"]
reveal_type(b"\xff\x00") # revealed: Literal[b"\xff\x00"]
```

View File

@@ -3,7 +3,9 @@
## Class getitem unbound
```py
class NotSubscriptable: pass
class NotSubscriptable: ...
a = NotSubscriptable[0] # error: "Cannot subscript object of type `Literal[NotSubscriptable]` with no `__class_getitem__` method"
```
@@ -14,6 +16,7 @@ class Identity:
def __class_getitem__(cls, item: int) -> str:
return item
reveal_type(Identity[0]) # revealed: str
```
@@ -22,15 +25,20 @@ reveal_type(Identity[0]) # revealed: str
```py
flag = True
class Identity:
class UnionClassGetItem:
if flag:
def __class_getitem__(cls, item: int) -> str:
return item
else:
def __class_getitem__(cls, item: int) -> int:
return item
reveal_type(Identity[0]) # revealed: str | int
reveal_type(UnionClassGetItem[0]) # revealed: str | int
```
## Class getitem with class union
@@ -38,21 +46,21 @@ reveal_type(Identity[0]) # revealed: str | int
```py
flag = True
class Identity1:
class A:
def __class_getitem__(cls, item: int) -> str:
return item
class Identity2:
class B:
def __class_getitem__(cls, item: int) -> int:
return item
if flag:
a = Identity1
else:
a = Identity2
reveal_type(a) # revealed: Literal[Identity1, Identity2]
reveal_type(a[0]) # revealed: str | int
x = A if flag else B
reveal_type(x) # revealed: Literal[A, B]
reveal_type(x[0]) # revealed: str | int
```
## Class getitem with unbound method union
@@ -61,14 +69,19 @@ reveal_type(a[0]) # revealed: str | int
flag = True
if flag:
class Identity:
def __class_getitem__(self, x: int) -> str:
pass
else:
class Identity: pass
a = Identity[42] # error: [call-non-callable] "Method `__class_getitem__` of type `Literal[__class_getitem__] | Unbound` is not callable on object of type `Literal[Identity, Identity]`"
reveal_type(a) # revealed: str | Unknown
class Spam:
def __class_getitem__(self, x: int) -> str:
return "foo"
else:
class Spam: ...
# error: [call-non-callable] "Method `__class_getitem__` of type `Literal[__class_getitem__] | Unbound` is not callable on object of type `Literal[Spam, Spam]`"
# revealed: str | Unknown
reveal_type(Spam[42])
```
## TODO: Class getitem non-class union
@@ -77,13 +90,16 @@ reveal_type(a) # revealed: str | Unknown
flag = True
if flag:
class Identity:
def __class_getitem__(self, x: int) -> str:
pass
else:
Identity = 1
a = Identity[42] # error: "Cannot subscript object of type `Literal[Identity] | Literal[1]` with no `__getitem__` method"
# TODO: should _probably_ emit `str | Unknown`
reveal_type(a) # revealed: Unknown
class Eggs:
def __class_getitem__(self, x: int) -> str:
return "foo"
else:
Eggs = 1
a = Eggs[42] # error: "Cannot subscript object of type `Literal[Eggs] | Literal[1]` with no `__getitem__` method"
# TODO: should _probably_ emit `str | Unknown`
reveal_type(a) # revealed: Unknown
```

View File

@@ -3,7 +3,9 @@
## Getitem unbound
```py
class NotSubscriptable: pass
class NotSubscriptable: ...
a = NotSubscriptable()[0] # error: "Cannot subscript object of type `NotSubscriptable` with no `__getitem__` method"
```
@@ -13,6 +15,7 @@ a = NotSubscriptable()[0] # error: "Cannot subscript object of type `NotSubscri
class NotSubscriptable:
__getitem__ = None
a = NotSubscriptable()[0] # error: "Method `__getitem__` of type `None` is not callable on object of type `NotSubscriptable`"
```
@@ -23,6 +26,7 @@ class Identity:
def __getitem__(self, index: int) -> int:
return index
reveal_type(Identity()[0]) # revealed: int
```
@@ -31,13 +35,18 @@ reveal_type(Identity()[0]) # revealed: int
```py
flag = True
class Identity:
if flag:
def __getitem__(self, index: int) -> int:
return index
else:
def __getitem__(self, index: int) -> str:
return str(index)
reveal_type(Identity()[0]) # revealed: int | str
```

View File

@@ -3,7 +3,7 @@
## Simple
```py
s = 'abcde'
s = "abcde"
reveal_type(s[0]) # revealed: Literal["a"]
reveal_type(s[1]) # revealed: Literal["b"]
@@ -14,7 +14,7 @@ a = s[8] # error: [index-out-of-bounds] "Index 8 is out of bounds for string `L
reveal_type(a) # revealed: Unknown
b = s[-8] # error: [index-out-of-bounds] "Index -8 is out of bounds for string `Literal["abcde"]` with length 5"
reveal_type(b) # revealed: Unknown
reveal_type(b) # revealed: Unknown
```
## Function return
@@ -23,7 +23,8 @@ reveal_type(b) # revealed: Unknown
def add(x: int, y: int) -> int:
return x + y
a = 'abcde'[add(0, 1)]
a = "abcde"[add(0, 1)]
# TODO: Support overloads... Should be `str`
reveal_type(a) # revealed: @Todo
```

View File

@@ -3,7 +3,7 @@
## Basic
```py
t = (1, 'a', 'b')
t = (1, "a", "b")
reveal_type(t[0]) # revealed: Literal[1]
reveal_type(t[1]) # revealed: Literal["a"]