8000 gh-116126: Implement PEP 696 by JelleZijlstra · Pull Request #116129 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-116126: Implement PEP 696 #116129

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 56 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
e7ca092
gh-116126: Grammar changes for PEP 696
JelleZijlstra Feb 29, 2024
828679e
Fix test_ast
JelleZijlstra Feb 29, 2024
d3b472b
new tests
JelleZijlstra Feb 29, 2024
6e467ee
fix test_unparse
JelleZijlstra Feb 29, 2024
fa48580
possible solution
JelleZijlstra Mar 1, 2024
49b077b
Can't use the name either
JelleZijlstra Mar 1, 2024
5cfd5e2
blurb
JelleZijlstra Mar 1, 2024
01b3270
should not have pushed that
JelleZijlstra Mar 1, 2024
7a69204
maybe this will compile
JelleZijlstra Mar 1, 2024
a265008
Feature version support
JelleZijlstra Mar 1, 2024
0c28297
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 10, 2024
8aa5b27
default_ -> default_value
JelleZijlstra Apr 10, 2024
bdd01bb
ast docs
JelleZijlstra Apr 10, 2024
2c28b9a
more docs
JelleZijlstra Apr 10, 2024
b0d467f
Fix typevarobject.c
JelleZijlstra Apr 10, 2024
d7e91e2
default=<unrepresentable>
JelleZijlstra Apr 10, 2024
35226b4
NoneType is immortal
JelleZijlstra Apr 10, 2024
2e079ee
Runtime implementation
JelleZijlstra Apr 10, 2024
e037052
What's New
JelleZijlstra Apr 10, 2024
7b15f11
Document None/NoneType weirdness
JelleZijlstra Apr 10, 2024
11bd102
fix doctest
JelleZijlstra Apr 11, 2024
c54aeed
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 23, 2024
71a348b
Feedback from Alex
JelleZijlstra Apr 23, 2024
900cd40
Partial work
JelleZijlstra Apr 23, 2024
c13420b
Allow * in TypeVarTuple default
JelleZijlstra Apr 23, 2024
8a08c62
Test and fix grammar
JelleZijlstra Apr 23, 2024
613159f
Fix TypeVarTuple substitution
JelleZijlstra Apr 23, 2024
4e0435e
Two more cases
JelleZijlstra Apr 23, 2024
7d54ace
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 23, 2024
12581d8
Fix TypeVar substitution
JelleZijlstra Apr 23, 2024
2b5a102
Fix ParamSpec
JelleZijlstra Apr 23, 2024
a2c48c9
Prohibit default after TypeVarTuple
JelleZijlstra Apr 23, 2024
29b9435
Wrap new syntax in run_code()
JelleZijlstra Apr 23, 2024
326de17
Silence Ruff
JelleZijlstra Apr 23, 2024
6f66775
Fix parameter markup
JelleZijlstra Apr 23, 2024
7efa4ce
default_ -> default_value
JelleZijlstra Apr 23, 2024
29ad843
Fix errors
JelleZijlstra Apr 23, 2024
2c04c14
arguments, not parameters
JelleZijlstra Apr 23, 2024
7d4f3fd
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 23, 2024
1a14367
Apply suggestions from code review
JelleZijlstra Apr 23, 2024
5890d79
regen clinic
JelleZijlstra Apr 23, 2024
19a7b48
variadic args for _unpack_args
JelleZijlstra Apr 23, 2024
ef0616b
Update Lib/typing.py
JelleZijlstra Apr 23, 2024
9d4e842
Update Doc/reference/executionmodel.rst
JelleZijlstra Apr 23, 2024
2686b97
Roundtrip tests
JelleZijlstra Apr 24, 2024
98d1dec
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 28, 2024
fbb6405
Fix doctest
JelleZijlstra Apr 28, 2024
e0ccfeb
Add typing.NoDefault as the default for TypeVar's default
JelleZijlstra Apr 28, 2024
15aaff9
Apply suggestions from code review
JelleZijlstra Apr 28, 2024
86f5aed
Add .has_default() and update docs
JelleZijlstra Apr 28, 2024
3e0c0fc
Apply suggestions from code review
JelleZijlstra Apr 28, 2024
f5a3d4b
remove trailing space
JelleZijlstra Apr 28, 2024
92ea108
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 30, 2024
5f6fdfd
Ignore new C globals
JelleZijlstra Apr 30, 2024
b3f053c
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra May 3, 2024
8c3e0b4
Fix scoping key
JelleZijlstra May 3, 2024
File filter

Filter by extension

8000 Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add typing.NoDefault as the default for TypeVar's default
  • Loading branch information
JelleZijlstra committed Apr 28, 2024
commit e0ccfeb8a3dfd4813e1b7823d14e78b0cdcb183b
13 changes: 10 additions & 3 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ without the dedicated syntax, as documented below.

.. _typevar:

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

Type variable.

Expand Down Expand Up @@ -1774,7 +1774,7 @@ without the dedicated syntax, as documented below.

.. _typevartuple:

.. class:: TypeVarTuple(name, default=<unrepresentable>)
.. class:: TypeVarTuple(name, default=typing.NoDefault)

Type variable tuple. A specialized form of :ref:`type variable <typevar>`
that enables *variadic* generics.
Expand Down Expand Up @@ -1903,7 +1903,7 @@ without the dedicated syntax, as documented below.

Support for default values was added.

.. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False, default=<unrepresentable>)
.. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False, default=typing.NoDefault)

Parameter specification variable. A specialized version of
:ref:`type variables <typevar>`.
Expand Down Expand Up @@ -3209,6 +3209,13 @@ Introspection helpers

.. versionadded:: 3.7.4

.. data:: NoDefault

A sentinel object used to represent the default value of certain parameters
used in this module.

.. versionadded:: 3.13

Constant
--------

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,10 @@ typing
an item of a :class:`typing.TypedDict` as read-only for type checkers.
See :pep:`705` for more details.

* Add :data:`typing.NoDefault`, a sentinel object used to represent the defaults
of some parameters in the :mod:`typing` module. (Contributed by Jelle Zijlstra in
:gh:`116126`.)

unicodedata
-----------

Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_typevarobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern int _Py_initialize_generic(PyInterpreterState *);
extern void _Py_clear_generic_types(PyInterpreterState *);

extern PyTypeObject _PyTypeAlias_Type;
extern PyObject _Py_NoDefaultStruct;

#ifdef __cplusplus
}
Expand Down
46 changes: 39 additions & 7 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from typing import Self, LiteralString
from typing import TypeAlias
from typing import ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs
from typing import TypeGuard, TypeIs
from typing import TypeGuard, TypeIs, NoDefault
import abc
import textwrap
import typing
Expand Down Expand Up @@ -592,8 +592,12 @@ class A(Generic[T]): ...
def test_typevar_none(self):
U = TypeVar('U')
U_None = TypeVar('U_None', default=None)
self.assertEqual(U.__default__, None)
self.assertEqual(U_None.__default__, type(None))
self.assertIs(U.__default__, NoDefault)
self.assertIs(U_None.__default__, None)

class X[T]: ...
T, = X.__type_params__
self.assertIs(T.__default__, NoDefault)

def test_paramspec(self):
P = ParamSpec('P', default=(str, int))
Expand All @@ -609,8 +613,12 @@ class A(Generic[P]): ...
def test_paramspec_none(self):
U = ParamSpec('U')
U_None = ParamSpec('U_None', default=None)
self.assertEqual(U.__default__, None)
self.assertEqual(U_None.__default__, type(None))
self.assertIs(U.__default__, NoDefault)
self.assertIs(U_None.__default__, None)

class X[**P]: ...
P, = X.__type_params__
self.assertIs(P.__default__, NoDefault)

def test_typevartuple(self):
Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
Expand Down Expand Up @@ -682,8 +690,12 @@ class A(Generic[T, P, U]): ...
def test_typevartuple_none(self):
U = TypeVarTuple('U')
U_None = TypeVarTuple('U_None', default=None)
self.assertEqual(U.__default__, None)
self.assertEqual(U_None.__default__, type(None))
self.assertIs(U.__default__, NoDefault)
self.assertIs(U_None.__default__, None)

class X[**Ts]: ...
Ts, = X.__type_params__
self.assertIs(Ts.__default__, NoDefault)

def test_no_default_after_non_default(self):
DefaultStrT = TypeVar('DefaultStrT', default=str)
Expand Down Expand Up @@ -10145,6 +10157,26 @@ class CustomerModel(ModelBase, init=False):
self.assertIsInstance(CustomerModel, Decorated)


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.assertEqual(repr(NoDefault), 'typing.NoDefault')

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


class AllTests(BaseTestCase):
"""Tests for __all__."""

Expand Down
14 changes: 8 additions & 6 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
ParamSpecKwargs,
TypeAliasType,
Generic,
NoDefault,
)

# Please keep __all__ alphabetized within each category.
Expand Down Expand Up @@ -138,6 +139,7 @@
'NewType',
'no_type_check',
'no_type_check_decorator',
'NoDefault',
'NoReturn',
'NotRequired',
'overload',
Expand Down Expand Up @@ -284,11 +286,11 @@ def _collect_parameters(args):
parameters.append(collected)
elif hasattr(t, '__typing_subst__'):
if t not in parameters:
if type_var_tuple_encountered and t.__default__ is not None:
if type_var_tuple_encountered and t.__default__ is not NoDefault:
raise TypeError('Type parameter with a default'
' follows TypeVarTuple')

if t.__default__ is not None:
if t.__default__ is not NoDefault:
default_encountered = True
elif default_encountered:
raise TypeError(f'Type parameter {t!r} without a default'
Expand Down Expand Up @@ -323,10 +325,10 @@ def _check_generic_specialization(cls, arguments):
# the number of arguments being passed not matching the number
# of parameters: all parameters that aren't explicitly
# specialized in this call are parameters with default values.
if cls.__parameters__[actual_len].__default__ is not None:
if cls.__parameters__[actual_len].__default__ is not NoDefault:
return

expected_len -= sum(p.__default__ is not None for p in cls.__parameters__)
expected_len -= sum(p.__default__ is not NoDefault for p in cls.__parameters__)
expect_val = f"at least {expected_len}"
else:
expect_val = expected_len
Expand Down Expand Up @@ -1124,7 +1126,7 @@ def _typevartuple_prepare_subst(self, alias, args):
elif left + right > alen:
raise TypeError(f"Too few arguments for {alias};"
f" actual {alen}, expected at least {plen-1}")
if left == alen - right and self.__default__ is not None:
if left == alen - right and self.__default__ is not NoDefault:
replacement = _unpack_args(self.__default__)
else:
replacement = args[left: alen - right]
Expand All @@ -1150,7 +1152,7 @@ def _paramspec_subst(self, arg):
def _paramspec_prepare_subst(self, alias, args):
params = alias.__parameters__
i = params.index(self)
if i == len(args) and self.__default__ is not None:
if i == len(args) and self.__default__ is not NoDefault:
args = [*args, self.__default__]
if i >= len(args):
raise TypeError(f"Too few arguments for {alias}")
Expand Down
3 changes: 3 additions & 0 deletions Modules/_typingmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ _typing_exec(PyObject *m)
if (PyModule_AddObjectRef(m, "TypeAliasType", (PyObject *)&_PyTypeAlias_Type) < 0) {
return -1;
}
if (PyModule_AddObjectRef(m, "NoDefault", (PyObject *)&_Py_NoDefaultStruct) < 0) {
return -1;
}
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions Objects/clinic/typevarobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0