Skip to content

Display full type of left operand in error messages #3147

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 5 commits into from
Apr 12, 2017
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
5 changes: 4 additions & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,14 @@ def expand(target: Type) -> Type:
arg_types = func.arg_types[1:]
ret_type = func.ret_type
variables = func.variables
if isinstance(original_type, CallableType) and original_type.is_type_obj():
original_type = TypeType(original_type.ret_type)
res = func.copy_modified(arg_types=arg_types,
arg_kinds=func.arg_kinds[1:],
arg_names=func.arg_names[1:],
variables=variables,
ret_type=ret_type)
ret_type=ret_type,
bound_args=[original_type])
return cast(F, res)


Expand Down
5 changes: 4 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ def incompatible_argument(self, n: int, m: int, callee: CallableType, arg_type:
target = ''
if callee.name:
name = callee.name
base = extract_type(name)
if callee.bound_args and callee.bound_args[0] is not None:
base = self.format(callee.bound_args[0])
else:
base = extract_type(name)

for op, method in op_methods.items():
for variant in method, '__r' + method[2:]:
Expand Down
12 changes: 11 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ class CallableType(FunctionLike):
# Was this callable generated by analyzing Type[...] instantiation?
from_type_type = False # type: bool

bound_args = None # type: List[Type]

def __init__(self,
arg_types: List[Type],
arg_kinds: List[int],
Expand All @@ -550,6 +552,7 @@ def __init__(self,
is_classmethod_class: bool = False,
special_sig: Optional[str] = None,
from_type_type: bool = False,
bound_args: List[Type] = None,
) -> None:
if variables is None:
variables = []
Expand All @@ -571,6 +574,7 @@ def __init__(self,
self.is_classmethod_class = is_classmethod_class
self.special_sig = special_sig
self.from_type_type = from_type_type
self.bound_args = bound_args or []
super().__init__(line, column)

def copy_modified(self,
Expand All @@ -586,7 +590,8 @@ def copy_modified(self,
column: int = _dummy,
is_ellipsis_args: bool = _dummy,
special_sig: Optional[str] = _dummy,
from_type_type: bool = _dummy) -> 'CallableType':
from_type_type: bool = _dummy,
bound_args: List[Type] = _dummy) -> 'CallableType':
return CallableType(
arg_types=arg_types if arg_types is not _dummy else self.arg_types,
arg_kinds=arg_kinds if arg_kinds is not _dummy else self.arg_kinds,
Expand All @@ -604,6 +609,7 @@ def copy_modified(self,
is_classmethod_class=self.is_classmethod_class,
special_sig=special_sig if special_sig is not _dummy else self.special_sig,
from_type_type=from_type_type if from_type_type is not _dummy else self.from_type_type,
bound_args=bound_args if bound_args is not _dummy else self.bound_args,
)

def is_type_obj(self) -> bool:
Expand Down Expand Up @@ -739,6 +745,8 @@ def serialize(self) -> JsonDict:
'is_ellipsis_args': self.is_ellipsis_args,
'implicit': self.implicit,
'is_classmethod_class': self.is_classmethod_class,
'bound_args': [(None if t is None else t.serialize())
for t in self.bound_args],
}

@classmethod
Expand All @@ -756,6 +764,8 @@ def deserialize(cls, data: JsonDict) -> 'CallableType':
is_ellipsis_args=data['is_ellipsis_args'],
implicit=data['implicit'],
is_classmethod_class=data['is_classmethod_class'],
bound_args=[(None if t is None else deserialize_type(t))
for t in data['bound_args']],
)


Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3038,7 +3038,7 @@ class Concrete(metaclass=Meta):
pass

reveal_type(Concrete + X()) # E: Revealed type is 'builtins.str'
Concrete + "hello" # E: Unsupported operand types for + ("Meta" and "str")
Concrete + "hello" # E: Unsupported operand types for + (Type[Concrete] and "str")

[case testMetaclassGetitem]
class M(type):
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ class B: pass
class C: pass
[out]
main:8: error: Incompatible types in assignment (expression has type "C", variable has type "B")
main:9: error: Unsupported operand types for + ("A" and "C")
main:9: error: Unsupported operand types for + (A[B, C] and "C")
main:10: error: Incompatible types in assignment (expression has type "B", variable has type "C")
main:11: error: Invalid index type "B" for "A"; expected type "C"
main:11: error: Invalid index type "B" for A[B, C]; expected type "C"

[case testOperatorAssignmentWithIndexLvalue1]
from typing import TypeVar, Generic
Expand All @@ -310,7 +310,7 @@ class C:
[out]
main:7: error: Unsupported operand types for + ("C" and "B")
main:7: error: Incompatible types in assignment (expression has type "B", target has type "C")
main:8: error: Invalid index type "C" for "A"; expected type "B"
main:8: error: Invalid index type "C" for A[C]; expected type "B"

[case testOperatorAssignmentWithIndexLvalue2]
from typing import TypeVar, Generic
Expand All @@ -331,9 +331,9 @@ class B: pass
class C:
def __add__(self, o: 'C') -> 'C': pass
[out]
main:7: error: Invalid index type "B" for "A"; expected type "C"
main:8: error: Invalid index type "C" for "A"; expected type "B"
main:9: error: Invalid index type "B" for "A"; expected type "C"
main:7: error: Invalid index type "B" for A[C]; expected type "C"
main:8: error: Invalid index type "C" for A[C]; expected type "B"
main:9: error: Invalid index type "B" for A[C]; expected type "C"


-- Nested generic types
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ while bool():
x + 'a'
break
x + [1]
x + 'a' # E: Unsupported operand types for + ("list" and "str")
x + 'a' # E: Unsupported operand types for + (List[int] and "str")
x + [1] # E: Unsupported operand types for + (likely involving Union)

[builtins fixtures/isinstancelist.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-newsyntax.test
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ from typing import Dict, Any
d: Dict[int, str] = {}
d[42] = 'ab'
d[42] = 42 # E: Incompatible types in assignment (expression has type "int", target has type "str")
d['ab'] = 'ab' # E: Invalid index type "str" for "dict"; expected type "int"
d['ab'] = 'ab' # E: Invalid index type "str" for Dict[int, str]; expected type "int"
[builtins fixtures/dict.pyi]
[out]

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ x = {None: None}
x["bar"] = 1
[builtins fixtures/dict.pyi]
[out]
main:2: error: Invalid index type "str" for "dict"; expected type None
main:2: error: Invalid index type "str" for Dict[None, None]; expected type None
main:2: error: Incompatible types in assignment (expression has type "int", target has type None)

[case testInferNonOptionalDictType]
Expand Down
10 changes: 5 additions & 5 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ print(tuple(a))
import typing
[1] + iter([2, 3])
[out]
_program.py:2: error: Unsupported operand types for + ("list" and Iterator[int])
_program.py:2: error: Unsupported operand types for + (List[int] and Iterator[int])

[case testInferHeterogeneousListOfIterables]
from typing import Sequence
Expand Down Expand Up @@ -1095,10 +1095,10 @@ MyDDict(dict)['0']
MyDDict(dict)[0]
[out]
_program.py:6: error: Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], str]
_program.py:9: error: Invalid index type "str" for "dict"; expected type "int"
_program.py:9: error: Invalid index type "str" for defaultdict[int, str]; expected type "int"
_program.py:9: error: Incompatible types in assignment (expression has type "int", target has type "str")
_program.py:19: error: Dict entry 0 has incompatible type "str": List[<uninhabited>]
_program.py:23: error: Invalid index type "str" for "dict"; expected type "int"
_program.py:23: error: Invalid index type "str" for MyDDict[Dict[_KT, _VT]]; expected type "int"

[case testNoSubcriptionOfStdlibCollections]
import collections
Expand All @@ -1120,7 +1120,7 @@ def f(d: collections.defaultdict[int, str]) -> None:
_program.py:5: error: "defaultdict" is not subscriptable
_program.py:6: error: "Counter" is not subscriptable
_program.py:9: error: "defaultdict" is not subscriptable
_program.py:12: error: Invalid index type "int" for "dict"; expected type "str"
_program.py:12: error: Invalid index type "int" for defaultdict[str, int]; expected type "str"
_program.py:14: error: "defaultdict" is not subscriptable, use "typing.DefaultDict" instead

[case testCollectionsAliases]
Expand Down Expand Up @@ -1148,7 +1148,7 @@ reveal_type(o6)

[out]
_testCollectionsAliases.py:5: error: Revealed type is 'collections.Counter[builtins.int]'
_testCollectionsAliases.py:6: error: Invalid index type "str" for "dict"; expected type "int"
_testCollectionsAliases.py:6: error: Invalid index type "str" for Counter[int]; expected type "int"
_testCollectionsAliases.py:9: error: Revealed type is 'collections.ChainMap[builtins.int, builtins.str]'
_testCollectionsAliases.py:12: error: Revealed type is 'collections.deque[builtins.int]'
_testCollectionsAliases.py:15: error: Revealed type is 'collections.Counter[builtins.int*]'
Expand Down