Skip to content

Commit

Permalink
Change error "Module has no attribute 'x'" to "Module 'y' has no attr…
Browse files Browse the repository at this point in the history
…ibute 'x'".
  • Loading branch information
Guido van Rossum committed Sep 14, 2016
1 parent 193a74c commit c7ffdf9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
7 changes: 4 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ def visit_import_from(self, imp: ImportFrom) -> None:
module_public=module_public)
self.add_symbol(imported_id, symbol, imp)
else:
message = "Module has no attribute '{}'".format(id)
message = "Module '{}' has no attribute '{}'".format(import_id, id)
extra = self.undefined_name_extra_info('{}.{}'.format(import_id, id))
if extra:
message += " {}".format(extra)
Expand Down Expand Up @@ -2163,9 +2163,10 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
# the build would terminate after semantic analysis
# and we wouldn't be able to report any type errors.
full_name = '%s.%s' % (file.fullname() if file is not None else None, expr.name)
mod_name = " '%s'" % file.fullname() if file is not None else ''
if full_name in obsolete_name_mapping:
self.fail("Module has no attribute %r (it's now called %r)" % (
expr.name, obsolete_name_mapping[full_name]), expr)
self.fail("Module%s has no attribute %r (it's now called %r)" % (
mod_name, expr.name, obsolete_name_mapping[full_name]), expr)
elif isinstance(base, RefExpr) and isinstance(base.node, TypeInfo):
# This branch handles the case C.bar where C is a class
# and bar is a module resulting from `import bar` inside
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ from foo import B
class C(B):
pass
[out]
tmp/bar.py:1: error: Module has no attribute 'B'
tmp/bar.py:1: error: Module 'foo' has no attribute 'B'

[case testImportSuppressedWhileAlmostSilent]
# cmd: mypy -m main
Expand Down
16 changes: 8 additions & 8 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ from m import y
[file m.py]
x = 1
[out]
main:2: error: Module has no attribute 'y'
main:2: error: Module 'm' has no attribute 'y'

[case testMissingModule]
import typing
Expand Down Expand Up @@ -1240,8 +1240,8 @@ main: note: In function "f":
main:2: error: Name 'List' is not defined

[case testImportObsoleteTypingFunction]
from typing import Function # E: Module has no attribute 'Function' (it's now called 'typing.Callable')
from _m import Function # E: Module has no attribute 'Function'
from typing import Function # E: Module 'typing' has no attribute 'Function' (it's now called 'typing.Callable')
from _m import Function # E: Module '_m' has no attribute 'Function'
[file _m.py]
[out]

Expand Down Expand Up @@ -1273,7 +1273,7 @@ with f() as 1: pass # E: Invalid assignment target
from typing import typevar
t = typevar('t')
[out]
main:1: error: Module has no attribute 'typevar' (it's now called 'typing.TypeVar')
main:1: error: Module 'typing' has no attribute 'typevar' (it's now called 'typing.TypeVar')
--' (this fixes syntax highlighting)

[case testUseObsoleteNameForTypeVar2]
Expand All @@ -1286,7 +1286,7 @@ main:1: note: (Did you mean 'typing.TypeVar'?)
import typing
t = typing.typevar('t')
[out]
main:2: error: Module has no attribute 'typevar' (it's now called 'typing.TypeVar')
main:2: error: Module 'typing' has no attribute 'typevar' (it's now called 'typing.TypeVar')
--' (work around syntax highlighting :-/)

[case testInvalidTypeAnnotation]
Expand Down Expand Up @@ -1314,16 +1314,16 @@ class A:

[case testStubPackage]
from m import x
from m import y # E: Module has no attribute 'y'
from m import y # E: Module 'm' has no attribute 'y'
[file m/__init__.pyi]
x = 1
[out]

[case testStubPackageSubModule]
from m import x
from m import y # E: Module has no attribute 'y'
from m import y # E: Module 'm' has no attribute 'y'
from m.m2 import y
from m.m2 import z # E: Module has no attribute 'z'
from m.m2 import z # E: Module 'm.m2' has no attribute 'z'
[file m/__init__.pyi]
x = 1
[file m/m2.pyi]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/semanal-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ import m.x
from .x import nonexistent
[out]
main:1: note: In module imported here:
tmp/m/x.py:1: error: Module has no attribute 'nonexistent'
tmp/m/x.py:1: error: Module 'm.x' has no attribute 'nonexistent'

[case testImportFromSameModule]
import m.x
Expand All @@ -781,7 +781,7 @@ import m.x
from m.x import nonexistent
[out]
main:1: note: In module imported here:
tmp/m/x.py:1: error: Module has no attribute 'nonexistent'
tmp/m/x.py:1: error: Module 'm.x' has no attribute 'nonexistent'

[case testFromImportAsInStub]
from m import *
Expand Down

0 comments on commit c7ffdf9

Please sign in to comment.