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

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
Feedback from Alex
  • Loading branch information
JelleZijlstra committed Apr 23, 2024
commit 71a348b89fffae3b7466d6d29d5e974a243f81a1
7 changes: 3 additions & 4 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _collect_parameters(args):
parameters.append(collected)
elif hasattr(t, '__typing_subst__'):
if t not in parameters:
if getattr(t, '__default__', None) is not None:
if t.__default__ is not None:
default_encountered = True
elif default_encountered:
raise TypeError(f'Type parameter {t!r} without a default'
Expand Down Expand Up @@ -314,11 +314,10 @@ def _check_generic(cls, parameters, elen):
if alen < elen:
# since we validate TypeVarLike default in _collect_type_vars
# or _collect_parameters we can safely check parameters[alen]
if getattr(parameters[alen], '__default__', None) is not None:
if parameters[alen].__default__ is not None:
return

num_default_tv = sum(getattr(p, '__default__', None)
is not None for p in parameters)
num_default_tv = sum(p.__default__ is not None for p in parameters)

elen -= num_default_tv

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Add grammar support for defaults on type parameters, as specified in
:pep:`696`. Patch by Jelle Zijlstra.
Implement :pep:`696`, adding support for defaults on type parameters.
Patch by Jelle Zijlstra.
18 changes: 18 additions & 0 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,18 @@ variables::\n\
class StrOrBytesSequence[A: (str, bytes)]:\n\
...\n\
\n\
Type variables can also have defaults:\n\
\n\
class IntDefault[T = int]:\n\
...\n\
\n\
However, if desired, reusable type variables can also be constructed\n\
manually, like so::\n\
\n\
T = TypeVar('T') # Can be anything\n\
S = TypeVar('S', bound=str) # Can be any subtype of str\n\
A = TypeVar('A', str, bytes) # Must be exactly str or bytes\n\
D = TypeVar('D', default=int) # Defaults to int\n\
\n\
Type variables exist primarily for the benefit of static type\n\
checkers. They serve as the parameters for generic types as well\n\
Expand Down Expand Up @@ -1019,10 +1025,17 @@ where the use of '**' creates a parameter specification::\n\
\n\
type IntFunc[**P] = Callable[P, int]\n\
\n\
The following syntax creates a parameter specification that defaults\n\
to a callable accepting two positional-only arguments of types int\n\
and str:\n\
\n\
type IntFuncDefault[**P = (int, str)] = Callable[P, int]\n\
\n\
For compatibility with Python 3.11 and earlier, ParamSpec objects\n\
can also be created as follows::\n\
\n\
P = ParamSpec('P')\n\
DefaultP = ParamSpec('DefaultP', default=(int, str))\n\
\n\
Parameter specification variables exist primarily for the benefit of\n\
static type checkers. They are used to forward the parameter types of\n\
Expand Down Expand Up @@ -1294,10 +1307,15 @@ where a single '*' indicates a type variable tuple::\n\
def move_first_element_to_last[T, *Ts](tup: tuple[T, *Ts]) -> tuple[*Ts, T]:\n\
return (*tup[1:], tup[0])\n\
\n\
Type variables tuples can have default values:\n\
\n\
type AliasWithDefault[*Ts = (str, int)] = tuple[*Ts]\n\
\n\
For compatibility with Python 3.11 and earlier, TypeVarTuple objects\n\
can also be created as follows::\n\
\n\
Ts = TypeVarTuple('Ts') # Can be given any name\n\
DefaultTs = TypeVarTuple('Ts', default=(str, int))\n\
\n\
Just as a TypeVar (type variable) is a placeholder for a single type,\n\
a TypeVarTuple is a placeholder for an *arbitrary* number of types. For\n\
Expand Down
0