GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-43224: Implement PEP 646 grammar changes by mrahtz · Pull Request #31018 · python/cpython

Conversation

@mrahtz

@mrahtz mrahtz commented Jan 30, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

These are the parts of the old PR (#30398) relevant to only the grammar changes. To summarise, this adds support for:

  1. Starring within squared brackets, e.g. Tensor[*Ts], Tensor[T, *Ts] (where Ts is a TypeVarTuple and T is a TypeVar).
    • But not within an actual slice - e.g. Tensor[0:*Ts] should not be valid.
  2. Starring the annotation given to *args, e.g. *args: *Ts.

For 1, our intention is to call __iter__ on Ts and add the items from the resulting iterator to the tuple of arguments sent to Tensor.__cls_getitem__. For example, with Tensor[*Ts], if Ts.__iter__ returns an iterator yielding Ts.unpacked, Tensor.__cls_getitem__ would receive (Ts.unpacked,). With Tensor[T, *Ts], Tensor.__cls_getitem__ would receive (T, Ts.unpacked).

For 2, our intention is to call __iter__ on Ts, get a single item from the iterator, verify that the iterator is exhausted, and set that item as the annotation for *args.

I'm guessing the person I should ask for review on this is @pablogsal?

https://bugs.python.org/issue43224

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Jan 30, 2022
@bedevere-bot bedevere-bot added the awaiting review label Jan 30, 2022
@mrahtz
bpo-43224: Implement PEP 646 grammar changes
7ecb7ba
@mrahtz
mrahtz force-pushed the pep646-grammar branch from aaf0364 to 7ecb7ba Compare January 30, 2022 12:06
@mrahtz
mrahtz marked this pull request as ready for review January 30, 2022 12:26
@mrahtz
mrahtz requested review from lysnikolaou, markshannon and pablogsal as code owners January 30, 2022 12:26
@mrahtz mrahtz mentioned this pull request Jan 30, 2022
bpo-43224: Implement PEP 646 changes to genericaliasobject.c #31019
Merged
JelleZijlstra
JelleZijlstra reviewed Jan 30, 2022
View reviewed changes
Comment thread Lib/test/test_pep646_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz mrahtz mentioned this pull request Jan 30, 2022
bpo-43224: Implement PEP 646 changes to typing.py #31021
Merged
@mrahtz
Add tests for del
c0531ec
mrahtz
mrahtz commented Jan 30, 2022
View reviewed changes
Comment thread Grammar/python.gram Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Grammar/python.gram Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_pep646_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz
Tweak news to better fit in with other PRs
479c175
@pablogsal

pablogsal commented Feb 1, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@mrahtz Could you please add some expected syntax errors to Lib/test/test_syntax.py? Things like def f(**kwargs: *a) and the like.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz

mrahtz commented Feb 2, 2022

Copy link
Copy Markdown
Contributor Author

@pablogsal

Could you please add some expected syntax errors to Lib/test/test_syntax.py? Things like def f(**kwargs: *a) and the like.

Sure, will do. Two questions:

  • Is there a convention for which tests should be implemented as doctests vs test cases in test_syntax.py?
  • Should I move the uses of PEP 646 syntax that don't cause failure to test_grammar.py, or can they be kept in a test module of their own?
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Feb 2, 2022

Copy link
Copy Markdown
Member
  • Is there a convention for which tests should be implemented as doctests vs test cases in test_syntax.py?

If you want to check that something is a Syntax error and just want to assert the message and can do they with a simple input, in th doctest.

If you required special compilation modes, some gigantic or dynamic string or check the location of the errors, then a specific test case is needed.

  • Should I move the uses of PEP 646 syntax that don't cause failure to test_grammar.py, or can they be kept in a test module of their own?

In general we have one test file for testing the runtime implications of the grammar, and things go to test_grammar to check grammatical relates aspects or the shape of the AST. Normally you can do both if you want/need

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz
Move error cases into test_syntax
091ceeb
@mrahtz

mrahtz commented Feb 14, 2022

Copy link
Copy Markdown
Contributor Author

Sorry for the slow reply - I'm only just now finding time again to work on this.

Looking through test_grammar.py and test_syntax.py, it seemed like the most appropriate thing was just to move the failure cases from test_pep646_syntax.py to test_syntax.py. This is what I've done in the latest commit, but let me know if you'd like more doing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz
Add an extra failure test
f1c9c77
@phiresky phiresky mentioned this pull request Feb 18, 2022
Typing support for shapes numpy/numpy#16544
Closed
@mrahtz

mrahtz commented Mar 5, 2022

Copy link
Copy Markdown
Contributor Author

@pablogsal Friendly poke :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz

mrahtz commented Mar 16, 2022

Copy link
Copy Markdown
Contributor Author

@pablogsal Another friendly poke :) I'm getting a bit nervous we might miss the merge window for 3.11 on this - if you're busy at the moment, would you be able to recommend anyone else who'd be a good fit for a review of this? (Or @JelleZijlstra do you know of anyone?)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Mar 16, 2022

