Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,39 @@ reveal_type(C(1, True)) # revealed: C[Literal[1]]
wrong_innards: C[int] = C("five", 1)
```

### Some `__init__` overloads only apply to certain specializations

```py
from typing import overload, Generic, TypeVar

T = TypeVar("T")

class C(Generic[T]):
@overload
def __init__(self: "C[str]", x: str) -> None: ...
@overload
def __init__(self: "C[bytes]", x: bytes) -> None: ...
@overload
def __init__(self, x: int) -> None: ...
def __init__(self, x: str | bytes | int) -> None: ...

reveal_type(C("string")) # revealed: C[str]
reveal_type(C(b"bytes")) # revealed: C[bytes]
reveal_type(C(12)) # revealed: C[Unknown]

C[str]("string")
C[str](b"bytes") # error: [no-matching-overload]
C[str](12)

C[bytes]("string") # error: [no-matching-overload]
C[bytes](b"bytes")
C[bytes](12)

C[None]("string") # error: [no-matching-overload]
C[None](b"bytes") # error: [no-matching-overload]
C[None](12)
```

## Generic subclass

When a generic subclass fills its superclass's type parameter with one of its own, the actual types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,37 @@ reveal_type(C(1, True)) # revealed: C[Literal[1]]
wrong_innards: C[int] = C("five", 1)
```

### Some `__init__` overloads only apply to certain specializations

```py
from typing import overload

class C[T]:
@overload
def __init__(self: C[str], x: str) -> None: ...
@overload
def __init__(self: C[bytes], x: bytes) -> None: ...
@overload
def __init__(self, x: int) -> None: ...
def __init__(self, x: str | bytes | int) -> None: ...

reveal_type(C("string")) # revealed: C[str]
reveal_type(C(b"bytes")) # revealed: C[bytes]
reveal_type(C(12)) # revealed: C[Unknown]

C[str]("string")
C[str](b"bytes") # error: [no-matching-overload]
C[str](12)

C[bytes]("string") # error: [no-matching-overload]
C[bytes](b"bytes")
C[bytes](12)

C[None]("string") # error: [no-matching-overload]
C[None](b"bytes") # error: [no-matching-overload]
C[None](12)
```

## Generic subclass

When a generic subclass fills its superclass's type parameter with one of its own, the actual types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class C[T]:
return "a"

reveal_type(getattr_static(C[int], "f")) # revealed: def f(self, x: int) -> str
reveal_type(getattr_static(C[int], "f").__get__) # revealed: <method-wrapper `__get__` of `f[int]`>
reveal_type(getattr_static(C[int], "f").__get__) # revealed: <method-wrapper `__get__` of `f`>
reveal_type(getattr_static(C[int], "f").__get__(None, C[int])) # revealed: def f(self, x: int) -> str
# revealed: bound method C[int].f(x: int) -> str
reveal_type(getattr_static(C[int], "f").__get__(C[int](), C[int]))
Expand Down
Loading
Loading