Skip to content

Commit 4e9c5ed

Browse files
mrkmndzilevkivskyi
authored andcommitted
[typing-extensions] Simple implementation for IntVar (python#631)
1 parent 252d72a commit 4e9c5ed

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src_py2/test_typing_extensions.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import types
99
from unittest import TestCase, main, skipUnless
1010

11-
from typing_extensions import NoReturn, ClassVar, Final, Literal, TypedDict
11+
from typing_extensions import NoReturn, ClassVar, Final, IntVar, Literal, TypedDict
1212
from typing_extensions import ContextManager, Counter, Deque, DefaultDict
1313
from typing_extensions import NewType, overload, Protocol, runtime
1414
import typing
@@ -158,6 +158,19 @@ def test_no_isinstance(self):
158158
issubclass(int, Final)
159159

160160

161+
class IntVarTests(BaseTestCase):
162+
def test_valid(self):
163+
T_ints = IntVar("T_ints")
164+
165+
def test_invalid(self):
166+
with self.assertRaises(TypeError):
167+
T_ints = IntVar("T_ints", int)
168+
with self.assertRaises(TypeError):
169+
T_ints = IntVar("T_ints", bound=int)
170+
with self.assertRaises(TypeError):
171+
T_ints = IntVar("T_ints", covariant=True)
172+
173+
161174
class LiteralTests(BaseTestCase):
162175
def test_basics(self):
163176
Literal[1]

src_py2/typing_extensions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
# One-off things.
2929
'final',
30+
'IntVar',
3031
'Literal',
3132
'NewType',
3233
'overload',
@@ -197,6 +198,10 @@ class Other(Leaf): # Error reported by type checker
197198
return f
198199

199200

201+
def IntVar(name):
202+
return TypeVar(name)
203+
204+
200205
class _LiteralMeta(TypingMeta):
201206
"""Metaclass for _Literal"""
202207

src_py3/test_typing_extensions.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import Generic
1414
from typing import get_type_hints
1515
from typing import no_type_check
16-
from typing_extensions import NoReturn, ClassVar, Final, Literal, Type, NewType, TypedDict
16+
from typing_extensions import NoReturn, ClassVar, Final, IntVar, Literal, Type, NewType, TypedDict
1717
try:
1818
from typing_extensions import Protocol, runtime
1919
except ImportError:
@@ -214,6 +214,20 @@ def test_no_isinstance(self):
214214
with self.assertRaises(TypeError):
215215
issubclass(int, Final)
216216

217+
218+
class IntVarTests(BaseTestCase):
219+
def test_valid(self):
220+
T_ints = IntVar("T_ints")
221+
222+
def test_invalid(self):
223+
with self.assertRaises(TypeError):
224+
T_ints = IntVar("T_ints", int)
225+
with self.assertRaises(TypeError):
226+
T_ints = IntVar("T_ints", bound=int)
227+
with self.assertRaises(TypeError):
228+
T_ints = IntVar("T_ints", covariant=True)
229+
230+
217231
class LiteralTests(BaseTestCase):
218232
def test_basics(self):
219233
Literal[1]

src_py3/typing_extensions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def _check_methods_in_mro(C, *methods):
127127

128128
# One-off things.
129129
'final',
130+
'IntVar',
130131
'Literal',
131132
'NewType',
132133
'overload',
@@ -513,6 +514,10 @@ class Other(Leaf): # Error reported by type checker
513514
return f
514515

515516

517+
def IntVar(name):
518+
return TypeVar(name)
519+
520+
516521
if hasattr(typing, 'Literal'):
517522
Literal = typing.Literal
518523
elif sys.version_info[:2] >= (3, 7):

0 commit comments

Comments
 (0)