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

Fix type inference in pattern matching by positional argument #13618

Merged
merged 2 commits into from
Sep 7, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented 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:

mypy/mypy/expandtype.py

Lines 144 to 148 in 88aed94

repl = self.variables.get(t.id, t)
if isinstance(repl, ProperType) and isinstance(repl, Instance):
# TODO: do we really need to do this?
# If I try to remove this special-casing ~40 tests fail on reveal_type().
return repl.copy_modified(last_known_value=None)

Let's see what will happen.

Closes #13612

@sobolevn sobolevn requested review from jhance and ilevkivskyi and removed request for jhance September 7, 2022 13:19
@github-actions

This comment has been minimized.

@sobolevn
Copy link
Member Author

sobolevn commented Sep 7, 2022

I am quite surprised!

@ariebovenberg
Copy link

ariebovenberg commented Sep 7, 2022

@sobolevn a good surprise indeed! Tests look comprehensive enough.

I wonder, does it also run correctly with inheritance? (Note the extra a: Base[str] = ... alias)

class Base(Generic[T]):
    ...

@dataclass  # problem also occurs with non-dataclass equivalent
class A(Base[T]):
    x: T


a: Base[str] = A("foo")
reveal_type(a)  # Base[str] (correct)

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

It might be a good addition to the tests, because inheritance here breaks dataclass which did work without it.

@sobolevn
Copy link
Member Author

sobolevn commented Sep 7, 2022

I will add a test case for subclasses and dataclasses. Thanks!

@sobolevn
Copy link
Member Author

sobolevn commented Sep 7, 2022

Ok, this indeed does not work for Generic subclasses. But, I belive that it is something different.

Let's open a new issue for it. Can you, @ariebovenberg? 🙂

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@sobolevn sobolevn merged commit b031f1c into python:master Sep 7, 2022
tmke8 added a commit to tmke8/mypy that referenced this pull request Sep 7, 2022
@sobolevn
Copy link
Member Author

sobolevn commented Sep 8, 2022

@thomkeh hi, can you please share any context on your commit? Is there something wrong?

@tmke8
Copy link
Contributor

tmke8 commented Sep 8, 2022

Oh sorry, I clicked the "revert" button accidentally! I didn't know this was possible.

Everything is fine as far as I know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot deduce type of generic attributes within match statements
4 participants