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 hints of tuple not used effectively when calling a function on its values #16119

Closed
exoriente opened this issue Sep 15, 2023 · 1 comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@exoriente
Copy link

Bug Report

mypy does not use the information is has about the types in a tuple effectively when iterating through the tuple's values. mypy does not allow a function that can handle every type in the tuple to be called on the tuples values. mypy will check only if the function supports the closest common parent class of all types in the tuple.

To Reproduce

class A:
    ...
    
class B:
    ...


def function(x: A | B) -> None:
    return

t: tuple[A, B] = (A(), B())

for x in t:
    function(x)

https://gist.github.com/mypy-play/671835ea899ca94ddd33150f155fbc39

Expected Behavior

We would expect mypy to approve the code. Logically function() can never be called with a value that conflicts with the type hints of its parameter x.

Actual Behavior

mypy exits with an error, even though the code is perfectly safe to run in terms of passing valid types with certainty.

main.py:14: error: Argument 1 to "function" has incompatible type "object"; expected "A | B"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

We used a mypy Playground set up with no defaults changed:

  • Mypy version used: 1.5.1 (latest)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11
@exoriente exoriente added the bug mypy got something wrong label Sep 15, 2023
@JelleZijlstra JelleZijlstra added the topic-join-v-union Using join vs. using unions label Sep 15, 2023
@hauntsaninja
Copy link
Collaborator

Fixed by #17408

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

3 participants