Skip to content

global x: int is not tested #108983

Closed
Closed
@sobolevn

Description

@sobolevn

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

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)testsTests in the Lib/test dir

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions