Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

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

.. attribute:: __default__

The default value of the type variable, if any. This attribute is ``None``
if the type variable has no default, and :class:`types.NoneType`
if the default is set to ``None``.
The default value of the type variable, or :data:`typing.NoDefault` if it
has no default.

.. versionadded:: 3.13

.. method:: has_default()

Return whether or not the type variable has a default value. This is equivalent
to checking whether :attr:`__default__` is not equal to :data:`typing.NoDefault`,
except that it does not force evaluation of the
:ref:`lazily evaluated <lazy-evaluation>` default value.

.. versionadded:: 3.13

Expand DownExpand Up@@ -1886,9 +1894,17 @@ without the dedicated syntax, as documented below.

.. attribute:: __default__

The default value of the type variable tuple, if any. This attribute is ``None``
if the type variable tuple has no default, and :class:`types.NoneType`
if the default is set to ``None``.
The default value of the type variable tuple, or :data:`typing.NoDefault` if it
has no default.

.. versionadded:: 3.13

.. method:: has_default()

Return whether or not the type variable tuple has a default value. This is equivalent
to checking whether :attr:`__default__` is not equal to :data:`typing.NoDefault`,
except that it does not force evaluation of the
:ref:`lazily evaluated <lazy-evaluation>` default value.

.. versionadded:: 3.13

Expand DownExpand Up@@ -1974,9 +1990,17 @@ without the dedicated syntax, as documented below.

.. attribute:: __default__

The default value of the parameter specification, if any. This attribute is ``None``
if the parameter specification has no default, and :class:`types.NoneType`
if the default is set to ``None``.
The default value of the parameter specification, or :data:`typing.NoDefault` if it
has no default.

.. versionadded:: 3.13

.. method:: has_default()

Return whether or not the parameter specification has a default value. This is equivalent
to checking whether :attr:`__default__` is not equal to :data:`typing.NoDefault`,
except that it does not force evaluation of the
:ref:`lazily evaluated <lazy-evaluation>` default value.

.. versionadded:: 3.13

Expand DownExpand Up@@ -3213,9 +3237,9 @@ Introspection helpers

A sentinel object used to indicate that a type parameter has no default
value. For example:

.. doctest::

>>> T = TypeVar("T")
>>> T.__default__ is typing.NoDefault
True
Expand Down
16 changes: 8 additions & 8 deletionsLib/test/test_type_params.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,7 @@
import weakref
from test.support import requires_working_socket, check_syntax_error, run_code

from typing import Generic, Sequence, TypeVar, TypeVarTuple, ParamSpec, get_args
from typing import Generic,NoDefault,Sequence, TypeVar, TypeVarTuple, ParamSpec, get_args


class TypeParamsInvalidTest(unittest.TestCase):
Expand DownExpand Up@@ -599,12 +599,12 @@ class Foo[T: Foo, U: (Foo, Foo)]:
self.assertEqual(type_params[0].__name__, "T")
self.assertIs(type_params[0].__bound__, Foo)
self.assertEqual(type_params[0].__constraints__, ())
self.assertIs(type_params[0].__default__,None)
self.assertIs(type_params[0].__default__,NoDefault)

self.assertEqual(type_params[1].__name__, "U")
self.assertIs(type_params[1].__bound__, None)
self.assertEqual(type_params[1].__constraints__, (Foo, Foo))
self.assertIs(type_params[1].__default__,None)
self.assertIs(type_params[1].__default__,NoDefault)

def test_evaluation_error(self):
class Foo[T: Undefined, U: (Undefined,)]:
Expand All@@ -615,8 +615,8 @@ class Foo[T: Undefined, U: (Undefined,)]:
type_params[0].__bound__
self.assertEqual(type_params[0].__constraints__, ())
self.assertIs(type_params[1].__bound__, None)
self.assertIs(type_params[0].__default__,None)
self.assertIs(type_params[1].__default__,None)
self.assertIs(type_params[0].__default__,NoDefault)
self.assertIs(type_params[1].__default__,NoDefault)
with self.assertRaises(NameError):
type_params[1].__constraints__

Expand DownExpand Up@@ -1182,7 +1182,7 @@ def func[T=int, **U=float, *V=None]():
T, U, V = ns["func"].__type_params__
self.assertIs(T.__default__, int)
self.assertIs(U.__default__, float)
self.assertIs(V.__default__,type(None))
self.assertIs(V.__default__, None)

def test_defaults_on_class(self):
ns = run_code("""
Expand All@@ -1193,7 +1193,7 @@ class C[T=int, **U=float, *V=None]:
T, U, V = ns["C"].__type_params__
self.assertIs(T.__default__, int)
self.assertIs(U.__default__, float)
self.assertIs(V.__default__,type(None))
self.assertIs(V.__default__, None)

def test_defaults_on_type_alias(self):
ns = run_code("""
Expand All@@ -1203,7 +1203,7 @@ def test_defaults_on_type_alias(self):
T, U, V = ns["Alias"].__type_params__
self.assertIs(T.__default__, int)
self.assertIs(U.__default__, float)
self.assertIs(V.__default__,type(None))
self.assertIs(V.__default__, None)

def test_starred_invalid(self):
check_syntax_error(self, "type Alias[T = *int] = int")
Expand Down
12 changes: 12 additions & 0 deletionsLib/test/test_typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -584,6 +584,7 @@ class TypeParameterDefaultsTests(BaseTestCase):
def test_typevar(self):
T = TypeVar('T', default=int)
self.assertEqual(T.__default__, int)
self.assertTrue(T.has_default())
self.assertIsInstance(T, TypeVar)

class A(Generic[T]): ...
Expand All@@ -593,15 +594,19 @@ def test_typevar_none(self):
U = TypeVar('U')
U_None = TypeVar('U_None', default=None)
self.assertIs(U.__default__, NoDefault)
self.assertFalse(U.has_default())
self.assertIs(U_None.__default__, None)
self.assertTrue(U_None.has_default())

class X[T]: ...
T, = X.__type_params__
self.assertIs(T.__default__, NoDefault)
self.assertFalse(T.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)

class A(Generic[P]): ...
Expand All@@ -614,15 +619,19 @@ def test_paramspec_none(self):
U = ParamSpec('U')
U_None = ParamSpec('U_None', default=None)
self.assertIs(U.__default__, NoDefault)
self.assertFalse(U.has_default())
self.assertIs(U_None.__default__, None)
self.assertTrue(U_None.has_default())

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

def test_typevartuple(self):
Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
self.assertTrue(Ts.has_default())
self.assertIsInstance(Ts, TypeVarTuple)

class A(Generic[Unpack[Ts]]): ...
Expand DownExpand Up@@ -691,11 +700,14 @@ def test_typevartuple_none(self):
U = TypeVarTuple('U')
U_None = TypeVarTuple('U_None', default=None)
self.assertIs(U.__default__, NoDefault)
self.assertFalse(U.has_default())
self.assertIs(U_None.__default__, None)
self.assertTrue(U_None.has_default())

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

def test_no_default_after_non_default(self):
DefaultStrT = TypeVar('DefaultStrT', default=str)
Expand Down
12 changes: 6 additions & 6 deletionsLib/typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -286,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 NoDefault:
if type_var_tuple_encountered and t.has_default():
raise TypeError('Type parameter with a default'
' follows TypeVarTuple')

if t.__default__ is not NoDefault:
if t.has_default():
default_encountered = True
elif default_encountered:
raise TypeError(f'Type parameter {t!r} without a default'
Expand DownExpand Up@@ -325,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 NoDefault:
if cls.__parameters__[actual_len].has_default():
return

expected_len -= sum(p.__default__ is not NoDefault for p in cls.__parameters__)
expected_len -= sum(p.has_default() for p in cls.__parameters__)
expect_val = f"at least {expected_len}"
else:
expect_val = expected_len
Expand DownExpand Up@@ -1126,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 NoDefault:
if left == alen - right and self.has_default():
replacement = _unpack_args(self.__default__)
else:
replacement = args[left: alen - right]
Expand All@@ -1152,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 NoDefault:
if i == len(args) and self.has_default():
args = [*args, self.__default__]
if i >= len(args):
raise TypeError(f"Too few arguments for {alias}")
Expand Down
61 changes: 56 additions & 5 deletionsObjects/clinic/typevarobject.c.h
View file
Open in desktop

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

Loading

[8]ページ先頭

©2009-2025 Movatter.jp