Skip to content

filter() function overrides item type #3428

Description

@TTsangSC

Summary

Example

(Playground: https://play.ty.dev/d92d2eb5-5007-4bc5-954c-be5018137ab6)

from collections.abc import Iterable


def check(obj: str | bytes) -> bool:
    return True  # TODO


iter_strings: Iterable[str] = iter('foobarbaz')
iter_strings = filter(check, iter_strings)

Expected behavior

No errors.

Actual behavior

Object of type `filter[str | bytes]` is not assignable to `Iterable[str]` (invalid-assignment) [Ln 9, Col 16]

Notes

I suspect that this has to do with #2799 and #2572, in that type info of iterable items is lost/overridden after being passed to a callable which takes another callable operating on said items. #1970 also reported pathological behaviors with filter() and map(), but the failure pattern was different there.

Version

ty 0.0.34 (d00448e 2026-05-01)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions