Skip to content

Commit

Permalink
Fix crash in relative imports (#4259)
Browse files Browse the repository at this point in the history
Fixes #4111

This corrects a previous wrong --strict-optional change (and adds various tests).
  • Loading branch information
ilevkivskyi authored and gvanrossum committed Nov 17, 2017
1 parent 820a4ca commit e9fc69d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ def visit_Import(self, n: ast3.Import) -> Import:
def visit_ImportFrom(self, n: ast3.ImportFrom) -> ImportBase:
assert n.level is not None
if len(n.names) == 1 and n.names[0].name == '*':
assert n.module is not None
i = ImportAll(n.module, n.level) # type: ImportBase
mod = n.module if n.module is not None else ''
i = ImportAll(mod, n.level) # type: ImportBase
else:
i = ImportFrom(self.translate_module_id(n.module) if n.module is not None else '',
n.level,
Expand Down
4 changes: 2 additions & 2 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ def visit_Import(self, n: ast27.Import) -> Import:
def visit_ImportFrom(self, n: ast27.ImportFrom) -> ImportBase:
assert n.level is not None
if len(n.names) == 1 and n.names[0].name == '*':
assert n.module is not None
i = ImportAll(n.module, n.level) # type: ImportBase
mod = n.module if n.module is not None else ''
i = ImportAll(mod, n.level) # type: ImportBase
else:
i = ImportFrom(self.translate_module_id(n.module) if n.module is not None else '',
n.level,
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,32 @@ def m(x, ((x, y), z)): # E: Duplicate argument 'x' in function definition
pass

lambda x, (y, x): None # E: Duplicate argument 'x' in function definition

[case testNoCrashOnImportFromStar]
from pack import *
[file pack/__init__.py]
from . import *

[case testNoCrashOnImportFromStarNested]
import blamodule
[file blamodule/__init__.py]
from . import command
from . import backends

[file blamodule/backends/__init__.py]
from .Bla import Bla
reveal_type(Bla().method()) # E: Revealed type is 'builtins.str'

[file blamodule/backends/Bla.py]
from .. import *

class Bla:
def method(self) -> str:
return command.call()

[file blamodule/command.py]
def call() -> str: pass

[case testNoCrashOnImportFromStarPython2]
# flags: --py2
from . import * # E: No parent module -- cannot perform relative import

0 comments on commit e9fc69d

Please sign in to comment.