Skip to content

--strict changes return type to Any #13933

Description

@erzoe

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

Activity

  1. erzoe commented on Oct 22, 2022

    @erzoe
    Author

    The problem seems to be --no-implicit-reexport. mypy --strict --implicit-reexport test.py shows the expected error message.

    EDIT: If I see things correctly the return type DecodeResultType relies 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

  2. hauntsaninja commented on Apr 24, 2023

    @hauntsaninja
    Collaborator

    Thanks, closing as a duplicate of the (partially fixed) #13965

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions