Skip to content

Incorrect inferred type #14210

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

Closed
epenet opened this issue Nov 28, 2022 · 1 comment
Closed

Incorrect inferred type #14210

epenet opened this issue Nov 28, 2022 · 1 comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@epenet
Copy link

epenet commented Nov 28, 2022

Bug Report

Whilst working on HomeAssistant PR, found this weird behavior where the type inferred from a dict is completely wrong.
home-assistant/core#82853

To Reproduce

ZHA_HW_DISCOVERY_DATA = {
    "name": "Yellow",
    "port": {
        "path": "/dev/ttyAMA1",
        "baudrate": 115200,
        "flow_control": "hardware",
    },
    "radio_type": "efr32",
}
reveal_type(ZHA_HW_DISCOVERY_DATA)

Expected Behavior

I would have expected the value type to be dict[str, str | dict], or maybe dict[str, Any]

Actual Behavior

Revealed type is builtins.dict[builtins.str, typing.Collection[builtins.str]]

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.15
@epenet epenet added the bug mypy got something wrong label Nov 28, 2022
@AlexWaygood AlexWaygood added the topic-join-v-union Using join vs. using unions label Nov 28, 2022
@AlexWaygood
Copy link
Member

This is another one of the many "join-vs-union" behavioural peculiarities that mypy has: in many situations, mypy prefers to infer a type as being a common supertype rather than a union. Here, it sees that some values in the dictionary are str, and others are dict[str, object], so infers Collection[str], since this is indeed a common supertype of str and dict[str, object] according to the definitions in typeshed.

We already have many join-vs-union issues open, and I don't really think we need another, so I'm going to close this out. Follow #12056 for discussion on possibly changing mypy's behaviour here :)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

2 participants