Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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 4 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 13 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 56 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
39 changes: 36 additions & 3 deletions 39 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 Down Expand Up @@ -6207,12 +6207,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 +6228,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 +6281,34 @@ def test_pickle(self):
self.assertEqual(z.__default__, typevar.__default__)


class NoDefaultTests(BaseTestCase):
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):
NoDefault(1)

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

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

@skipIf(
sys.version_info[:5] == (3, 13, 0, "beta", 1),
"incorrectly raises TypeError in the first 3.13 beta"
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.