Skip to content

Commit d2b14f8

Browse files
AlexWaygoodShaneMurphy2
authored andcommitted
Make tests pass on conda builds (python#151)
1 parent 8bff0a3 commit d2b14f8

1 file changed

Lines changed: 153 additions & 25 deletions

File tree

src/test_typing_extensions.py

Lines changed: 153 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
import collections.abc
99
import copy
1010
from functools import lru_cache
11+
import importlib
1112
import inspect
1213
import pickle
1314
import subprocess
15+
import tempfile
1416
import types
17+
from pathlib import Path
1518
from unittest import TestCase, main, skipUnless, skipIf
1619
from unittest.mock import patch
17-
from test import ann_module, ann_module2, ann_module3
1820
import typing
1921
from typing import TypeVar, Optional, Union, AnyStr
2022
from typing import T, KT, VT # Not in __all__.
2123
from typing import Tuple, List, Dict, Iterable, Iterator, Callable
2224
from typing import Generic
2325
from typing import no_type_check
26+
import warnings
27+
2428
import typing_extensions
2529
from typing_extensions import NoReturn, Any, ClassVar, Final, IntVar, Literal, Type, NewType, TypedDict, Self
2630
from typing_extensions import TypeAlias, ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs, TypeGuard
@@ -32,7 +36,6 @@
3236
from typing_extensions import NamedTuple
3337
from typing_extensions import override, deprecated, Buffer
3438
from _typed_dict_test_helper import Foo, FooGeneric
35-
import warnings
3639

3740
# Flags used to mark tests that only apply after a specific
3841
# version of the typing module.
@@ -47,6 +50,112 @@
4750
# versions, but not all
4851
HAS_FORWARD_MODULE = "module" in inspect.signature(typing._type_check).parameters
4952

53+
ANN_MODULE_SOURCE = '''\
54+
from typing import Optional
55+
from functools import wraps
56+
57+
__annotations__[1] = 2
58+
59+
class C:
60+
61+
x = 5; y: Optional['C'] = None
62+
63+
from typing import Tuple
64+
x: int = 5; y: str = x; f: Tuple[int, int]
65+
66+
class M(type):
67+
68+
__annotations__['123'] = 123
69+
o: type = object
70+
71+
(pars): bool = True
72+
73+
class D(C):
74+
j: str = 'hi'; k: str= 'bye'
75+
76+
from types import new_class
77+
h_class = new_class('H', (C,))
78+
j_class = new_class('J')
79+
80+
class F():
81+
z: int = 5
82+
def __init__(self, x):
83+
pass
84+
85+
class Y(F):
86+
def __init__(self):
87+
super(F, self).__init__(123)
88+
89+
class Meta(type):
90+
def __new__(meta, name, bases, namespace):
91+
return super().__new__(meta, name, bases, namespace)
92+
93+
class S(metaclass = Meta):
94+
x: str = 'something'
95+
y: str = 'something else'
96+
97+
def foo(x: int = 10):
98+
def bar(y: List[str]):
99+
x: str = 'yes'
100+
bar()
101+
102+
def dec(func):
103+
@wraps(func)
104+
def wrapper(*args, **kwargs):
105+
return func(*args, **kwargs)
106+
return wrapper
107+
'''
108+
109+
ANN_MODULE_2_SOURCE = '''\
110+
from typing import no_type_check, ClassVar
111+
112+
i: int = 1
113+
j: int
114+
x: float = i/10
115+
116+
def f():
117+
class C: ...
118+
return C()
119+
120+
f().new_attr: object = object()
121+
122+
class C:
123+
def __init__(self, x: int) -> None:
124+
self.x = x
125+
126+
c = C(5)
127+
c.new_attr: int = 10
128+
129+
__annotations__ = {}
130+
131+
132+
@no_type_check
133+
class NTC:
134+
def meth(self, param: complex) -> None:
135+
...
136+
137+
class CV:
138+
var: ClassVar['CV']
139+
140+
CV.var = CV()
141+
'''
142+
143+
ANN_MODULE_3_SOURCE = '''\
144+
def f_bad_ann():
145+
__annotations__[1] = 2
146+
147+
class C_OK:
148+
def __init__(self, x: int) -> None:
149+
self.x: no_such_name = x # This one is OK as proposed by Guido
150+
151+
class D_bad_ann:
152+
def __init__(self, x: int) -> None:
153+
sfel.y: int = 0
154+
155+
def g_bad_ann():
156+
no_such_name.attr: int = 0
157+
'''
158+
50159

51160
class BaseTestCase(TestCase):
52161
def assertIsSubclass(self, cls, class_or_tuple, msg=None):
@@ -384,8 +493,13 @@ def test_repr(self):
384493
else:
385494
mod_name = 'typing_extensions'
386495
self.assertEqual(repr(Any), f"{mod_name}.Any")
387-
if sys.version_info < (3, 11): # skip for now on 3.11+ see python/cpython#95987
388-
self.assertEqual(repr(self.SubclassesAny), "<class 'test_typing_extensions.AnyTests.SubclassesAny'>")
496+
497+
@skipIf(sys.version_info[:3] == (3, 11, 0), "A bug was fixed in 3.11.1")
498+
def test_repr_on_Any_subclass(self):
499+
self.assertEqual(
500+
repr(self.SubclassesAny),
501+
f"<class '{self.SubclassesAny.__module__}.AnyTests.SubclassesAny'>"
502+
)
389503

390504
def test_instantiation(self):
391505
with self.assertRaises(TypeError):
@@ -942,28 +1056,42 @@ class AnnotatedMovie(TypedDict):
9421056

9431057

9441058
class GetTypeHintTests(BaseTestCase):
1059+
@classmethod
1060+
def setUpClass(cls):
1061+
with tempfile.TemporaryDirectory() as tempdir:
1062+
sys.path.append(tempdir)
1063+
Path(tempdir, "ann_module.py").write_text(ANN_MODULE_SOURCE)
1064+
Path(tempdir, "ann_module2.py").write_text(ANN_MODULE_2_SOURCE)
1065+
Path(tempdir, "ann_module3.py").write_text(ANN_MODULE_3_SOURCE)
1066+
cls.ann_module = importlib.import_module("ann_module")
1067+
cls.ann_module2 = importlib.import_module("ann_module2")
1068+
cls.ann_module3 = importlib.import_module("ann_module3")
1069+
sys.path.pop()
1070+
1071+
@classmethod
1072+
def tearDownClass(cls):
1073+
for modname in "ann_module", "ann_module2", "ann_module3":
1074+
delattr(cls, modname)
1075+
del sys.modules[modname]
1076+
9451077
def test_get_type_hints_modules(self):
9461078
ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str}
947-
if (TYPING_3_11_0
948-
or (TYPING_3_10_0 and sys.version_info.releaselevel in {'candidate', 'final'})):
949-
# More tests were added in 3.10rc1.
950-
ann_module_type_hints['u'] = int | float
951-
self.assertEqual(gth(ann_module), ann_module_type_hints)
952-
self.assertEqual(gth(ann_module2), {})
953-
self.assertEqual(gth(ann_module3), {})
1079+
self.assertEqual(gth(self.ann_module), ann_module_type_hints)
1080+
self.assertEqual(gth(self.ann_module2), {})
1081+
self.assertEqual(gth(self.ann_module3), {})
9541082

