Skip to content

Fix adding a class in a new module and using it #4761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2018
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
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ def add_unknown_symbol(self, name: str, context: Context, is_import: bool = Fals
var._fullname = self.qualified_name(name)
var.is_ready = True
if is_import:
any_type = AnyType(TypeOfAny.from_unimported_type)
any_type = AnyType(TypeOfAny.from_unimported_type, missing_import_name=var._fullname)
else:
any_type = AnyType(TypeOfAny.from_error)
var.type = any_type
Expand Down
2 changes: 2 additions & 0 deletions mypy/server/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ def visit_instance(self, typ: Instance) -> List[str]:
return triggers

def visit_any(self, typ: AnyType) -> List[str]:
if typ.missing_import_name is not None:
return [make_trigger(typ.missing_import_name)]
return []

def visit_none_type(self, typ: NoneTyp) -> List[str]:
Expand Down
3 changes: 2 additions & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
# context. This is slightly problematic as it allows using the type 'Any'
# as a base class -- however, this will fail soon at runtime so the problem
# is pretty minor.
return AnyType(TypeOfAny.from_unimported_type)
return AnyType(TypeOfAny.from_unimported_type,
missing_import_name=sym.node.type.missing_import_name)
# Allow unbound type variables when defining an alias
if not (self.aliasing and sym.kind == TVAR and
(not self.tvar_scope or self.tvar_scope.get_binding(sym) is None)):
Expand Down
16 changes: 14 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ class AnyType(Type):
def __init__(self,
type_of_any: TypeOfAny,
source_any: Optional['AnyType'] = None,
missing_import_name: Optional[str] = None,
line: int = -1,
column: int = -1) -> None:
super().__init__(line, column)
Expand All @@ -304,6 +305,14 @@ def __init__(self,
if source_any and source_any.source_any:
self.source_any = source_any.source_any

if source_any is None:
self.missing_import_name = missing_import_name
else:
self.missing_import_name = source_any.missing_import_name

# Only unimported type anys and anys from other anys should have an import name
assert (missing_import_name is None or
type_of_any in (TypeOfAny.from_unimported_type, TypeOfAny.from_another_any))
# Only Anys that come from another Any can have source_any.
assert type_of_any != TypeOfAny.from_another_any or source_any is not None
# We should not have chains of Anys.
Expand All @@ -321,6 +330,7 @@ def copy_modified(self,
if original_any is _dummy:
original_any = self.source_any
return AnyType(type_of_any=type_of_any, source_any=original_any,
missing_import_name=self.missing_import_name,
line=self.line, column=self.column)

def __hash__(self) -> int:
Expand All @@ -331,14 +341,16 @@ def __eq__(self, other: object) -> bool:

def serialize(self) -> JsonDict:
return {'.class': 'AnyType', 'type_of_any': self.type_of_any.name,
'source_any': self.source_any.serialize() if self.source_any is not None else None}
'source_any': self.source_any.serialize() if self.source_any is not None else None,
'missing_import_name': self.missing_import_name}

@classmethod
def deserialize(cls, data: JsonDict) -> 'AnyType':
assert data['.class'] == 'AnyType'
source = data['source_any']
return AnyType(TypeOfAny[data['type_of_any']],
AnyType.deserialize(source) if source is not None else None)
AnyType.deserialize(source) if source is not None else None,
data['missing_import_name'])


class UninhabitedType(Type):
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/deps.test
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,19 @@ from a import *
x = 0
[out]
<a[wildcard]> -> m

[case testMissingModuleClass1]
from b import A # type: ignore
def f(x: A) -> None:
x.foo()
[out]
<m.A> -> <m.f>, m.f
<b.A> -> m

[case testMissingModuleClass2]
from p.b import A # type: ignore
def f(x: A) -> None:
x.foo()
[out]
<m.A> -> <m.f>, m.f
<p.b.A> -> m
82 changes: 82 additions & 0 deletions test-data/unit/fine-grained-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1362,3 +1362,85 @@ def f() -> None:
a.py:2: error: "int" not callable
a.py:3: error: "str" not callable
==

[case testAddAndUseClass1]
[file a.py]
[file a.py.2]
from b import Foo
def bar(f: Foo) -> None:
f.foo(12)
[file b.py.2]
class Foo:
def foo(self, s: str) -> None: pass
[out]
==
a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"

[case testAddAndUseClass2]
[file a.py]
[file a.py.3]
from b import Foo
def bar(f: Foo) -> None:
f.foo(12)
[file b.py.2]
class Foo:
def foo(self, s: str) -> None: pass
[out]
==
==
a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"

[case testAddAndUseClass3]
# flags: --ignore-missing-imports
[file a.py]
[file a.py.2]
from b import Foo
def bar(f: Foo) -> None:
f.foo(12)
[file b.py.3]
class Foo:
def foo(self, s: str) -> None: pass
[out]
==
==
a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"

[case testAddAndUseClass4]
[file a.py]
[file a.py.2]
from b import *
def bar(f: Foo) -> None:
f.foo(12)
[file b.py.2]
class Foo:
def foo(self, s: str) -> None: pass
[out]
==
a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"

[case testAddAndUseClass4]
[file a.py]
[file a.py.2]
from p.b import *
def bar(f: Foo) -> None:
f.foo(12)
[file p/__init__.py]
[file p/b.py.2]
class Foo:
def foo(self, s: str) -> None: pass
[out]
==
a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"

[case testAddAndUseClass5]
[file a.py]
[file a.py.2]
from b import *
def bar(f: Foo) -> None:
f.foo(12)
[file b.py.2]
class Foo:
def foo(self, s: str) -> None: pass
[out]
==
a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"