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 @@ -301,13 +301,19 @@ consistent with each other.

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")

class C(Generic[T]):
def __new__(cls, x: T) -> "C[T]":
return object.__new__(cls)

# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(into_callable(C)))

reveal_type(C(1)) # revealed: C[int]

# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
Expand All @@ -318,12 +324,18 @@ wrong_innards: C[int] = C("five")

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")

class C(Generic[T]):
def __init__(self, x: T) -> None: ...

# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(into_callable(C)))

reveal_type(C(1)) # revealed: C[int]

# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
Expand All @@ -334,6 +346,7 @@ wrong_innards: C[int] = C("five")

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")

Expand All @@ -343,6 +356,11 @@ class C(Generic[T]):

def __init__(self, x: T) -> None: ...

# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(into_callable(C)))

reveal_type(C(1)) # revealed: C[int]

# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
Expand All @@ -353,6 +371,7 @@ wrong_innards: C[int] = C("five")

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")

Expand All @@ -362,6 +381,11 @@ class C(Generic[T]):

def __init__(self, x: T) -> None: ...

# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(into_callable(C)))

reveal_type(C(1)) # revealed: C[int]

# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
Expand All @@ -373,6 +397,11 @@ class D(Generic[T]):

def __init__(self, *args, **kwargs) -> None: ...

# revealed: ty_extensions.GenericContext[T@D]
reveal_type(generic_context(D))
# revealed: ty_extensions.GenericContext[T@D]
reveal_type(generic_context(into_callable(D)))

reveal_type(D(1)) # revealed: D[int]

# error: [invalid-assignment] "Object of type `D[int | str]` is not assignable to `D[int]`"
Expand All @@ -386,6 +415,7 @@ to specialize the class.

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")
U = TypeVar("U")
Expand All @@ -398,13 +428,19 @@ class C(Generic[T, U]):
class D(C[V, int]):
def __init__(self, x: V) -> None: ...

# revealed: ty_extensions.GenericContext[V@D]
reveal_type(generic_context(D))
# revealed: ty_extensions.GenericContext[V@D]
reveal_type(generic_context(into_callable(D)))

reveal_type(D(1)) # revealed: D[int]
```

### Generic class inherits `__init__` from generic base class

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")
U = TypeVar("U")
Expand All @@ -415,6 +451,11 @@ class C(Generic[T, U]):
class D(C[T, U]):
pass

# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(D))
# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(into_callable(D)))

reveal_type(C(1, "str")) # revealed: C[int, str]
reveal_type(D(1, "str")) # revealed: D[int, str]
```
Expand All @@ -425,13 +466,19 @@ This is a specific example of the above, since it was reported specifically by a

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")
U = TypeVar("U")

class D(dict[T, U]):
pass

# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(D))
# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(into_callable(D)))

reveal_type(D(key=1)) # revealed: D[str, int]
```

Expand All @@ -443,12 +490,18 @@ context. But from the user's point of view, this is another example of the above

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")
U = TypeVar("U")

class C(tuple[T, U]): ...

# revealed: ty_extensions.GenericContext[T@C, U@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C, U@C]
reveal_type(generic_context(into_callable(C)))

reveal_type(C((1, 2))) # revealed: C[int, int]
```

Expand Down Expand Up @@ -480,13 +533,19 @@ def func8(t1: tuple[complex, list[int]], t2: tuple[int, *tuple[str, ...]], t3: t

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

S = TypeVar("S")
T = TypeVar("T")

class C(Generic[T]):
def __init__(self, x: T, y: S) -> None: ...

# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C, S@__init__]
reveal_type(generic_context(into_callable(C)))

reveal_type(C(1, 1)) # revealed: C[int]
reveal_type(C(1, "string")) # revealed: C[int]
reveal_type(C(1, True)) # revealed: C[int]
Expand All @@ -499,6 +558,7 @@ wrong_innards: C[int] = C("five", 1)

```py
from typing_extensions import overload, Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")
U = TypeVar("U")
Expand All @@ -514,6 +574,11 @@ class C(Generic[T]):
def __init__(self, x: int) -> None: ...
def __init__(self, x: str | bytes | int) -> None: ...

# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C]
reveal_type(generic_context(into_callable(C)))

reveal_type(C("string")) # revealed: C[str]
reveal_type(C(b"bytes")) # revealed: C[bytes]
reveal_type(C(12)) # revealed: C[Unknown]
Expand Down Expand Up @@ -541,6 +606,11 @@ class D(Generic[T, U]):
def __init__(self, t: T, u: U) -> None: ...
def __init__(self, *args) -> None: ...

# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(D))
# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(into_callable(D)))

reveal_type(D("string")) # revealed: D[str, str]
reveal_type(D(1)) # revealed: D[str, int]
reveal_type(D(1, "string")) # revealed: D[int, str]
Expand All @@ -551,31 +621,48 @@ reveal_type(D(1, "string")) # revealed: D[int, str]
```py
from dataclasses import dataclass
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")

@dataclass
class A(Generic[T]):
x: T

# revealed: ty_extensions.GenericContext[T@A]
reveal_type(generic_context(A))
# revealed: ty_extensions.GenericContext[T@A]
reveal_type(generic_context(into_callable(A)))

reveal_type(A(x=1)) # revealed: A[int]
```

### Class typevar has another typevar as a default

```py
from typing_extensions import Generic, TypeVar
from ty_extensions import generic_context, into_callable

T = TypeVar("T")
U = TypeVar("U", default=T)

class C(Generic[T, U]): ...

# revealed: ty_extensions.GenericContext[T@C, U@C]
reveal_type(generic_context(C))
# revealed: ty_extensions.GenericContext[T@C, U@C]
reveal_type(generic_context(into_callable(C)))

reveal_type(C()) # revealed: C[Unknown, Unknown]

class D(Generic[T, U]):
def __init__(self) -> None: ...

# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(D))
# revealed: ty_extensions.GenericContext[T@D, U@D]
reveal_type(generic_context(into_callable(D)))

reveal_type(D()) # revealed: D[Unknown, Unknown]
```

Expand Down
Loading
Loading