Skip to content
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

gh-103763: Implement PEP 695 #103764

Merged
merged 232 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
232 commits
Select commit Hold shift + click to select a range
9c8cd71
Moved portions of earlier prototype into latest main CPython branch. …
msfterictraut Apr 9, 2023
609ac0d
Run Tools/build/generate_global_objects.py
JelleZijlstra Apr 13, 2023
7a5e457
Basic C implementation of TypeVar, TypeVarTuple, ParamSpec
JelleZijlstra Apr 13, 2023
1e0b74c
fix TypeVar signature
JelleZijlstra Apr 13, 2023
2a5023a
fix TypeVar argument parsing
JelleZijlstra Apr 13, 2023
1bc8574
ParamSpecArgs/Kwargs
JelleZijlstra Apr 14, 2023
ed42796
make typing importable
JelleZijlstra Apr 14, 2023
d06f43d
Add some things
JelleZijlstra Apr 14, 2023
830b36a
Updated unit tests to reflect the latest PEP 695 spec.
msfterictraut Apr 14, 2023
b6bf2bc
Merge remote-tracking branch 'erictraut/type_param_syntax4' into tvob…
JelleZijlstra Apr 15, 2023
6bb4795
Make *Ts work
JelleZijlstra Apr 15, 2023
c7c1618
Just call typing.py
JelleZijlstra Apr 15, 2023
772920f
attempt at pickling
JelleZijlstra Apr 15, 2023
6ff6f5a
better approach
JelleZijlstra Apr 15, 2023
9fdf373
fix a few tests and refcount bugs
JelleZijlstra Apr 15, 2023
f7c513d
Use Name expr for TypeAlias
cdce8p Apr 16, 2023
1d7b035
Add ast optimizer for typeparam
cdce8p Apr 16, 2023
85a4409
Use Name expr for TypeParameter
cdce8p Apr 16, 2023
55d0fd9
Revert AST changes for TypeParams
cdce8p Apr 17, 2023
618e173
Merge pull request #7 from cdce8p/eric-ast-changes
erictraut Apr 18, 2023
bef978f
broken compiler before merge
JelleZijlstra Apr 18, 2023
3cd026a
Merge remote-tracking branch 'erictraut/type_param_syntax4' into tvob…
JelleZijlstra Apr 18, 2023
b12663d
Works for functions
JelleZijlstra Apr 18, 2023
bb9c43c
fall back to normal scope
JelleZijlstra Apr 18, 2023
037ddfa
does not exactly work
JelleZijlstra Apr 19, 2023
a62f0c2
commit before new approach
JelleZijlstra Apr 21, 2023
9c7a153
New approach
JelleZijlstra Apr 21, 2023
aa8ea68
correctly make it a cellvar
JelleZijlstra Apr 21, 2023
ab6b718
Now it works
JelleZijlstra Apr 21, 2023
dba4293
fix defaults
JelleZijlstra Apr 21, 2023
e36c5f5
undo some unnecessary changes
JelleZijlstra Apr 21, 2023
21e1b1a
support async def
JelleZijlstra Apr 21, 2023
8505134
start class support
JelleZijlstra Apr 21, 2023
338b978
detect duplicate type params
JelleZijlstra Apr 21, 2023
04bc911
clean up some whitespace
JelleZijlstra Apr 22, 2023
521bd81
Improve some tests
JelleZijlstra Apr 22, 2023
f2d91b0
No runtime error there
JelleZijlstra Apr 22, 2023
ccf68d9
actually generate a typevar
JelleZijlstra Apr 22, 2023
6159d75
refactor typeparam blocks
JelleZijlstra Apr 22, 2023
d9c0d90
Fix nested class scopes (functions)
JelleZijlstra Apr 22, 2023
d26b82a
fix it in classes too
JelleZijlstra Apr 22, 2023
423d2de
Support bounds
JelleZijlstra Apr 22, 2023
fdd8877
Use dummy class to get TypeVar-like types
JelleZijlstra Apr 22, 2023
2461ed5
fix typo in test
JelleZijlstra Apr 22, 2023
e9a196f
Add union support to TypeVar (and ParamSpec, somewhat dubiously)
JelleZijlstra Apr 22, 2023
536cf13
add function.__type_variables__
JelleZijlstra Apr 23, 2023
c0b04e3
Generic work
JelleZijlstra Apr 23, 2023
59e6fa0
Use _Py_DECLARE_STR
JelleZijlstra Apr 23, 2023
5521c7d
Support inheriting from Generic
JelleZijlstra Apr 23, 2023
b9d8fbb
Fix up qualnames
JelleZijlstra Apr 23, 2023
f3340c2
Update the magic number, rearrange typing.py accordingly
JelleZijlstra Apr 23, 2023
b5de372
fix missing initialization
JelleZijlstra Apr 23, 2023
fa81c01
set __type_variables__ on classes
JelleZijlstra Apr 23, 2023
7aeee08
Add docs to typevarobject.c
JelleZijlstra Apr 23, 2023
a7e6bcb
drop Python implementations of Generic, TypeVar, etc.
JelleZijlstra Apr 23, 2023
fe84458
fix Generic
JelleZijlstra Apr 23, 2023
34c024d
make generics instantiable
JelleZijlstra Apr 23, 2023
d9ec56e
fix NamedTuple
JelleZijlstra Apr 23, 2023
6ab8b1e
fix some tests
JelleZijlstra Apr 23, 2023
d916974
one more
JelleZijlstra Apr 23, 2023
b38dea4
Use a heap type for Generic
JelleZijlstra Apr 24, 2023
8991bb1
Better way to set __type_variables__ in the class
JelleZijlstra Apr 24, 2023
c3f520a
Fix TypeVar | "ForwardRef"
JelleZijlstra Apr 24, 2023
e668ed8
Make ParamSpecArgs and Kwargs not subclassable
JelleZijlstra Apr 24, 2023
2b844f4
Correctly unpack TypeVarTuple
JelleZijlstra Apr 24, 2023
47d24d9
Add support for type aliases (but not for computing the value)
JelleZijlstra Apr 24, 2023
8ad0b5b
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 24, 2023
6312d7b
fix merge
JelleZijlstra Apr 24, 2023
48e23b0
Avoid complicating compile_call_helper
JelleZijlstra Apr 24, 2023
dec31ae
Apparently it is a length-0 list if there are no typeparams, not NULL
JelleZijlstra Apr 24, 2023
062999c
Fix GC for TypeAlias
JelleZijlstra Apr 24, 2023
9147a4a
fix warnings
JelleZijlstra Apr 24, 2023
1b043c8
Add to globals-to-fix
JelleZijlstra Apr 24, 2023
16cb6a5
Add to _freeze_module.vcxproj
JelleZijlstra Apr 24, 2023
1bdfb75
Use heap types, fixing pickling
JelleZijlstra Apr 24, 2023
308c7c1
Fix GC
JelleZijlstra Apr 24, 2023
ff22d22
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 24, 2023
c4ce394
Self-review
JelleZijlstra Apr 24, 2023
f02199f
It's supposed to be called TypeAliasType
JelleZijlstra Apr 25, 2023
3fe019b
Allow evaluating TypeAlias
JelleZijlstra Apr 25, 2023
737d96a
Support __or__ on type aliases
JelleZijlstra Apr 25, 2023
6fe9476
fix | and subscripting on TypeAliasType
JelleZijlstra Apr 25, 2023
80a9efb
Test fixes and new tests
JelleZijlstra Apr 25, 2023
21b3aec
Add syntax error for nonlocal as specified by the PEP
JelleZijlstra Apr 25, 2023
30487a5
Implement TypeAlias repr
JelleZijlstra Apr 25, 2023
1575c66
__type_variables__ -> __type_params__
JelleZijlstra Apr 25, 2023
a6045f0
Remove POP_NULL
JelleZijlstra Apr 25, 2023
2c8b4f1
Lazily evaluate TypeVar bounds/constraints
JelleZijlstra Apr 25, 2023
66851e6
Add NEWS
JelleZijlstra Apr 25, 2023
be059c1
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 25, 2023
ac7f722
Disallow yield/yield from/await/walrus
JelleZijlstra Apr 25, 2023
0f70925
fix doctests
JelleZijlstra Apr 25, 2023
e17c3b0
Stop altering the execution environment
JelleZijlstra Apr 25, 2023
45696ae
Disallow walrus/yield while evaluating generics
JelleZijlstra Apr 25, 2023
02e0a8a
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 25, 2023
7a8fc14
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 25, 2023
7195162
regen all, minor quality improvements
JelleZijlstra Apr 25, 2023
5fbd4d5
fix leaked ref to the TypeAlias type
JelleZijlstra Apr 25, 2023
f86be9c
Fix another leak
JelleZijlstra Apr 25, 2023
77b3807
Deallocate Generic correctly
JelleZijlstra Apr 25, 2023
f448248
Avoid a new funcflags
JelleZijlstra Apr 25, 2023
d32bc61
Fix missing INCREF
JelleZijlstra Apr 25, 2023
27a8a4c
Fix repr() for recursive type aliases
JelleZijlstra Apr 25, 2023
44b9222
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 26, 2023
445a39b
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 26, 2023
7fa37b4
Fix refleak in ParamSpecArgs/Kwargs
JelleZijlstra Apr 26, 2023
880f066
Add tp_clear methods
JelleZijlstra Apr 26, 2023
3d8e24e
regen-global-objects
JelleZijlstra Apr 26, 2023
74c1a0d
Apparently not
JelleZijlstra Apr 26, 2023
d5b86c5
Merge branch 'main' into tvobject
JelleZijlstra Apr 26, 2023
3b6098d
Add lookahead to grammar
JelleZijlstra Apr 26, 2023
9632c02
First try! It fails! James Powell rules apply.
larryhastings Apr 26, 2023
8a0ecb7
simplify the intrinsic
JelleZijlstra Apr 26, 2023
0bb175a
Changed strategy, func_class_dict and *_CLASS_DICT.
larryhastings Apr 26, 2023
5e4ec0e
Update comment on new magic number.
larryhastings Apr 26, 2023
12d3004
Cleanup leftover gunk from previous approach.
larryhastings Apr 26, 2023
6c3f65a
Add class.__globals__, fix refcnt bugs. Thx Jelle!
larryhastings Apr 26, 2023
84a00ce
LOAD_CLASS_DICT also checks globals. Thx Jelle!
larryhastings Apr 26, 2023
e0acb87
Make LOAD_CLASS_DICT also look in the global namespace and rename it …
JelleZijlstra Apr 26, 2023
d77c4fb
Merge branch 'loadclass' into tvobject
JelleZijlstra Apr 26, 2023
ea66c7c
Use INTRINSIC_SET_CLASS_DICT
JelleZijlstra Apr 27, 2023
b314a3f
Correctly handle class namespaces in TV bounds
JelleZijlstra Apr 27, 2023
2e913ca
Same for type aliases
JelleZijlstra Apr 27, 2023
394d128
Add test cases
JelleZijlstra Apr 27, 2023
bd49622
Handle name mangling
JelleZijlstra Apr 27, 2023
bf45a8e
I don't need __globals__ and it causes a lot of failures
JelleZijlstra Apr 27, 2023
ce7fe9c
Fix another test
JelleZijlstra Apr 27, 2023
e5a6c21
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 27, 2023
96324a5
Extend NEWS entry, give credit to Larry
JelleZijlstra Apr 27, 2023
fd0f7b4
Fix GC tracking in error conditions
JelleZijlstra Apr 27, 2023
e784da1
More error paths
JelleZijlstra Apr 27, 2023
72fc40c
Fix the NEWS?
JelleZijlstra Apr 27, 2023
4b6a815
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 27, 2023
93a4552
Can't have multi-line bulleted lists in NEWS
JelleZijlstra Apr 27, 2023
b36b7af
More test cases, remove redundant comment
JelleZijlstra Apr 27, 2023
a85fb6c
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 27, 2023
6454026
Add LOAD_CLASSDICT_OR_DEREF
JelleZijlstra Apr 27, 2023
21667d9
Remove outdated comment
JelleZijlstra Apr 27, 2023
e65d668
Add helper function to tests
JelleZijlstra Apr 27, 2023
e0a0b04
Expose TypeVar and friends through _typing
JelleZijlstra Apr 27, 2023
d5c44f5
Remove unnecessary symtable entry
JelleZijlstra Apr 27, 2023
ce6a990
Rename variable for clarity
JelleZijlstra Apr 27, 2023
2fc9cc7
fix shadowed test
JelleZijlstra Apr 27, 2023
3f71911
Use run_code in test_type_aliases too
JelleZijlstra Apr 27, 2023
0891a61
Update NEWS
JelleZijlstra Apr 27, 2023
78a9ba2
type is a soft keyword too
JelleZijlstra Apr 27, 2023
c0b45e1
add to test_ast
JelleZijlstra Apr 27, 2023
16a037f
Move functions closer together
JelleZijlstra Apr 27, 2023
c5d9378
try this way
JelleZijlstra Apr 28, 2023
0399ed4
comments
JelleZijlstra Apr 28, 2023
782fc35
Maybe this
JelleZijlstra Apr 28, 2023
a092896
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 28, 2023
0241da5
avoid special qualname handling
JelleZijlstra Apr 28, 2023
5b3a204
Fix opcode classification
JelleZijlstra Apr 28, 2023
78f3f4b
Fix qualname test
JelleZijlstra Apr 28, 2023
9d3177b
Harmonize implementation of the two intrinsics
JelleZijlstra Apr 28, 2023
c52179c
Simplify LOAD_LOCALS at the cost of compiler complexity
JelleZijlstra Apr 28, 2023
d0fa718
fix WASM
JelleZijlstra Apr 29, 2023
f0936de
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 29, 2023
5259096
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra Apr 29, 2023
c3d6464
Fix compiler warning
JelleZijlstra Apr 29, 2023
f580583
use check_syntax_error
JelleZijlstra Apr 30, 2023
17c8060
Better error messages for illegal bounds/constraints
JelleZijlstra Apr 30, 2023
f4ea500
Add CHECK_VERSION for type param lists
JelleZijlstra Apr 30, 2023
43bcd02
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 3, 2023
827b9e5
Test that super() works correctly within a hidden function
JelleZijlstra May 3, 2023
71cef78
Use a __classdict__ cell to store the class namespace (#3)
JelleZijlstra May 7, 2023
80ffbf3
Merge branch 'main' into tvobject
JelleZijlstra May 7, 2023
e786382
Fix a test
JelleZijlstra May 7, 2023
2a8f0ef
Remove stray function declarations
JelleZijlstra May 9, 2023
967f100
More thorough tests
JelleZijlstra May 9, 2023
4cbbd24
More CR on tests
JelleZijlstra May 9, 2023
8dfb93d
Update comment
JelleZijlstra May 9, 2023
43f0270
Real error for type_check
JelleZijlstra May 9, 2023
0b35c6c
Use vectorcall
JelleZijlstra May 9, 2023
a2f3c75
Small fixes to typevarobject.c
JelleZijlstra May 9, 2023
c51b9fc
mod, not mad
JelleZijlstra May 9, 2023
047abba
Use dict_add_o for class cells
JelleZijlstra May 9, 2023
51a4655
Small compiler fixes
JelleZijlstra May 9, 2023
5e68cd8
Refactor compiler_function for correct scope handling
JelleZijlstra May 9, 2023
3b63b76
Same for classes
JelleZijlstra May 9, 2023
281f575
And for type aliases
JelleZijlstra May 9, 2023
a3cd2e7
fixup
JelleZijlstra May 9, 2023
a4f6076
Remove unnecessary LOAD_LOCALS
JelleZijlstra May 9, 2023
88ac5dc
Remove .type_params for functions
JelleZijlstra May 9, 2023
32fc89c
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 9, 2023
02f1fca
Forgot one regen
JelleZijlstra May 9, 2023
249ca43
Simplify bytecode as suggested by Carl
JelleZijlstra May 9, 2023
645b08b
Add some test cases for unusual calls
JelleZijlstra May 9, 2023
17c30f6
Avoid compiler_nameop change
JelleZijlstra May 9, 2023
0067e00
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 10, 2023
39ace71
Rip out making bound a set
JelleZijlstra May 10, 2023
a1c59f7
New solution for nonlocal
JelleZijlstra May 10, 2023
f7e6219
Simplify TypeAliasType.__repr__
JelleZijlstra May 10, 2023
8f54b02
Remove bogus assertions
JelleZijlstra May 10, 2023
bb2df61
Remove redundant condition
JelleZijlstra May 10, 2023
fdf65ad
Use compiler_addop_o
JelleZijlstra May 10, 2023
4fecf7d
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 10, 2023
f8406ce
Undo unnecessary bracing changes
JelleZijlstra May 11, 2023
fd4fe5c
Separate out and expand nonlocal tests
JelleZijlstra May 11, 2023
0b94580
Add comments to the enum
JelleZijlstra May 11, 2023
e952d61
Some more tests and consistent naming
JelleZijlstra May 11, 2023
785dd45
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 11, 2023
be70e3c
Use _PyST_IsFunctionLike in comprehension inlining code
JelleZijlstra May 11, 2023
92c29e7
Simplify Generic creation
JelleZijlstra May 11, 2023
529c74d
Public constructor for TypeAlias
JelleZijlstra May 12, 2023
531fcf7
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 12, 2023
497e22a
regen opcode_targets.h
JelleZijlstra May 12, 2023
bfb5ac0
Add test now that it no longer crashes
JelleZijlstra May 12, 2023
a1be61d
Fix one refleak
JelleZijlstra May 12, 2023
1e9273c
Fix the other refleak
JelleZijlstra May 12, 2023
bc37579
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 12, 2023
3817ab4
Fix one more refleak
JelleZijlstra May 12, 2023
f2a9393
Remove unrelated change
JelleZijlstra May 12, 2023
4f1223e
remove stray raw assert
JelleZijlstra May 12, 2023
d490251
Merge branch 'main' into tvobject
JelleZijlstra May 12, 2023
33f4c12
Update typevarobject.c: use Py_RETURN_NONE
JelleZijlstra May 12, 2023
b9f4540
Review feedback
JelleZijlstra May 12, 2023
a64922f
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 12, 2023
8a783b3
Update Lib/test/support/__init__.py
JelleZijlstra May 12, 2023
9e6658a
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 13, 2023
0aad1f4
Use PyObject for TypeVar etc. names
JelleZijlstra May 14, 2023
a22a08f
Reorganize compiler opcodes; no more LOAD_CLASSDEREF
JelleZijlstra May 14, 2023
1328c75
Merge branch 'main' into tvobject
JelleZijlstra May 14, 2023
7d6016e
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 14, 2023
1401209
Fix another class scoping oddity
JelleZijlstra May 14, 2023
e049f5a
Make test still succeed under PEP 649
JelleZijlstra May 14, 2023
c4d9ec4
Fix bug with explicit global in class scope; rename flag for clarity
JelleZijlstra May 14, 2023
b93f85e
Fix more similar cases
JelleZijlstra May 14, 2023
f412f49
Merge remote-tracking branch 'upstream/main' into tvobject
JelleZijlstra May 15, 2023
d4e72a5
A few more tests
JelleZijlstra May 15, 2023
08d931c
Remove redundant function
JelleZijlstra May 15, 2023
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
3 changes: 3 additions & 0 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,7 @@ Function and class definitions
body=[
FunctionDef(
name='f',
typeparams=[],
args=arguments(
posonlyargs=[],
args=[
Expand Down Expand Up @@ -1847,6 +1848,7 @@ Function and class definitions
body=[
ClassDef(
name='Foo',
typeparams=[],
bases=[
Name(id='base1', ctx=Load()),
Name(id='base2', ctx=Load())],
Expand Down Expand Up @@ -1885,6 +1887,7 @@ Async and await
body=[
AsyncFunctionDef(
name='f',
typeparams=[],
args=arguments(
posonlyargs=[],
args=[],
Expand Down
46 changes: 40 additions & 6 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ simple_stmts[asdl_stmt_seq*]:
# will throw a SyntaxError.
simple_stmt[stmt_ty] (memo):
| assignment
| &"type" type_alias
| e=star_expressions { _PyAST_Expr(e, EXTRA) }
| &'return' return_stmt
| &('import' | 'from') import_stmt
Expand Down Expand Up @@ -252,8 +253,8 @@ class_def[stmt_ty]:

class_def_raw[stmt_ty]:
| invalid_class_def_raw
| 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
_PyAST_ClassDef(a->v.Name.id,
| 'class' a=NAME t=[type_params] b=['(' z=[arguments] ')' { z }] ':' c=block {
_PyAST_ClassDef(a->v.Name.id, t,
(b) ? ((expr_ty) b)->v.Call.args : NULL,
(b) ? ((expr_ty) b)->v.Call.keywords : NULL,
c, NULL, EXTRA) }
Expand All @@ -267,16 +268,16 @@ function_def[stmt_ty]:

function_def_raw[stmt_ty]:
| invalid_def_raw
| 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
_PyAST_FunctionDef(n->v.Name.id,
| 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
_PyAST_FunctionDef(n->v.Name.id, t,
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
| ASYNC 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
| ASYNC 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
CHECK_VERSION(
stmt_ty,
5,
"Async functions are",
_PyAST_AsyncFunctionDef(n->v.Name.id,
_PyAST_AsyncFunctionDef(n->v.Name.id, t,
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
) }
Expand Down Expand Up @@ -628,6 +629,39 @@ keyword_patterns[asdl_seq*]:
keyword_pattern[KeyPatternPair*]:
| arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }

# Type statement
# ---------------

type_alias[stmt_ty]:
| "type" n=NAME t=[type_params] '=' b=expression {
CHECK_VERSION(stmt_ty, 12, "Type statement is",
_PyAST_TypeAlias(CHECK(expr_ty, _PyPegen_set_expr_context(p, n, Store)), t, b, EXTRA)) }

# Type parameter declaration
# --------------------------

type_params[asdl_typeparam_seq*]: '[' t=type_param_seq ']' {
CHECK_VERSION(asdl_typeparam_seq *, 12, "Type parameter lists are", t) }

type_param_seq[asdl_typeparam_seq*]: a[asdl_typeparam_seq*]=','.type_param+ [','] { a }

type_param[typeparam_ty] (memo):
| a=NAME b=[type_param_bound] { _PyAST_TypeVar(a->v.Name.id, b, EXTRA) }
| '*' a=NAME colon=":" e=expression {
RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
? "cannot use constraints with TypeVarTuple"
: "cannot use bound with TypeVarTuple")
}
| '*' a=NAME { _PyAST_TypeVarTuple(a->v.Name.id, EXTRA) }
| '**' a=NAME colon=":" e=expression {
RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
? "cannot use constraints with ParamSpec"
: "cannot use bound with ParamSpec")
}
| '**' a=NAME { _PyAST_ParamSpec(a->v.Name.id, EXTRA) }

type_param_bound[expr_ty]: ":" e=expression { e }

# EXPRESSIONS
# -----------

Expand Down
1 change: 1 addition & 0 deletions Include/cpython/funcobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct {
PyObject *func_weakreflist; /* List of weak references */
PyObject *func_module; /* The __module__ attribute, can be anything */
PyObject *func_annotations; /* Annotations, a dict or NULL */
PyObject *func_typeparams; /* Tuple of active type variables or NULL */
vectorcallfunc vectorcall;
/* Version number for use by specializer.
* Can set to non-zero when we want to specialize.
Expand Down
97 changes: 76 additions & 21 deletions Include/internal/pycore_ast.h

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

7 changes: 7 additions & 0 deletions Include/internal/pycore_ast_state.h

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

2 changes: 2 additions & 0 deletions Include/internal/pycore_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct _py_func_state {
extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);

extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
extern PyObject *_Py_set_function_type_params(
PyThreadState* unused, PyObject *func, PyObject *type_params);

#ifdef __cplusplus
}
Expand Down
8 changes: 8 additions & 0 deletions Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ struct _Py_interp_cached_objects {
PyObject *type_slots_pname;
pytype_slotdef *type_slots_ptrs[MAX_EQUIV];

/* TypeVar and related types */
PyTypeObject *generic_type;
Copy link
Member

Choose a reason for hiding this comment

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

Why are all these classes stored on the interpreter?
Can't they be made static and shareable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Generic needs to be used with multiple inheritance; when I used it with static types that didn't work. If you feel strongly that we need static types I can try again.

Also, thanks for the review! I'll probably not have time to push code changes until tonight, but will post some replies.

Copy link
Member

Choose a reason for hiding this comment

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

Generic needs to be used with multiple inheritance; when I used it with static types that didn't work.

That is unfortunate. I do think they should be static classes.
However, if we need to fix how inheritance is implemented first, then no need to do it in this PR.

Can you remember and summarize what the problem was?

Copy link
Member Author

Choose a reason for hiding this comment

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

As I recall, it just didn't work; I got errors from fairly normal usage like class X[T](Base): (which needs to double-inherit from Base and Generic).

Copy link
Member

Choose a reason for hiding this comment

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

Any recollection as to what the error messages were?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, sorry. I can try it out tonight.

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually misremembered; the failures were a bit less basic: they had to do with the inheritance of __new__. I went back to commit b38dea4, before I made Generic a heap type. There were several test failures in test_typing:

A heap type allowed me to simply omit __new__ and have it be inherited like a normal Python class. With a static type without tp_new, the class would not be instantiable, and if I had it set to PyType_GenericNew, super().__new__ wouldn't be called correctly.

There were also some issues with pickling, but those should have been fixable.

PyTypeObject *typevar_type;
PyTypeObject *typevartuple_type;
PyTypeObject *paramspec_type;
PyTypeObject *paramspecargs_type;
PyTypeObject *paramspeckwargs_type;
PyTypeObject *typealias_type;
};

#define _Py_INTERP_STATIC_OBJECT(interp, NAME) \
Expand Down
Loading