Skip to content

Fine-grained: Fix crashes when refreshing synthetic types #4667

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 1 commit into from
Mar 2, 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
15 changes: 12 additions & 3 deletions mypy/server/astmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
)
from mypy.traverser import TraverserVisitor
from mypy.types import (
Type, TypeVisitor, Instance, AnyType, NoneTyp, CallableType, DeletedType, PartialType,
Type, SyntheticTypeVisitor, Instance, AnyType, NoneTyp, CallableType, DeletedType, PartialType,
TupleType, TypeType, TypeVarType, TypedDictType, UnboundType, UninhabitedType, UnionType,
Overloaded, TypeVarDef, TypeList
Overloaded, TypeVarDef, TypeList, CallableArgument, EllipsisType, StarType
)
from mypy.util import get_prefix

Expand Down Expand Up @@ -307,7 +307,7 @@ def replace_statements(self, nodes: List[Statement]) -> List[Statement]:
return result


class TypeReplaceVisitor(TypeVisitor[None]):
class TypeReplaceVisitor(SyntheticTypeVisitor[None]):
"""Similar to NodeReplaceVisitor, but for type objects."""

def __init__(self, replacements: Dict[SymbolNode, SymbolNode]) -> None:
Expand Down Expand Up @@ -376,6 +376,15 @@ def visit_type_list(self, typ: TypeList) -> None:
for item in typ.items:
item.accept(self)

def visit_callable_argument(self, typ: CallableArgument) -> None:
typ.typ.accept(self)

def visit_ellipsis_type(self, typ: EllipsisType) -> None:
pass

def visit_star_type(self, typ: StarType) -> None:
typ.type.accept(self)

def visit_uninhabited_type(self, typ: UninhabitedType) -> None:
pass

Expand Down
6 changes: 1 addition & 5 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def __init__(self, items: List[Type], line: int = -1, column: int = -1) -> None:
self.items = items

def accept(self, visitor: 'TypeVisitor[T]') -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_type_list(self)

def serialize(self) -> JsonDict:
Expand Down Expand Up @@ -1506,11 +1507,6 @@ def visit_type_type(self, t: TypeType) -> T:
def visit_forwardref_type(self, t: ForwardRef) -> T:
raise RuntimeError('Internal error: unresolved forward reference')

def visit_type_list(self, t: TypeList) -> T:
# TODO: Do we need to implement this in more visitors? TypeList objects can
# exist as components of UnboundTypes.
raise self._notimplemented_helper('type_list')


class SyntheticTypeVisitor(TypeVisitor[T]):
"""A TypeVisitor that also knows how to visit synthetic AST constructs.
Expand Down
37 changes: 37 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -2531,3 +2531,40 @@ x = ''
b.py:5: error: Incompatible types in assignment (expression has type "int", base class "tuple" defined the type as "Callable[[Tuple[int, ...], Any], int]")
==
b.py:5: error: Incompatible types in assignment (expression has type "int", base class "tuple" defined the type as "Callable[[Tuple[int, ...], Any], int]")

[case testReprocessEllipses1]
import a
[file a.py]
from typing import Tuple
def foo(x: Tuple[int, ...]) -> None: pass
[file a.py.2]
from typing import Tuple
def foo(x: Tuple[int, ...]) -> None: pass
[builtins fixtures/tuple.pyi]
[out]
==

[case testReprocessEllipses2]
import a
[file a.py]
from typing import Callable
def foo(x: Callable[..., int]) -> None: pass
[file a.py.2]
from typing import Callable
def foo(x: Callable[..., int]) -> None: pass
[out]
==

[case testReprocessCallableArg]
import a
[file a.py]
from typing import Callable
from mypy_extensions import Arg
def a(f: Callable[[Arg(int, 'x')], int]) -> None: pass
[file a.py.2]
from typing import Callable
from mypy_extensions import Arg
def a(f: Callable[[Arg(int, 'x')], int]) -> None: pass
[builtins fixtures/dict.pyi]
[out]
==
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/mypy_extensions.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# NOTE: Requires fixtures/dict.pyi

from typing import Dict, Type, TypeVar, Optional, Any

_T = TypeVar('_T')
Expand Down