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

Variable-length tuple doesn't narrow in isinstance #15443

Closed
A5rocks opened this issue Jun 15, 2023 · 1 comment · Fixed by #15691
Closed

Variable-length tuple doesn't narrow in isinstance #15443

A5rocks opened this issue Jun 15, 2023 · 1 comment · Fixed by #15691
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@A5rocks
Copy link
Contributor

A5rocks commented Jun 15, 2023

Bug Report

This may be a duplicate but I couldn't find any.

See the title for a short description or the short reproduction attached.

To Reproduce

a: object
b: tuple[type[int], ...]

if isinstance(a, b):
    reveal_type(a)

Expected Behavior

main.py:5: note: Revealed type is "builtins.int"
Success: no issues found in 1 source file

Actual Behavior

main.py:5: note: Revealed type is "builtins.object"
Success: no issues found in 1 source file

This works, however:

a: object
b: tuple[type[int]]

if isinstance(a, b):
    reveal_type(a)

Your Environment

https://mypy-play.net/?mypy=latest&python=3.11&gist=050eb94523510217a3bf91e9977539d0

@A5rocks A5rocks added the bug mypy got something wrong label Jun 15, 2023
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Jun 15, 2023
@sobolevn
Copy link
Member

Yes, looks like a problem that can be easily fixed.

Here's a more real-life example that can illustrate the same problem:

class Base: ...

class Impl1(Base): ...
class Impl2(Base): ...

impls: tuple[type[Base], ...] = (Impl1, Impl2)
some: object

if isinstance(some, impls):
    reveal_type(some)  # must be `Base`

I have a fix ready :)

sobolevn added a commit that referenced this issue Jul 29, 2023
`flatten_types` forgot about the second way we represent `tuple` inside.

Closes #15443

---------

Co-authored-by: Ilya Priven <[email protected]>
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-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants