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:
global x: int is not tested, only nonlocal x: int is
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
While reading https://peps.python.org/pep-0526/#where-annotations-aren-t-allowed I've noticed that not all corner-cases are covered:
cpython/Lib/test/test_grammar.py
Lines 347 to 365 in 6f8411c
For example, explicit PEP's example:
Two problems:
global x: intis not tested, onlynonlocal x: intisx: int; nonlocal xis not testedThere's also an interesting corner-case from the rejected ideas:
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
test_grammar#108984test_grammar(GH-108984) #109000test_grammar(GH-108984) #109001