Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypy/newsemanal/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,11 @@ def visit_import_from(self, imp: ImportFrom) -> None:
if self.process_import_over_existing_name(
imported_id, existing_symbol, node, imp):
continue
if (existing_symbol and isinstance(existing_symbol.node, MypyFile) and
existing_symbol.no_serialize): # submodule added to parent module
# Special case: allow replacing submodules with variables. This pattern
# is used by some libraries.
del self.globals[imported_id]
# 'from m import x as x' exports x in a stub file.
module_public = not self.is_stub_file or as_id is not None
module_hidden = not module_public and possible_module_id not in self.modules
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ from . import command
from . import backends

[file blamodule/backends/__init__.py]
from .Bla import Bla # E: Name 'Bla' already defined (by an import)
Bla().method() # E: Module not callable
from .Bla import Bla
Bla().method()

[file blamodule/backends/Bla.py]
from .. import *
Expand Down
62 changes: 62 additions & 0 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2049,3 +2049,65 @@ from pp import x as y
def __getattr__(attr): ...
[out2]
tmp/a.py:2: error: Revealed type is 'Any'

[case testNewAnanlyzerTrickyImportPackage]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to an incremental mode test case as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will add an incremental test.

from lib import config
import lib

reveal_type(lib.config.x) # E: Revealed type is 'builtins.int'
reveal_type(config.x) # E: Revealed type is 'builtins.int'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about trying to import lib.config as a module? Will it work? What about import lib.config as c?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, OK, I will try adding these tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so these two have different behavior in mypy:

  • import lib.config actually imports lib, so when one writes lib.config later, the variable takes precedence.
  • import lib.config as tmp finds module lib.config and binds it to tmp, so when one writes tmp later it refers to the module.

At runtime variable always wins. Fixing the second one is non-trivial, because we need to check whether there is a "shadowing" variable at every submodule level. Also this is kind of corner case to a corner case.


[file lib/__init__.py]
from lib.config import config

[file lib/config.py]
class Config:
x: int

config = Config()
[builtins fixtures/module.pyi]

[case testNewAnanlyzerTrickyImportPackageAlt]
import lib.config
import lib.config as tmp

reveal_type(lib.config.x) # E: Revealed type is 'builtins.int'
# TODO: this actually doesn't match runtime behavior, variable wins.
tmp.x # E: Module has no attribute "x"

[file lib/__init__.py]
from lib.config import config

[file lib/config.py]
class Config:
x: int

config = Config()
[builtins fixtures/module.pyi]

[case testNewAnanlyzerTrickyImportPackage_incremental]
import a

[file a.py]
from lib import config
import lib

[file a.py.2]
from lib import config
import lib

reveal_type(lib.config.x)
reveal_type(config.x)

[file lib/__init__.py]
from lib.config import config

[file lib/config.py]
class Config:
x: int

config = Config()
[builtins fixtures/module.pyi]
[out2]
tmp/a.py:4: error: Revealed type is 'builtins.int'
tmp/a.py:5: error: Revealed type is 'builtins.int'