Skip to content

global x: int is not tested #108983

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

Closed
sobolevn opened this issue Sep 6, 2023 · 1 comment
Closed

global x: int is not tested #108983

sobolevn opened this issue Sep 6, 2023 · 1 comment
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) tests Tests in the Lib/test dir

Comments

@sobolevn
Copy link
Member

sobolevn commented Sep 6, 2023

While reading https://peps.python.org/pep-0526/#where-annotations-aren-t-allowed I've noticed that not all corner-cases are covered:

def test_var_annot_syntax_errors(self):
# parser pass
check_syntax_error(self, "def f: int")
check_syntax_error(self, "x: int: str")
check_syntax_error(self, "def f():\n"
" nonlocal x: int\n")
# AST pass
check_syntax_error(self, "[x, 0]: int\n")
check_syntax_error(self, "f(): int\n")
check_syntax_error(self, "(x,): int")
check_syntax_error(self, "def f():\n"
" (x, y): int = (1, 2)\n")
# symtable pass
check_syntax_error(self, "def f():\n"
" x: int\n"
" global x\n")
check_syntax_error(self, "def f():\n"
" global x\n"
" x: int\n")

For example, explicit PEP's example:

def f():
    global x: int  # SyntaxError

def g():
    x: int  # Also a SyntaxError
    global x

Two problems:

  1. global x: int is not tested, only nonlocal x: int is
  2. x: int; nonlocal x is not tested

There's also an interesting corner-case from the rejected ideas:

x: int = y = 1
z = w: int = 1

Since, we have this test check_syntax_error(self, "def f: int"), I assume that we also test rejected ideas here. So, let's add this one as well.

I propose adding these three cases.

Linked PRs

@sobolevn sobolevn added tests Tests in the Lib/test dir interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Sep 6, 2023
@sobolevn sobolevn self-assigned this Sep 6, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Sep 6, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 6, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 6, 2023
JelleZijlstra pushed a commit that referenced this issue Sep 6, 2023
#109001)

gh-108983: Add more PEP 526 tests to `test_grammar` (GH-108984)
(cherry picked from commit 1fb20d4)

Co-authored-by: Nikita Sobolev <[email protected]>
Yhg1s pushed a commit that referenced this issue Sep 8, 2023
#109000)

gh-108983: Add more PEP 526 tests to `test_grammar` (GH-108984)
(cherry picked from commit 1fb20d4)

Co-authored-by: Nikita Sobolev <[email protected]>
@hugovk
Copy link
Member

hugovk commented Nov 9, 2023

Thank you!

@hugovk hugovk closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) tests Tests in the Lib/test dir
Projects
None yet
Development

No branches or pull requests

2 participants