@@ -2471,8 +2471,9 @@ class Other(Leaf): # Error reported by type checker
2471
2471
return f
2472
2472
2473
2473
2474
- # Some unconstrained type variables. These are used by the container types.
2475
- # (These are not for export.)
2474
+ # Some unconstrained type variables. These were initially used by the container types.
2475
+ # They were never meant for export and are now unused, but we keep them around to
2476
+ # avoid breaking compatibility with users who import them.
2476
2477
T = TypeVar ('T' ) # Any type.
2477
2478
KT = TypeVar ('KT' ) # Key type.
2478
2479
VT = TypeVar ('VT' ) # Value type.
@@ -2577,8 +2578,6 @@ def new_user(user_class: Type[U]) -> U:
2577
2578
At this point the type checker knows that joe has type BasicUser.
2578
2579
"""
2579
2580
2580
- # Internal type variable for callables. Not for export.
2581
- F = TypeVar ("F" , bound = Callable [..., Any ])
2582
2581
2583
2582
@runtime_checkable
2584
2583
class SupportsInt (Protocol ):
@@ -2631,22 +2630,22 @@ def __index__(self) -> int:
2631
2630
2632
2631
2633
2632
@runtime_checkable
2634
- class SupportsAbs ( Protocol [ T_co ] ):
2633
+ class SupportsAbs [ T ]( Protocol ):
2635
2634
"""An ABC with one abstract method __abs__ that is covariant in its return type."""
2636
2635
__slots__ = ()
2637
2636
2638
2637
@abstractmethod
2639
- def __abs__ (self ) -> T_co :
2638
+ def __abs__ (self ) -> T :
2640
2639
pass
2641
2640
2642
2641
2643
2642
@runtime_checkable
2644
- class SupportsRound ( Protocol [ T_co ] ):
2643
+ class SupportsRound [ T ]( Protocol ):
2645
2644
"""An ABC with one abstract method __round__ that is covariant in its return type."""
2646
2645
__slots__ = ()
2647
2646
2648
2647
@abstractmethod
2649
- def __round__ (self , ndigits : int = 0 ) -> T_co :
2648
+ def __round__ (self , ndigits : int = 0 ) -> T :
2650
2649
pass
2651
2650
2652
2651
@@ -3183,7 +3182,7 @@ class re(metaclass=_DeprecatedType):
3183
3182
sys .modules [re .__name__ ] = re
3184
3183
3185
3184
3186
- def reveal_type (obj : T , / ) -> T :
3185
+ def reveal_type [ T ] (obj : T , / ) -> T :
3187
3186
"""Reveal the inferred type of a variable.
3188
3187
3189
3188
When a static type checker encounters a call to ``reveal_type()``,
@@ -3203,6 +3202,11 @@ def reveal_type(obj: T, /) -> T:
3203
3202
return obj
3204
3203
3205
3204
3205
+ class _IdentityCallable (Protocol ):
3206
+ def __call__ [T ](self , arg : T , / ) -> T :
3207
+ ...
3208
+
3209
+
3206
3210
def dataclass_transform (
3207
3211
* ,
3208
3212
eq_default : bool = True ,
@@ -3211,7 +3215,7 @@ def dataclass_transform(
3211
3215
frozen_default : bool = False ,
3212
3216
field_specifiers : tuple [type [Any ] | Callable [..., Any ], ...] = (),
3213
3217
** kwargs : Any ,
3214
- ) -> Callable [[ T ], T ] :
3218
+ ) -> _IdentityCallable :
3215
3219
"""Decorator that marks a function, class, or metaclass as providing
3216
3220
dataclass-like behavior.
3217
3221
@@ -3288,8 +3292,10 @@ def decorator(cls_or_fn):
3288
3292
return decorator
3289
3293
3290
3294
3295
+ type _Func = Callable [..., Any ]
3296
+
3291
3297
3292
- def override (method : F , / ) -> F :
3298
+ def override [ F : _Func ] (method : F , / ) -> F :
3293
3299
"""Indicate that a method is intended to override a method in a base class.
3294
3300
3295
3301
Usage:
0 commit comments