Skip to content

Commit e9fc69d

Browse files
ilevkivskyigvanrossum
authored andcommitted
Fix crash in relative imports (#4259)
Fixes #4111 This corrects a previous wrong --strict-optional change (and adds various tests).
1 parent 820a4ca commit e9fc69d

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

mypy/fastparse.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,8 @@ def visit_Import(self, n: ast3.Import) -> Import:
651651
def visit_ImportFrom(self, n: ast3.ImportFrom) -> ImportBase:
652652
assert n.level is not None
653653
if len(n.names) == 1 and n.names[0].name == '*':
654-
assert n.module is not None
655-
i = ImportAll(n.module, n.level) # type: ImportBase
654+
mod = n.module if n.module is not None else ''
655+
i = ImportAll(mod, n.level) # type: ImportBase
656656
else:
657657
i = ImportFrom(self.translate_module_id(n.module) if n.module is not None else '',
658658
n.level,

mypy/fastparse2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ def visit_Import(self, n: ast27.Import) -> Import:
647647
def visit_ImportFrom(self, n: ast27.ImportFrom) -> ImportBase:
648648
assert n.level is not None
649649
if len(n.names) == 1 and n.names[0].name == '*':
650-
assert n.module is not None
651-
i = ImportAll(n.module, n.level) # type: ImportBase
650+
mod = n.module if n.module is not None else ''
651+
i = ImportAll(mod, n.level) # type: ImportBase
652652
else:
653653
i = ImportFrom(self.translate_module_id(n.module) if n.module is not None else '',
654654
n.level,

test-data/unit/check-fastparse.test

+29
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,32 @@ def m(x, ((x, y), z)): # E: Duplicate argument 'x' in function definition
364364
pass
365365

366366
lambda x, (y, x): None # E: Duplicate argument 'x' in function definition
367+
368+
[case testNoCrashOnImportFromStar]
369+
from pack import *
370+
[file pack/__init__.py]
371+
from . import *
372+
373+
[case testNoCrashOnImportFromStarNested]
374+
import blamodule
375+
[file blamodule/__init__.py]
376+
from . import command
377+
from . import backends
378+
379+
[file blamodule/backends/__init__.py]
380+
from .Bla import Bla
381+
reveal_type(Bla().method()) # E: Revealed type is 'builtins.str'
382+
383+
[file blamodule/backends/Bla.py]
384+
from .. import *
385+
386+
class Bla:
387+
def method(self) -> str:
388+
return command.call()
389+
390+
[file blamodule/command.py]
391+
def call() -> str: pass
392+
393+
[case testNoCrashOnImportFromStarPython2]
394+
# flags: --py2
395+
from . import * # E: No parent module -- cannot perform relative import

0 commit comments

Comments
 (0)