Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeVar(infer_variance=True) #80

Merged
merged 1 commit into from
Oct 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Release 4.4.0 ()

- Add `typing_extensions.Any` a backport of python 3.11's Any class which is
subclassable at runtime. (backport from python/cpython#31841, by Shantanu
and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234).
- Add initial support for TypeVarLike `default` parameter, PEP 696.
Patch by Marc Mueller (@cdce8p).
- Add the `infer_variance` parameter to `TypeVar`, as specified in PEP 695.
Patch by Jelle Zijlstra.

# Release 4.3.0 (July 1, 2022)

Expand Down
23 changes: 23 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3449,5 +3449,28 @@ def test_pickle(self):
self.assertEqual(z.__default__, typevar.__default__)


class TypeVarInferVarianceTests(BaseTestCase):
def test_typevar(self):
T = typing_extensions.TypeVar('T')
self.assertFalse(T.__infer_variance__)
T_infer = typing_extensions.TypeVar('T_infer', infer_variance=True)
self.assertTrue(T_infer.__infer_variance__)
T_noinfer = typing_extensions.TypeVar('T_noinfer', infer_variance=False)
self.assertFalse(T_noinfer.__infer_variance__)

def test_pickle(self):
global U, U_infer # pickle wants to reference the class by name
U = typing_extensions.TypeVar('U')
U_infer = typing_extensions.TypeVar('U_infer', infer_variance=True)
for proto in range(pickle.HIGHEST_PROTOCOL):
for typevar in (U, U_infer):
z = pickle.loads(pickle.dumps(typevar, proto))
self.assertEqual(z.__name__, typevar.__name__)
self.assertEqual(z.__covariant__, typevar.__covariant__)
self.assertEqual(z.__contravariant__, typevar.__contravariant__)
self.assertEqual(z.__bound__, typevar.__bound__)
self.assertEqual(z.__infer_variance__, typevar.__infer_variance__)


if __name__ == '__main__':
main()
5 changes: 3 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,18 +1163,19 @@ def __init__(self, default):
self.__default__ = None


# Add default Parameter - PEP 696
# Add default and infer_variance parameters from PEP 696 and 695
class TypeVar(typing.TypeVar, _DefaultMixin, _root=True):
"""Type variable."""

__module__ = 'typing'

def __init__(self, name, *constraints, bound=None,
covariant=False, contravariant=False,
default=None):
default=None, infer_variance=False):
super().__init__(name, *constraints, bound=bound, covariant=covariant,
contravariant=contravariant)
_DefaultMixin.__init__(self, default)
self.__infer_variance__ = infer_variance

# for pickling:
try:
Expand Down