Skip to content

Commit

Permalink
[red-knot] Format mdtest Python snippets more concisely (#13905)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Oct 24, 2024
1 parent 77ae0cc commit 3eb4546
Show file tree
Hide file tree
Showing 33 changed files with 4 additions and 193 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ repos:
)$
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.19.0
rev: 1.19.1
hooks:
- id: blacken-docs
args: ["--line-length", "130"]
args: ["--pyi", "--line-length", "130"]
files: '^crates/.*/resources/mdtest/.*\.md'
additional_dependencies:
- black==24.10.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ Name lookups within a class scope fall back to globals, but lookups of class att
```py
x = 1


class C:
y = x
if flag:
x = 2


reveal_type(C.x) # revealed: Literal[2]
reveal_type(C.y) # revealed: Literal[1]
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

```py
if flag:

class C:
x = 1

else:

class C:
x = 2


reveal_type(C.x) # revealed: Literal[1, 2]
```
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ class A:
def __or__(self, other) -> A:
return self


class B: ...


reveal_type(A() + B()) # revealed: A
reveal_type(A() - B()) # revealed: A
reveal_type(A() * B()) # revealed: A
Expand Down Expand Up @@ -117,10 +115,8 @@ class A:
def __ror__(self, other) -> A:
return self


class B: ...


reveal_type(B() + A()) # revealed: A
reveal_type(B() - A()) # revealed: A
reveal_type(B() * A()) # revealed: A
Expand Down Expand Up @@ -148,10 +144,8 @@ class A:
def __rsub__(self, other) -> int:
return 1


class B: ...


reveal_type(A() + B()) # revealed: int
reveal_type(B() - A()) # revealed: int
```
Expand All @@ -167,15 +161,12 @@ class A:
def __add__(self, other: B) -> int:
return 42


class B:
def __radd__(self, other: A) -> str:
return "foo"


reveal_type(A() + B()) # revealed: int


# Edge case: C is a subtype of C, *but* if the two sides are of *equal* types,
# the lhs *still* takes precedence
class C:
Expand All @@ -185,7 +176,6 @@ class C:
def __radd__(self, other: C) -> str:
return "foo"


reveal_type(C() + C()) # revealed: int
```

Expand All @@ -203,22 +193,17 @@ class A:
def __radd__(self, other) -> str:
return "foo"


class MyString(str): ...


class B(A):
def __radd__(self, other) -> MyString:
return MyString()


reveal_type(A() + B()) # revealed: MyString


# N.B. Still a subtype of `A`, even though `A` does not appear directly in the class's `__bases__`
class C(B): ...


# TODO: we currently only understand direct subclasses as subtypes of the superclass.
# We need to iterate through the full MRO rather than just the class's bases;
# if we do, we'll understand `C` as a subtype of `A`, and correctly understand this as being
Expand All @@ -240,10 +225,8 @@ class A:
def __radd__(self, other) -> int:
return 42


class B(A): ...


reveal_type(A() + B()) # revealed: str
```

Expand All @@ -266,12 +249,10 @@ class A:
def __sub__(self, other: A) -> A:
return A()


class B:
def __rsub__(self, other: A) -> B:
return B()


# TODO: this should be `B` (the return annotation of `B.__rsub__`),
# because `A.__sub__` is annotated as only accepting `A`,
# but `B.__rsub__` will accept `A`.
Expand All @@ -287,11 +268,9 @@ class A:
def __call__(self, other) -> int:
return 42


class B:
__add__ = A()


reveal_type(B() + B()) # revealed: int
```

Expand All @@ -311,15 +290,12 @@ reveal_type(42 + 4.2) # revealed: int
# TODO should be complex, need to check arg type and fall back to `rhs.__radd__`
reveal_type(3 + 3j) # revealed: int


def returns_int() -> int:
return 42


def returns_bool() -> bool:
return True


x = returns_bool()
y = returns_int()

Expand All @@ -343,7 +319,6 @@ class A:
def __radd__(self, other) -> A:
return self


reveal_type(A() + 1) # revealed: A
# TODO should be `A` since `int.__add__` doesn't support `A` instances
reveal_type(1 + A()) # revealed: int
Expand Down Expand Up @@ -388,15 +363,12 @@ from does_not_exist import Foo # error: [unresolved-import]

reveal_type(Foo) # revealed: Unknown


class X:
def __add__(self, other: object) -> int:
return 42


class Y(Foo): ...


# TODO: Should be `int | Unknown`; see above discussion.
reveal_type(X() + Y()) # revealed: int
```
Expand All @@ -411,12 +383,10 @@ The magic method must exist on the class, not just on the instance:
def add_impl(self, other) -> int:
return 1


