Skip to content

Commit 404b33b

Browse files
authored
Fix tests on 3.15 (#760)
1 parent 7ce55df commit 404b33b

2 files changed

Lines changed: 47 additions & 22 deletions

File tree

src/test_typing_extensions.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@
134134

135135
TYPING_3_14_0 = sys.version_info[:3] >= (3, 14, 0)
136136

137+
TYPING_3_15_0 = sys.version_info[:3] >= (3, 15, 0)
138+
139+
TYPING_3_15_0_BETA_1 = sys.version_info[:5] == (3, 15, 0, 'beta', 1)
140+
137141
# https://github.com/python/cpython/pull/27017 was backported into some 3.9 and 3.10
138142
# versions, but not all
139143
HAS_FORWARD_MODULE = "module" in inspect.signature(typing._type_check).parameters
@@ -1761,8 +1765,6 @@ def test_annotation_and_optional_default(self):
17611765
Optional[annotation] : Optional[annotation],
17621766
Union[str, None, str] : Optional[str],
17631767
Unpack[Tuple[int, None]]: Unpack[Tuple[int, None]],
1764-
# Note: A starred *Ts will use typing.Unpack in 3.11+ see Issue #485
1765-
Unpack[Ts] : Unpack[Ts],
17661768
}
17671769
# contains a ForwardRef, TypeVar(~prefix) or no expression
17681770
do_not_stringify_cases = {
@@ -1779,6 +1781,12 @@ def test_annotation_and_optional_default(self):
17791781
Union["annotation", T_default] : Union[annotation, T_default],
17801782
Annotated["annotation", "nested"] : Annotated[Union[int, None], "data", "nested"],
17811783
}
1784+
# Note: A starred *Ts will use typing.Unpack in 3.11+ see Issue #485
1785+
if TYPING_3_15_0:
1786+
# The repr is typing.Unpack[~Ts], which cannot be evaluated.
1787+
do_not_stringify_cases[Unpack[Ts]] = Unpack[Ts]
1788+
else:
1789+
cases[Unpack[Ts]] = Unpack[Ts]
17821790
if TYPING_3_10_0: # cannot construct UnionTypes before 3.10
17831791
do_not_stringify_cases["str | NoneAlias | StrAlias"] = str | None
17841792
cases[str | None] = Optional[str]
@@ -6599,10 +6607,16 @@ def test_basic_plain(self):
65996607
with self.assertRaises(TypeError):
66006608
Unpack()
66016609

6610+
@skipIf(TYPING_3_15_0, "repr changed in 3.15")
66026611
def test_repr(self):
66036612
Ts = TypeVarTuple('Ts')
66046613
self.assertEqual(repr(Unpack[Ts]), f'{Unpack.__module__}.Unpack[Ts]')
66056614

6615+
@skipUnless(TYPING_3_15_0, "repr changed in 3.15")
6616+
def test_repr_py315(self):
6617+
Ts = TypeVarTuple('Ts')
6618+
self.assertEqual(repr(Unpack[Ts]), f'{Unpack.__module__}.Unpack[~Ts]')
6619+
66066620
def test_cannot_subclass_vars(self):
66076621
with self.assertRaises(TypeError):
66086622
class V(Unpack[TypeVarTuple('Ts')]):
@@ -6797,10 +6811,16 @@ def test_basic_plain(self):
67976811
Ys = TypeVarTuple('Ys')
67986812
self.assertNotEqual(Xs, Ys)
67996813

6814+
@skipIf(TYPING_3_15_0, "repr changed in 3.15")
68006815
def test_repr(self):
68016816
Ts = TypeVarTuple('Ts')
68026817
self.assertEqual(repr(Ts), 'Ts')
68036818

6819+
@skipUnless(TYPING_3_15_0, "repr changed in 3.15")
6820+
def test_repr_py315(self):
6821+
Ts = TypeVarTuple('Ts')
6822+
self.assertEqual(repr(Ts), '~Ts')
6823+
68046824
def test_no_redefinition(self):
68056825
self.assertNotEqual(TypeVarTuple('Ts'), TypeVarTuple('Ts'))
68066826

@@ -7973,7 +7993,6 @@ def test_doc(self):
79737993
self.assertIsInstance(self.sentinel_type.__doc__, str)
79747994

79757995
def test_constructor(self):
7976-
self.assertIs(self.sentinel_type, type(self.sentinel_type)())
79777996
with self.assertRaises(TypeError):
79787997
type(self.sentinel_type)(1)
79797998

@@ -8009,12 +8028,13 @@ def test_repr(self):
80098028
class NoExtraItemsTests(SentinelTestsMixin, BaseTestCase):
80108029
sentinel_type = NoExtraItems
80118030

8031+
@skipIf(TYPING_3_15_0, "repr changed in 3.15")
80128032
def test_repr(self):
8013-
if hasattr(typing, 'NoExtraItems'):
8014-
mod_name = 'typing'
8015-
else:
8016-
mod_name = "typing_extensions"
8017-
self.assertEqual(repr(NoExtraItems), f"{mod_name}.NoExtraItems")
8033+
self.assertEqual(repr(NoExtraItems), "typing_extensions.NoExtraItems")
8034+
8035+
@skipUnless(TYPING_3_15_0, "repr changed in 3.15")
8036+
def test_repr_py315(self):
8037+
self.assertEqual(repr(NoExtraItems), "NoExtraItems")
80188038

80198039

80208040
class TypeVarInferVarianceTests(BaseTestCase):
@@ -9537,11 +9557,11 @@ def test_name_lookup_without_eval(self):
95379557
float,
95389558
)
95399559
self.assertIs(evaluate_forward_ref(typing.ForwardRef("int"), globals={"int": str}), str)
9560+
95409561
import builtins
95419562

9542-
from test import support
9543-
with support.swap_attr(builtins, "int", dict):
9544-
self.assertIs(evaluate_forward_ref(typing.ForwardRef("int")), dict)
9563+
with patch.object(builtins, "int", dict):
9564+
self.assertIs(evaluate_forward_ref(typing.ForwardRef("int")), dict)
95459565

95469566
def test_nested_strings(self):
95479567
# This variable must have a different name TypeVar
@@ -9599,10 +9619,9 @@ def test_sentinel_no_repr(self):
95999619
self.assertEqual(sentinel_no_repr.__name__, 'sentinel_no_repr')
96009620
self.assertEqual(repr(sentinel_no_repr), 'sentinel_no_repr')
96019621

9602-
def test_sentinel_deprecated_explicit_repr(self):
9603-
with self.assertWarnsRegex(DeprecationWarning, r"'repr' parameter is deprecated and will be removed"):
9604-
sentinel_explicit_repr = sentinel('sentinel_explicit_repr', repr='explicit_repr')
9605-
9622+
@skipIf(TYPING_3_15_0_BETA_1, reason="'repr' parameter is not yet available in 3.15.0b1")
9623+
def test_sentinel_explicit_repr(self):
9624+
sentinel_explicit_repr = sentinel('sentinel_explicit_repr', repr='explicit_repr')
96069625
self.assertEqual(repr(sentinel_explicit_repr), 'explicit_repr')
96079626

96089627
@skipIf(sys.version_info < (3, 10), reason='New unions not available in 3.9')
@@ -9644,6 +9663,7 @@ def test_sentinel_picklable_anonymous(self):
96449663
):
96459664
self.assertIs(anonymous_sentinel, pickle.loads(pickle.dumps(anonymous_sentinel, protocol=proto)))
96469665

9666+
@skipIf(TYPING_3_15_0, reason='Deprecated sentinel APIs were removed in 3.15')
96479667
def test_sentinel_deprecated(self):
96489668
with self.assertWarnsRegex(DeprecationWarning, r"Subclassing sentinel is deprecated"):
96499669
class SentinelSubclass(Sentinel):
@@ -9656,6 +9676,18 @@ class SentinelSubclass(Sentinel):
96569676
with self.assertWarnsRegex(DeprecationWarning, r"Setting attribute 'foo' on sentinel objects is deprecated"):
96579677
my_sentinel.foo = "bar"
96589678

9679+
@skipUnless(TYPING_3_15_0, reason='Deprecated sentinel APIs are available before 3.15')
9680+
def test_sentinel_removed_deprecated_apis(self):
9681+
with self.assertRaises(TypeError):
9682+
class SentinelSubclass(Sentinel):
9683+
pass
9684+
with self.assertRaises(TypeError):
9685+
sentinel()
9686+
with self.assertRaises(TypeError):
9687+
Sentinel(name="my_sentinel")
9688+
with self.assertRaises(AttributeError):
9689+
sentinel('my_sentinel').foo = "bar"
9690+
96599691

96609692
def load_tests(loader, tests, pattern):
96619693
import doctest

src/typing_extensions.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,6 @@ def __init__(
204204
__name = name
205205
if __name is _sentinel_placeholder:
206206
raise TypeError("First parameter 'name' is required")
207-
if repr is not None:
208-
warnings.warn(
209-
"The 'repr' parameter is deprecated "
210-
"and will be removed in Python 3.15.",
211-
DeprecationWarning,
212-
stacklevel=2,
213-
)
214207

215208
self.__name__ = __name
216209
self._repr = repr if repr is not None else __name

0 commit comments

Comments
 (0)