Skip to content

New semantic analyzer: Explain that global variable must be declared at module top level #6976

Open
@JukkaL

Description

@JukkaL

The error message is poor for code like this where we don't initialize or declare a global variable at the module top level:

from typing import List


def foo() -> None:
    global bar
    bar = []  # type: List[str]  # Name 'bar' already defined (possibly by an import)
    bar  # Name 'bar' is not defined

It would be better to be explicit and say something like "Global variable must be declared or initialized at module top level".

Follow-up to #6975.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions