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

Type is not infered from default value of dict.get() #10890

Closed
qi55wyqu opened this issue Jul 29, 2021 · 5 comments
Closed

Type is not infered from default value of dict.get() #10890

qi55wyqu opened this issue Jul 29, 2021 · 5 comments
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference

Comments

@qi55wyqu
Copy link

Bug Report

When using dict.get(value, default) the returned type is not inferred correctly if the value was initially declared as Optional.

To Reproduce

mapping: Dict[str, str] = {"a": "x"}
val: Optional[str] = None
val = "a"
reveal_type(val) # Revealed type is "builtins.str"
val = mapping[val] if val in mapping else val
reveal_type(val) # Revealed type is "builtins.str"
val = mapping.get(val, val)
reveal_type(val) # Revealed type is "Union[builtins.str, None]"

Expected Behavior

Revealed type is builtins.str.

Actual Behavior

Revealed type is Union[builtins.str, None].

Your Environment

  • Mypy version used: mypy 0.910
  • Mypy command-line flags: --
  • Mypy configuration options from mypy.ini (and other config files): --
  • Python version used: 3.8.10
  • Operating system and version: Ubuntu 20.04.2
@qi55wyqu qi55wyqu added the bug mypy got something wrong label Jul 29, 2021
@JelleZijlstra
Copy link
Member

Isn't this because val is of type Optional[str]?

@erictraut
Copy link

The declared type of val is Optional[str], but at the time it is used in the expression mapping.get(val, val), its type has been narrowed to just str, so I would expect the return type of this call expression to be str in this case. That's the type pyright computes.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jul 29, 2021

This is another thing that stems from mypy's notion of "type context". If you use a new variable, you get the result you want, i.e.:

new_val = mapping.get(val, val)
reveal_type(new_val)

@qi55wyqu
Copy link
Author

mapping[v] if v in mapping else v

and

mapping.get(v, v)

are basically the same thing so I would expect mypy to infer the same type for the return value in both cases.

keszybz added a commit to keszybz/mkosi that referenced this issue Jul 30, 2021
It seems mypy doesn't understand the star expansion:
mkosi/__init__.py:6766:26: error: List or tuple expected as variable arguments  [misc]
        FIRMWARE_LOCATIONS = [
                             ^
Found 1 error in 1 file (checked 3 source files)

There's a similar bug open upstream (python/mypy#10890),
so I hope this will be fixed, and then this commit can be reverted.
@AlexWaygood AlexWaygood added the topic-type-context Type context / bidirectional inference label Mar 30, 2022
@AlexWaygood
Copy link
Member

Fixed in #14151

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-type-context Type context / bidirectional inference
Projects
None yet
Development

No branches or pull requests

5 participants