Skip to content

Commit b46366e

Browse files
Michael0x2aambv
authored andcommitted
Make all single-constraint TypeVars to use bounds
According to the documentation in the typing module, TypeVars cannot have only a single constraint. Attempting to do so will actually result in an exception at runtime. (However, this error is currently ignored by mypy -- see python/mypy#2626 for a related pending pull request). This commit changes all instances of TypeVars using a single constraint (e.g. `T = TypeVar('T', Foo)`) to use bounds instead (e.g. `T = TypeVar('T', bound=Foo)`. This seems to be the correct fix for plistlib after reading the module docs, but it's less obvious this is correct for unittest. The unittest module originally had `_FT = TypeVar('_FT', Callable[[Any], Any])` -- an alternative fix would have been to do `_FT = Callable[[Any], Any]`. Although I'm not entirely sure what it means to have a bound be a Callable, I decided to make the assumption that the original authors probably meant to use TypeVars instead of type aliases for a reason (possibly to handle classes implementing `__call__`?)
1 parent c34f11d commit b46366e

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

stdlib/2and3/plistlib.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from enum import Enum
99
import sys
1010

1111
mm = MutableMapping[str, Any]
12-
_D = TypeVar('_D', mm)
12+
_D = TypeVar('_D', bound=mm)
1313
if sys.version_info >= (3,):
1414
_Path = str
1515
else:

stdlib/3/unittest/__init__.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from contextlib import ContextManager
1212

1313

1414
_T = TypeVar('_T')
15-
_FT = TypeVar('_FT', Callable[[Any], Any])
15+
_FT = TypeVar('_FT', bound=Callable[[Any], Any])
1616

1717

1818
def skip(reason: str) -> Callable[[_FT], _FT]: ...

0 commit comments

Comments
 (0)