9551083
def test_get_type_hints_classes(self):
956-
self.assertEqual(gth(ann_module.C, ann_module.__dict__),
957-
{'y': Optional[ann_module.C]})
958-
self.assertIsInstance(gth(ann_module.j_class), dict)
959-
self.assertEqual(gth(ann_module.M), {'123': 123, 'o': type})
960-
self.assertEqual(gth(ann_module.D),
961-
{'j': str, 'k': str, 'y': Optional[ann_module.C]})
962-
self.assertEqual(gth(ann_module.Y), {'z': int})
963-
self.assertEqual(gth(ann_module.h_class),
964-
{'y': Optional[ann_module.C]})
965-
self.assertEqual(gth(ann_module.S), {'x': str, 'y': str})
966-
self.assertEqual(gth(ann_module.foo), {'x': int})
1084+
self.assertEqual(gth(self.ann_module.C, self.ann_module.__dict__),
1085+
{'y': Optional[self.ann_module.C]})
1086+
self.assertIsInstance(gth(self.ann_module.j_class), dict)
1087+
self.assertEqual(gth(self.ann_module.M), {'123': 123, 'o': type})
1088+
self.assertEqual(gth(self.ann_module.D),
1089+
{'j': str, 'k': str, 'y': Optional[self.ann_module.C]})
1090+
self.assertEqual(gth(self.ann_module.Y), {'z': int})
1091+
self.assertEqual(gth(self.ann_module.h_class),
1092+
{'y': Optional[self.ann_module.C]})
1093+
self.assertEqual(gth(self.ann_module.S), {'x': str, 'y': str})
1094+
self.assertEqual(gth(self.ann_module.foo), {'x': int})
9671095
self.assertEqual(gth(NoneAndForward, globals()),
9681096
{'parent': NoneAndForward, 'meaning': type(None)})
9691097

@@ -974,16 +1102,16 @@ class Inn:
9741102
def __init__(self, x: 'not a type'): ...
9751103
self.assertTrue(NoTpCheck.__no_type_check__)
9761104
self.assertTrue(NoTpCheck.Inn.__init__.__no_type_check__)
977-
self.assertEqual(gth(ann_module2.NTC.meth), {})
1105+
self.assertEqual(gth(self.ann_module2.NTC.meth), {})
9781106
class ABase(Generic[T]):
9791107
def meth(x: int): ...
9801108
@no_type_check
9811109
class Der(ABase): ...
9821110
self.assertEqual(gth(ABase.meth), {'x': int})
9831111

9841112
def test_get_type_hints_ClassVar(self):
985-
self.assertEqual(gth(ann_module2.CV, ann_module2.__dict__),
986-
{'var': ClassVar[ann_module2.CV]})
1113+
self.assertEqual(gth(self.ann_module2.CV, self.ann_module2.__dict__),
1114+
{'var': ClassVar[self.ann_module2.CV]})
9871115
self.assertEqual(gth(B, globals()),
9881116
{'y': int, 'x': ClassVar[Optional[B]], 'b': int})
9891117
self.assertEqual(gth(CSub, globals()),

0 commit comments

Comments
 (0)