class A:
def __init__(self):
self.__add__ = add_impl


# error: [unsupported-operator] "Operator `+` is unsupported between objects of type `A` and `A`"
# revealed: Unknown
reveal_type(A() + A())
Expand All @@ -427,7 +397,6 @@ reveal_type(A() + A())
```py
class A: ...


# error: [unsupported-operator]
# revealed: Unknown
reveal_type(A() + A())
Expand All @@ -441,14 +410,11 @@ A left-hand dunder method doesn't apply for the right-hand operand, or vice vers
class A:
def __add__(self, other) -> int: ...


class B:
def __radd__(self, other) -> int: ...


class C: ...


# error: [unsupported-operator]
# revealed: Unknown
reveal_type(C() + A())
Expand All @@ -471,7 +437,6 @@ class Foo:
def __radd__(self, other: Foo) -> Foo:
return self


# error: [unsupported-operator]
# revealed: Unknown
reveal_type(Foo() + Foo())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ bool(1) / False
# revealed: float
reveal_type(1.0 / 0)


class MyInt(int): ...


# No error for a subclass of int
# revealed: float
reveal_type(MyInt(3) / 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ class Multiplier:
def __call__(self, number: float) -> float:
return number * self.factor


a = Multiplier(2.0)(3.0)
reveal_type(a) # revealed: float


class Unit: ...


b = Unit()(3.0) # error: "Object of type `Unit` is not callable"
reveal_type(b) # revealed: Unknown
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
```py
class Foo: ...


reveal_type(Foo()) # revealed: Foo
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
def get_int() -> int:
return 42


reveal_type(get_int()) # revealed: int
```

Expand All @@ -16,7 +15,6 @@ reveal_type(get_int()) # revealed: int
async def get_int_async() -> int:
return 42


# TODO: we don't yet support `types.CoroutineType`, should be generic `Coroutine[Any, Any, int]`
reveal_type(get_int_async()) # revealed: @Todo
```
Expand All @@ -26,20 +24,16 @@ reveal_type(get_int_async()) # revealed: @Todo
```py
from typing import Callable


def foo() -> int:
return 42


def decorator(func) -> Callable[[], int]:
return foo


@decorator
def bar() -> str:
return "bar"


# TODO: should reveal `int`, as the decorator replaces `bar` with `foo`
reveal_type(bar()) # revealed: @Todo
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ else:
def f() -> str:
return "foo"


reveal_type(f()) # revealed: int | str
```

Expand All @@ -27,7 +26,6 @@ if flag:
def f() -> int:
return 1


reveal_type(f()) # revealed: Unknown | int
```

Expand All @@ -43,7 +41,6 @@ else:
def f() -> int:
return 1


x = f() # error: "Object of type `Literal[1] | Literal[f]` is not callable (due to union element `Literal[1]`)"
reveal_type(x) # revealed: Unknown | int
```
Expand All @@ -62,7 +59,6 @@ else:
def f() -> int:
return 1


# error: "Object of type `Literal[1] | Literal["foo"] | Literal[f]` is not callable (due to union elements Literal[1], Literal["foo"])"
# revealed: Unknown | int
reveal_type(f())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ reveal_type(1 <= "" and 0 < 1) # revealed: @Todo | Literal[True]
# TODO: implement lookup of `__eq__` on typeshed `int` stub.
def int_instance() -> int: ...


reveal_type(1 == int_instance()) # revealed: @Todo
reveal_type(9 < int_instance()) # revealed: bool
reveal_type(int_instance() < int_instance()) # revealed: bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ Walking through examples:
```py
from __future__ import annotations


class A:
def __lt__(self, other) -> A: ...

class B:
def __lt__(self, other) -> B: ...

class C:
def __lt__(self, other) -> C: ...


x = A() < B() < C()
reveal_type(x) # revealed: A | B

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
```py
def str_instance() -> str: ...


reveal_type("abc" == "abc") # revealed: Literal[True]
reveal_type("ab_cd" <= "ab_ce") # revealed: Literal[True]
reveal_type("abc" in "ab cd") # revealed: Literal[False]
Expand Down
Loading

0 comments on commit 3eb4546

Please sign in to comment.