Copy link
Copy Markdown
Member

Maybe @isidentical if you'd like to take a look?

Seems like Pablo already looked at the diff to some extent, so if he doesn't have time for a full re-review, I can also do a review and help get this in. I'd of course prefer if someone familiar with the parser can do a review, though.

isidentical reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra
JelleZijlstra self-requested a review March 16, 2022 23:52
@isidentical
isidentical self-requested a review March 16, 2022 23:56
@JelleZijlstra

JelleZijlstra commented Mar 17, 2022

Copy link
Copy Markdown
Member

I noticed that this is allowed by the grammar: s[1:1, *x]. It's not meaningful for typing, but I don't think we need to go out of our way to disallow it. Could you add some tests that combine slicing with unpacking?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Mar 17, 2022

Copy link
Copy Markdown
Member

Maybe @isidentical if you'd like to take a look?

Seems like Pablo already looked at the diff to some extent, so if he doesn't have time for a full re-review, I can also do a review and help get this in. I'd of course prefer if someone familiar with the parser can do a review, though.

Apologies, unfortunately I was sick with COVID these past weeks and I couldn't dedicate much time to open source :(

I will do another pass today or tomorrow.

Thanks for your understanding @mrahtz

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

isidentical
isidentical requested changes Mar 17, 2022
View reviewed changes

@isidentical isidentical left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Seems like ast.unparse is broken on some scenerious:

import ast

tree_1 = ast.parse("A[1:2, *l]")
tree_2 = ast.parse(ast.unparse(ast.parse(tree_1)))
assert ast.dump(tree_1) == ast.dump(tree_2)

I assume it this is related to parenthesizing logic in here, which needs to be adapted to PEP646:

cpython/Lib/ast.py

Lines 1479 to 1487 in ef1327e

def is_simple_tuple(slice_value):
# when unparsing a non-empty tuple, the parentheses can be safely
# omitted if there aren't any elements that explicitly requires
# parentheses (such as starred expressions).
return (
isinstance(slice_value, Tuple)
and slice_value.elts
and not any(isinstance(elt, Starred) for elt in slice_value.elts)
)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot removed the awaiting review label Mar 17, 2022
@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Mar 22, 2022
JelleZijlstra added 2 commits March 22, 2022 20:32
@JelleZijlstra
fix whitespace (make patchcheck)
e1ab6ea
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into pep646-grammar
18e7fa7
@JelleZijlstra

JelleZijlstra commented Mar 23, 2022

Copy link
Copy Markdown
Member

I just fixed a merge conflict and a whitespace issue found by make patchcheck. I think we can go ahead and merge, but I noticed a few things we still need to address:

(1) The ast.py changes aren't tested. They should be tested with a call to ast.unparse.

(2) There is also a C version of unparse in Python/ast_unparse.c, which is used for from __future__ import annotations. It likely needs changes too.

(3) If you use from __future__ import annotations and call get_type_hints() on a function that takes *args: *Ts, it fails because it tries to parse *Ts as an expression. I guess to solve this we'll have to detect an initial * and remove it before trying to parse the code.

Here's an example that demonstrates these issues:

Details
from __future__ import annotations
import typing
import ast

Ts = typing.TypeVarTuple("Ts")

def f(x: tuple[*Ts], *args: *Ts):
    pass

print(f.__annotations__)
print(ast.unparse(ast.parse("tuple[*Ts]")))
print(typing.get_type_hints(f))
% ./python.exe Doc/tools/annotvt.py
{'x': 'tuple[(*Ts,)]', 'args': '*Ts'}
tuple[*Ts,]
Traceback (most recent call last):
  File "/Users/jelle/py/cpython/Lib/typing.py", line 738, in __init__
    code = compile(arg, '<string>', 'eval')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1
    *Ts
    ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jelle/py/cpython/Doc/tools/annotvt.py", line 12, in <module>
    print(typing.get_type_hints(f))
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jelle/py/cpython/Lib/typing.py", line 2199, in get_type_hints
    value = ForwardRef(
            ^^^^^^^^^^^
  File "/Users/jelle/py/cpython/Lib/typing.py", line 740, in __init__
    raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Forward reference must be an expression -- got '*Ts'
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Mar 23, 2022

Copy link
Copy Markdown
Member

Also, there were a few buildbot failures when Pablo triggered builds on 1543785, but apart from the whitespace issue I fixed, they looked like random failures on unrelated tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Mar 23, 2022

Copy link
Copy Markdown
Member

Thanks, go ahead, but do add the needed tests and fix the issues. Agreed on the parsing in get_type_hints.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mrahtz added 2 commits March 24, 2022 19:51
@mrahtz
Add tests for AST changes
72dd8b9
@mrahtz
Fix C unpack
80bb5fa
@JelleZijlstra

JelleZijlstra commented Mar 24, 2022

Copy link
Copy Markdown
Member

Thanks @mrahtz for the fixes! I'll do another review today or maybe tomorrow and then hopefully merge.

I think we can leave the get_type_hints() fix for another PR; no need to worry about that here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz
Fix typing.get_type_hints on variadic annotation
fbfd5b1
@mrahtz
mrahtz requested review from Fidget-Spinner and gvanrossum as code owners March 24, 2022 20:50
@mrahtz

mrahtz commented Mar 24, 2022

Copy link
Copy Markdown
Contributor Author

@JelleZijlstra Oops, I only just saw your message 😅 Let me know if you'd prefer to take the get_type_hints() fix out of this PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Mar 24, 2022

Copy link
Copy Markdown
Member

If you already implemented it, that's fine too :) Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mrahtz
mrahtz commented Mar 24, 2022
View reviewed changes
Comment thread Lib/test/test_future.py
scope = {}
exec(
"from __future__ import annotations\n"
+ code, {}, scope

@mrahtz mrahtz Mar 24, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I removed this because if both globals and locals are passed to exec, it treats the code as it if were executed in a class definition, which makes the new test I've added harder than it needs to be (we can't access c so easily). Removing this doesn't seem to break any of the rest of the tests, so I hope this is fine?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mrahtz
Fix typo
816098c
JelleZijlstra
JelleZijlstra reviewed Mar 25, 2022
View reviewed changes
Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner
Fidget-Spinner reviewed Mar 25, 2022
View reviewed changes
Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

mrahtz added 2 commits March 25, 2022 20:03
@mrahtz
Remove debug print
8962d26
@mrahtz
Fix potential shadowing in ForwardRef
973e9cc
@JelleZijlstra
JelleZijlstra self-requested a review March 25, 2022 20:16
JelleZijlstra
JelleZijlstra approved these changes Mar 26, 2022
View reviewed changes
@JelleZijlstra
JelleZijlstra merged commit e8e737b into python:main Mar 26, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Mar 26, 2022
@mrahtz

mrahtz commented Mar 26, 2022

Copy link
Copy Markdown
Contributor Author

Whoop whoop! :D

AlexWaygood and Fidget-Spinner reacted with heart emoji
All reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mrahtz
mrahtz deleted the pep646-grammar branch April 5, 2022 18:34
@mrahtz mrahtz mannequin mentioned this pull request Apr 11, 2022
Add support for PEP 646 #87390
Closed
@zsol zsol mentioned this pull request Jun 12, 2022
Add support for PEP-646 Instagram/LibCST#696
Merged
4 of 5 tasks
@JelleZijlstra JelleZijlstra mentioned this pull request Oct 25, 2023
Black cannot parse list unpacking psf/black#3977
Closed
@JelleZijlstra JelleZijlstra mentioned this pull request Feb 15, 2024
PEP 646 grammar changes are not documented #115528
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Fidget-Spinner Fidget-Spinner Fidget-Spinner left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@pablogsal pablogsal pablogsal approved these changes
@isidentical isidentical isidentical approved these changes
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@gvanrossum gvanrossum Awaiting requested review from gvanrossum

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@mrahtz @pablogsal @JelleZijlstra @bedevere-bot @gvanrossum @Fidget-Spinner @isidentical @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.