Skip to content

Regression: missing output from reveal_type #15954

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
eltoder opened this issue Aug 24, 2023 · 5 comments
Closed

Regression: missing output from reveal_type #15954

eltoder opened this issue Aug 24, 2023 · 5 comments
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@eltoder
Copy link

eltoder commented Aug 24, 2023

Apologies if this was already reported. reveal_type is a hard thing to search for.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=2a1d6a831177eb02b175a25fc07a374e

class A:
    x: int

class B:
    x: float

class C(A, B):
    x: int

c: A = C()
assert isinstance(c, B)
reveal_type(c)
reveal_type(c.x)

Expected Behavior

I expect to see 2 lines of output from reveal_type. This works in 1.4.x but stopped working in 1.5.x.

Actual Behavior

No output.

Your Environment

  • Mypy version used: 1.5.1
  • Python version used: 3.11
@eltoder eltoder added the bug mypy got something wrong label Aug 24, 2023
@AlexWaygood AlexWaygood added the topic-reachability Detecting unreachable code label Aug 24, 2023
@AlexWaygood
Copy link
Member

AlexWaygood commented Aug 24, 2023

This doesn't actually have anything to do with reveal_type: the reason why mypy doesn't output anything for those reveal_type calls is just because mypy doesn't analyse any blocks of code it has determined are unreachable, and it has (erroneously) determined here that all code below the assert isinstance(c, B) line is unreachable. If you add the --warn-unreachable flag to the mypy config, you can see this clearly: https://mypy-play.net/?mypy=latest&python=3.11&gist=617de6102ded35d1eaa9aa0c22142b43&flags=warn-unreachable

FWIW, it's a deliberate design decision for mypy to ignore all code it considers unreachable, but I agree that it can lead to some pretty unintuitive and unexpected behaviour at times.

@eltoder
Copy link
Author

eltoder commented Aug 24, 2023

@AlexWaygood thank you for the explanation. In that case, the question is whether isinstance() should be considered always false. If there's already an issue about this, feel free to close this as a duplicate.

@AlexWaygood
Copy link
Member

Yeah, it's a ~known issue that mypy struggles a little bit with multiple inheritance. See e.g. #3603 and #5529

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Aug 24, 2023
@eltoder
Copy link
Author

eltoder commented Aug 24, 2023

@AlexWaygood both of these issues are pretty old. The change here happened between 1.4 and 1.5, so this is something new.

@AlexWaygood
Copy link
Member

AlexWaygood commented Aug 24, 2023

@ikonst recently improved the consistency of whether or not mypy will actually ignore all blocks of code that are deemed unreachable or not. So the recent change in behaviour is likely due to that. But that's a strict improvement over the previous behaviour since, as I say, by design, mypy is currently meant to ignore everything in a block of code it considers unreachable.

The reachability analysis hasn't changed (mypy considered the block of code at the bottom unreachable in v1.4.x as well); the only difference is that mypy is now more consistent about how it treats unreachable code, which is a good thing.

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-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

2 participants