Skip to content

Improve handling of global scope for partially defined check #14213

@ilinum

Description

@ilinum

Currently, partially defined check (responsible for two error codes: partially-defined and use-before-def) doesn't handle variables defined in outer scope very consistently.

Here's an example that generates no error right now:

def foo() -> None:
  global x
  print(x)  # No error here right now.

foo()  # This will result in a runtime error.
x = 1

However, in some cases, the check also generates use-before-def errors:

def foo() -> None:
  global x
  print(x)  # E: Name "x" is used before definition[use-before-def]
  x = 1

x = 1

Note that the same applies to inner functions.

The question is: what should the behavior be here? Should we generate a may be defined error in both cases? Or, perhaps, we should generate no error in either case (because it's a pretty common pattern).

@JukkaL @ilevkivskyi do you have thoughts?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions