Skip to content

filtering with isinstance doesn't narrow down types #6847

Closed
@YtvwlD

Description

@YtvwlD

I had about this code (this is a small example):

from typing import List, Union

class TypeA: pass
class TypeB: pass

Types = Union[TypeA, TypeB]
t: List[Types] = [TypeA(), TypeB()]
a: List[TypeA] = list(filter(lambda x: isinstance(x, TypeA), t))

And I got the following message (both with my distribution's mypy 0.560 and with the current master):

error: Argument 2 to "filter" has incompatible type "List[Union[TypeA, TypeB]]"; expected "Iterable[TypeA]"

When I remove the type after a, it treats a as List[Types].

I get that this might be difficult, but it would be nice if it worked.

This could be related to #476, I'm not sure.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions