Skip to content

Promote non-Final module global types, when accessed externally #3253

@carljm

Description

@carljm

Given lib.py:

x = 1

def f():
    reveal_type(x)

def g():
    global x
    x = 2
    x = "foo"

And main.py:

from lib import x
import lib

reveal_type(x)
reveal_type(lib.x)

lib.x = 2
lib.x = "foo"

(Multiplay gist ID e8a25e05120c2a275f26096e7a3c01d7, for those who multiplay.)

We currently reveal Literal[1] at all three reveal_type calls, and error on neither assignment to the global x in g, but on both assignments to lib.x.

I believe that the behavior here that would be most consistent with our handling of class attributes, and would match intent in the most cases, would be to reveal int on all three reveal_type calls, and error on both assignments of "foo" but neither assignment of 2.

This describes the behavior of mypy. All other type checkers currently have some deviation from this behavior, none of which make much sense to me.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions