Skip to content

Mypy subclass/intersection issue on 1.15.0 #18912

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
gandhis1 opened this issue Apr 12, 2025 · 2 comments
Closed

Mypy subclass/intersection issue on 1.15.0 #18912

gandhis1 opened this issue Apr 12, 2025 · 2 comments
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@gandhis1
Copy link

This code filters using an isinstance call to try to find objects that fit the profile of both Foo and Bar:

from abc import ABC
from collections.abc import Sequence

class Foo(ABC):
    pass

class Bar(ABC):
    pass

class Baz(ABC):
    pass

class FooBar(Foo, Bar):
    pass

class FooBaz(Foo, Baz):
    pass

def my_func(foos: list[Foo]) -> Sequence[Foo]:
    my_list = []
    for foo in foos:
        if not isinstance(foo, Bar):
            continue
        my_list.append(foo)
    return my_list

foos = [FooBar(), FooBaz()]
my_func(foos)

This code does not fail on Mypy 1.14.1:

https://mypy-play.net/?mypy=1.14.1&python=3.12&gist=7b5be66ab0fb3f04d15a99317ef21a7d

But it does fail on Mypy 1.15:

main.py:24: error: Argument 1 to "append" of "list" has incompatible type "__main__.<subclass of "Foo" and "Bar">1"; expected "__main__.<subclass of "Foo" and "Bar">"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

https://mypy-play.net/?mypy=1.15.0&python=3.12&gist=7b5be66ab0fb3f04d15a99317ef21a7d

Originally posted by @gandhis1 in python/typing#1968

@sterliakov sterliakov added bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder labels Apr 12, 2025
@sterliakov
Copy link
Collaborator

This is the problem discussed in #18433 - pyodide false positive caused by incompatible pseudo-intersections.

@gandhis1
Copy link
Author

I realized I did not check the master branch, and it appears this issue is fixed on master: https://mypy-play.net/?mypy=master&python=3.12&gist=7b5be66ab0fb3f04d15a99317ef21a7d

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

No branches or pull requests

2 participants