Skip to content

Wrong type inferred for a name conflicting with a module name in an import cycle #7419

Open
@bluetech

Description

@bluetech

The following is a failing test case which demonstrates the issue. In runtime, the type of a that is printed is str, however mypy reveals the type _importlib_modulespec.ModuleType.

[case testImportCycleWithOverriddenName]
# flags: --strict --no-incremental
import package

[file package/__init__.py]
a = 'a from __init__'
from .b import B

[file package/a.py]

[file package/b.py]
from . import a
class B: pass
print(type(a))
reveal_type(a)

[out]
package/b.py:4: note: Revealed type is 'builtins.str'

Note that if the two lines in __init__.py are swapped, the runtime type is indeed ModuleType.

I did not look at the code, but my uneducated guess is that the current "state" of __init__.py is not consulted when the from . import a in b.py is processed. This can probably be reduced further.

  • mypy 0.720
  • Python 3.7.4

Spawned from issue #7393.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions