Skip to content
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

--strict changes return type to Any #13933

Closed
erzoe opened this issue Oct 21, 2022 · 2 comments
Closed

--strict changes return type to Any #13933

erzoe opened this issue Oct 21, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@erzoe
Copy link

erzoe commented Oct 21, 2022

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

@erzoe erzoe added the bug mypy got something wrong label Oct 21, 2022
@erzoe
Copy link
Author

erzoe commented Oct 22, 2022

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

erzoe added a commit to erzoe/cantools that referenced this issue Oct 24, 2022
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Oct 31, 2022
Fixes some of python#13965

We also need to modify attribute access logic (this is the TODO in the
PR), which is a little trickier. But cases like python#13933 convinced me it's
worth making this change, even before I get around to figuring that out.
JukkaL pushed a commit that referenced this issue Oct 31, 2022
Fixes some of #13965, fixes #12749

We also need to modify attribute access logic (this is the TODO in the
PR), which is a little trickier. But cases like #13933 convinced me it's
worth making this change, even before I get around to figuring that out.
@hauntsaninja
Copy link
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
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants