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

Cannot deduce type of generic attributes within match statements #13612

Closed
ariebovenberg opened this issue Sep 5, 2022 · 2 comments · Fixed by #13618
Closed

Cannot deduce type of generic attributes within match statements #13612

ariebovenberg opened this issue Sep 5, 2022 · 2 comments · Fixed by #13618
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder

Comments

@ariebovenberg
Copy link

Bug Report

It seems like mypy is having trouble deducing the type of generic attributes in match statements. If __match_args__ contains a generic attribute, this attribute becomes Any after case matching.

To Reproduce

Run mypy on the code below

from typing import Generic, TypeVar

T = TypeVar("T")


class A(Generic[T]):
    x: T

    __match_args__ = ("x",)

    def __init__(self, x: T):
        self.x = x


a = A("foo")
reveal_type(a)  # A[str] (correct)
reveal_type(a.x)  # builtins.str (correct)

match a:
    case A(b):
        reveal_type(b)  # Any (incorrect! Should be builtins.str)

Expected Behavior

The total output should be:

Revealed type is "mycode.A[builtins.str]"
Revealed type is "builtins.str"
Revealed type is "builtins.str"

Actual Behavior

the last line doesn't reveal builtins.str

Revealed type is "mycode.A[builtins.str]"
Revealed type is "builtins.str"
Revealed type is "Any"

Your Environment

  • Mypy version used: 0.971 (also occurs on master branch)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.5
  • Operating system and version: MacOS 12.5.1

What I've found so far

@ariebovenberg ariebovenberg added the bug mypy got something wrong label Sep 5, 2022
@AlexWaygood AlexWaygood added topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder labels Sep 5, 2022
@sobolevn
Copy link
Member

sobolevn commented Sep 5, 2022

Hi, @ariebovenberg 👋

Confession: I've never used a match statement in Python.
But, I will try to take a look (with no promises)

@sobolevn
Copy link
Member

sobolevn commented Sep 7, 2022

@ariebovenberg reviews are welcome (at least of tests!) :)

sobolevn added a commit that referenced this issue Sep 7, 2022
Oh, this was very interesting.
During the debug sessions I learned a lot about how pattern matching and
its analysis do work.

But, the problem was that `expand_type` did not preserve
`.last_known_value` for some reason.
I used `.copy_modified` to preserve everything we know about `Instance`.

However, I expect that some tests might fail now. This code even has
something similar in `TODO` some lines above:

https://github.com/python/mypy/blob/88aed94ae3de2542491f6cd65d1236c4f0cdedb1/mypy/expandtype.py#L144-L148

Let's see what will happen.

Closes #13612
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-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants