Closed
Description
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