Skip to content

Typing for Filtered Iterables #2073

Closed
Closed
@ztnel

Description

@ztnel

I would expect type hints would be evaluated for iterables which are filtered by some conditional function:

li: List[Optional[Union[str]]] = ["hello", "test", None]
filtered_li = list(filter(lambda el: el is not None, li)) # type: List[str | None]

Specifically, because list comprehension provides correct type hinting:

filtered_li = [el for el in li if el is not None] # type: List[str]

I am suggesting we adopt a similar behavior for filter functions as is done for list comprehension. Open to discussion regarding the implementation semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions