Skip to content

Fix most tests on Python 3.13 #378

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

Merged
merged 5 commits into from
May 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Unreleased

- Fix tests on Python 3.13.0a6 and newer. 3.13.0a6 adds a new
- Backport the `typing.NoDefault` sentinel object from Python 3.13.
TypeVars, ParamSpecs and TypeVarTuples without default values now have
their `__default__` attribute set to this sentinel value.
- TypeVars, ParamSpecs and TypeVarTuples now have a `has_default()`
method, matching `typing.TypeVar`, `typing.ParamSpec` and
`typing.TypeVarTuple` on Python 3.13+.
- TypeVars, ParamSpecs and TypeVarTuples with `default=None` passed to
their constructors now have their `__default__` attribute set to `None`
at runtime rather than `types.NoneType`.
- Fix most tests for `TypeVar`, `ParamSpec` and `TypeVarTuple` on Python
3.13.0b1 and newer.
- Fix `Protocol` tests on Python 3.13.0a6 and newer. 3.13.0a6 adds a new
`__static_attributes__` attribute to all classes in Python,
which broke some assumptions made by the implementation of
`typing_extensions.Protocol`. Similarly, 3.13.0b1 adds the new
Expand Down
56 changes: 52 additions & 4 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,19 @@ Special typing primitives

The improvements from Python 3.10 and 3.11 were backported.

.. data:: NoDefault

See :py:class:`typing.NoDefault`. In ``typing`` since 3.13.0.

.. versionadded:: 4.12.0

.. data:: NotRequired

See :py:data:`typing.NotRequired` and :pep:`655`. In ``typing`` since 3.11.

.. versionadded:: 4.0.0

.. class:: ParamSpec(name, *, default=...)
.. class:: ParamSpec(name, *, default=NoDefault)

See :py:class:`typing.ParamSpec` and :pep:`612`. In ``typing`` since 3.10.

Expand All @@ -284,6 +290,20 @@ Special typing primitives
Passing an ellipsis literal (``...``) to *default* now works on Python
3.10 and lower.

.. versionchanged:: 4.12.0

The :attr:`!__default__` attribute is now set to ``None`` if
``default=None`` is passed, and to :data:`NoDefault` if no value is passed.

Previously, passing ``None`` would result in :attr:`!__default__` being set
to :py:class:`types.NoneType`, and passing no value for the parameter would
result in :attr:`!__default__` being set to ``None``.

.. versionchanged:: 4.12.0

ParamSpecs now have a ``has_default()`` method, for compatibility
with :py:class:`typing.ParamSpec` on Python 3.13+.

.. class:: ParamSpecArgs

.. class:: ParamSpecKwargs
Expand Down Expand Up @@ -395,7 +415,7 @@ Special typing primitives
are mutable if they do not carry the :data:`ReadOnly` qualifier.

.. versionadded:: 4.9.0

The experimental ``closed`` keyword argument and the special key
``__extra_items__`` proposed in :pep:`728` are supported.

Expand Down Expand Up @@ -466,7 +486,7 @@ Special typing primitives
when ``closed=True`` is given were supported.

.. class:: TypeVar(name, *constraints, bound=None, covariant=False,
contravariant=False, infer_variance=False, default=...)
contravariant=False, infer_variance=False, default=NoDefault)

See :py:class:`typing.TypeVar`.

Expand All @@ -484,7 +504,21 @@ Special typing primitives

The implementation was changed for compatibility with Python 3.12.

.. class:: TypeVarTuple(name, *, default=...)
.. versionchanged:: 4.12.0

The :attr:`!__default__` attribute is now set to ``None`` if
``default=None`` is passed, and to :data:`NoDefault` if no value is passed.

Previously, passing ``None`` would result in :attr:`!__default__` being set
to :py:class:`types.NoneType`, and passing no value for the parameter would
result in :attr:`!__default__` being set to ``None``.

.. versionchanged:: 4.12.0

TypeVars now have a ``has_default()`` method, for compatibility
with :py:class:`typing.TypeVar` on Python 3.13+.

.. class:: TypeVarTuple(name, *, default=NoDefault)

See :py:class:`typing.TypeVarTuple` and :pep:`646`. In ``typing`` since 3.11.

Expand All @@ -501,6 +535,20 @@ Special typing primitives

The implementation was changed for compatibility with Python 3.12.

.. versionchanged:: 4.12.0

The :attr:`!__default__` attribute is now set to ``None`` if
``default=None`` is passed, and to :data:`NoDefault` if no value is passed.

Previously, passing ``None`` would result in :attr:`!__default__` being set
to :py:class:`types.NoneType`, and passing no value for the parameter would
result in :attr:`!__default__` being set to ``None``.

.. versionchanged:: 4.12.0

TypeVarTuples now have a ``has_default()`` method, for compatibility
with :py:class:`typing.TypeVarTuple` on Python 3.13+.

.. data:: Unpack

See :py:data:`typing.Unpack` and :pep:`646`. In ``typing`` since 3.11.
Expand Down
47 changes: 37 additions & 10 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from typing_extensions import clear_overloads, get_overloads, overload
from typing_extensions import NamedTuple, TypeIs
from typing_extensions import override, deprecated, Buffer, TypeAliasType, TypeVar, get_protocol_members, is_protocol
from typing_extensions import Doc
from typing_extensions import Doc, NoDefault
from _typed_dict_test_helper import Foo, FooGeneric, VeryAnnotated

# Flags used to mark tests that only apply after a specific
Expand All @@ -59,9 +59,9 @@
# versions, but not all
HAS_FORWARD_MODULE = "module" in inspect.signature(typing._type_check).parameters

skip_if_early_py313_alpha = skipIf(
sys.version_info[:4] == (3, 13, 0, 'alpha') and sys.version_info.serial < 3,
"Bugfixes will be released in 3.13.0a3"
skip_if_py313_beta_1 = skipIf(
sys.version_info[:5] == (3, 13, 0, 'beta', 1),
"Bugfixes will be released in 3.13.0b2"
)

ANN_MODULE_SOURCE = '''\
Expand Down Expand Up @@ -3485,7 +3485,6 @@ def method(self) -> None: ...
self.assertIsInstance(Foo(), ProtocolWithMixedMembers)
self.assertNotIsInstance(42, ProtocolWithMixedMembers)

@skip_if_early_py313_alpha
def test_protocol_issubclass_error_message(self):
@runtime_checkable
class Vec2D(Protocol):
Expand Down Expand Up @@ -5917,7 +5916,6 @@ class GenericNamedTuple(NamedTuple, Generic[T]):

self.assertEqual(CallNamedTuple.__orig_bases__, (NamedTuple,))

@skip_if_early_py313_alpha
def test_setname_called_on_values_in_class_dictionary(self):
class Vanilla:
def __set_name__(self, owner, name):
Expand Down Expand Up @@ -5989,7 +5987,6 @@ class NamedTupleClass(NamedTuple):
TYPING_3_12_0,
"__set_name__ behaviour changed on py312+ to use BaseException.add_note()"
)
@skip_if_early_py313_alpha
def test_setname_raises_the_same_as_on_other_classes_py312_plus(self):
class CustomException(BaseException): pass

Expand Down Expand Up @@ -6029,7 +6026,6 @@ class NamedTupleClass(NamedTuple):
normal_exception.__notes__[0].replace("NormalClass", "NamedTupleClass")
)

@skip_if_early_py313_alpha
def test_strange_errors_when_accessing_set_name_itself(self):
class CustomException(Exception): pass

Expand Down Expand Up @@ -6207,12 +6203,15 @@ class A(Generic[T]): ...
def test_typevar_none(self):
U = typing_extensions.TypeVar('U')
U_None = typing_extensions.TypeVar('U_None', default=None)
self.assertEqual(U.__default__, None)
self.assertEqual(U_None.__default__, type(None))
self.assertIs(U.__default__, NoDefault)
self.assertFalse(U.has_default())
self.assertEqual(U_None.__default__, None)
self.assertTrue(U_None.has_default())

def test_paramspec(self):
P = ParamSpec('P', default=(str, int))
self.assertEqual(P.__default__, (str, int))
self.assertTrue(P.has_default())
self.assertIsInstance(P, ParamSpec)
if hasattr(typing, "ParamSpec"):
self.assertIsInstance(P, typing.ParamSpec)
Expand All @@ -6225,11 +6224,13 @@ class A(Generic[P]): ...

P_default = ParamSpec('P_default', default=...)
self.assertIs(P_default.__default__, ...)
self.assertTrue(P_default.has_default())

def test_typevartuple(self):
Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
self.assertIsInstance(Ts, TypeVarTuple)
self.assertTrue(Ts.has_default())
if hasattr(typing, "TypeVarTuple"):
self.assertIsInstance(Ts, typing.TypeVarTuple)
typing_Ts = typing.TypeVarTuple('Ts')
Expand Down Expand Up @@ -6276,6 +6277,32 @@ def test_pickle(self):
self.assertEqual(z.__default__, typevar.__default__)


class NoDefaultTests(BaseTestCase):
@skip_if_py313_beta_1
def test_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(NoDefault, proto)
loaded = pickle.loads(s)
self.assertIs(NoDefault, loaded)

def test_constructor(self):
self.assertIs(NoDefault, type(NoDefault)())
with self.assertRaises(TypeError):
type(NoDefault)(1)

def test_repr(self):
self.assertRegex(repr(NoDefault), r'typing(_extensions)?\.NoDefault')

def test_no_call(self):
with self.assertRaises(TypeError):
NoDefault()

@skip_if_py313_beta_1
def test_immutable(self):
with self.assertRaises(AttributeError):
NoDefault.foo = 'bar'


class TypeVarInferVarianceTests(BaseTestCase):
def test_typevar(self):
T = typing_extensions.TypeVar('T')
Expand Down
Loading
Loading