Skip to content

Commit

Permalink
Fix invalid type causing namedtuple errors in wrong files
Browse files Browse the repository at this point in the history
Fixes #8511.

This is a workaround of #4987, where UnboundTypes can leak.
  • Loading branch information
msullivan committed Mar 16, 2020
1 parent 05e5d58 commit 9ce419f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from typing_extensions import Final

from mypy.types import (
Type, TupleType, AnyType, TypeOfAny, TypeVarDef, CallableType, TypeType, TypeVarType
Type, TupleType, AnyType, TypeOfAny, TypeVarDef, CallableType, TypeType, TypeVarType,
UnboundType,
)
from mypy.semanal_shared import (
SemanticAnalyzerInterface, set_callable_name, calculate_tuple_fallback, PRIORITY_FALLBACKS
Expand Down Expand Up @@ -334,6 +335,9 @@ def parse_namedtuple_fields_with_types(self, nodes: List[Expression], context: C
except TypeTranslationError:
return self.fail_namedtuple_arg('Invalid field type', type_node)
analyzed = self.api.anal_type(type)
# Workaround #4987 and avoid introducing a bogus UnboundType
if isinstance(analyzed, UnboundType):
analyzed = AnyType(TypeOfAny.from_error)
# These should be all known, otherwise we would defer in visit_assignment_stmt().
if analyzed is None:
return None
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,17 @@ class A:

reveal_type(A().b) # N: Revealed type is 'Any'
[builtins fixtures/tuple.pyi]

[case testNamedTupleWrongfile]
from typing import NamedTuple
from b import Type1
Type2 = NamedTuple('Type2', [('x', Type1)])
[file b.py]
from typing import NamedTuple

def foo():
pass

Type1 = NamedTuple('Type1', [('foo', foo)]) # type: ignore

[builtins fixtures/tuple.pyi]

0 comments on commit 9ce419f

Please sign in to comment.