Bug Report
Running mypy with --strict does not find an error which is found without --strict because the return type of a method is changed to Any.
To Reproduce
import cantools
ffn = 'test.dbc'
dbc = cantools.database.load_file(ffn)
assert isinstance(dbc, cantools.database.can.database.Database)
reveal_type(dbc.decode_message)
rawdata = b'ABCDEABC'
data = dbc.decode_message(0x123, rawdata)
reveal_type(data)
print(data['voltage'])
Running mypy without --strict: The return type of dbc.decode_message is recognized correctly and an expected error message is printed for the last line.
$ mypy test.py
test.py:9: note: Revealed type is "def (frame_id_or_name: Union[builtins.int, builtins.str], data: builtins.bytes, decode_choices: builtins.bool =, scaling: builtins.bool =, decode_containers: builtins.bool =, allow_truncated: builtins.bool =) -> Union[builtins.dict[builtins.str, Union[builtins.float, builtins.str, cantools.database.can.signal.NamedSignalValue]], typing.Sequence[Union[Tuple[cantools.database.can.message.Message, builtins.dict[builtins.str, Union[builtins.float, builtins.str, cantools.database.can.signal.NamedSignalValue]]], Tuple[builtins.int, builtins.bytes]]]]"
test.py:13: note: Revealed type is "Union[builtins.dict[builtins.str, Union[builtins.float, builtins.str, cantools.database.can.signal.NamedSignalValue]], typing.Sequence[Union[Tuple[cantools.database.can.message.Message, builtins.dict[builtins.str, Union[builtins.float, builtins.str, cantools.database.can.signal.NamedSignalValue]]], Tuple[builtins.int, builtins.bytes]]]]"
test.py:14: error: No overload variant of "__getitem__" of "Sequence" matches argument type "str"
test.py:14: note: Possible overload variants:
test.py:14: note: def __getitem__(self, int) -> Union[Tuple[Message, Dict[str, Union[float, str, NamedSignalValue]]], Tuple[int, bytes]]
test.py:14: note: def __getitem__(self, slice) -> Sequence[Union[Tuple[Message, Dict[str, Union[float, str, NamedSignalValue]]], Tuple[int, bytes]]]
Running mypy with --strict: The return type of dbc.decode_message is considered to be Any and because of that the error is not recognized.
$ mypy --strict test.py
test.py:9: note: Revealed type is "def (frame_id_or_name: Union[builtins.int, builtins.str], data: builtins.bytes, decode_choices: builtins.bool =, scaling: builtins.bool =, decode_containers: builtins.bool =, allow_truncated: builtins.bool =) -> Any"
test.py:13: note: Revealed type is "Any"
Success: no issues found in 1 source file
Expected Behavior
mypy should detect the return type even when using --strict and print the same error message like without --strict.
Actual Behavior
mypy does not find the error.
Your Environment
- Mypy version used: 0.982 (compiled: yes)
- Mypy command-line flags:
--strict
- Mypy configuration options from
mypy.ini (and other config files): none
- Python version used: 3.9.2
- cantools version: 37.2.0
References
Bug Report
Running mypy with
--strictdoes not find an error which is found without--strictbecause the return type of a method is changed toAny.To Reproduce
Running mypy without --strict: The return type of
dbc.decode_messageis recognized correctly and an expected error message is printed for the last line.Running mypy with
--strict: The return type ofdbc.decode_messageis considered to beAnyand because of that the error is not recognized.Expected Behavior
mypy should detect the return type even when using
--strictand print the same error message like without--strict.Actual Behavior
mypy does not find the error.
Your Environment
--strictmypy.ini(and other config files): noneReferences
dbc.decode_messageis defined here: https://github.com/cantools/cantools/blob/master/cantools/database/can/database.py#L451DecodeResultTypeis defined here: https://github.com/cantools/cantools/blob/master/cantools/typechecking.py#L61