Repository navigation
--strict changes return type to Any #13933
Copy link
Copy link
Closed
Labels
bugmypy got something wrongmypy got something wrong
Description
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 fileExpected 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
dbc.decode_messageis defined here: https://github.com/cantools/cantools/blob/master/cantools/database/can/database.py#L451- It's return type
DecodeResultTypeis defined here: https://github.com/cantools/cantools/blob/master/cantools/typechecking.py#L61
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong
Activity
erzoe commentedon Oct 22, 2022
The problem seems to be
--no-implicit-reexport.mypy --strict --implicit-reexport test.pyshows the expected error message.EDIT: If I see things correctly the return type
DecodeResultTyperelies on implicit reexport which is disabled by--strict. So I guess mypy treats it as Any because it does not know it. The problem is that this is happening silently. I think mypy should give an error that the return type is not defined.Implicit reexport is explained here: https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-no-implicit-reexport
return type of Database.decode_message is not considered to be Any wi…
Preserve (some) implicitly exported types
Preserve (some) implicitly exported types (#13967)
hauntsaninja commentedon Apr 24, 2023
Thanks, closing as a duplicate of the (partially fixed) #13965