Skip to content

Mypy subclass/intersection issue on 1.15.0 #18912

Closed

Description

@gandhis1

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

Activity

  1. sterliakov commented on Apr 13, 2025

    @sterliakov
    Collaborator

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

  2. gandhis1 commented on Apr 14, 2025

    @gandhis1
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions