Skip to content

Unexpected "incompatible type" error when chaining bytes(filter()) #12247

Closed

Description

@ion201

Bug Report

Mypy reports a strange type error when calling bytes(filter(...)) like below.

Write the following contents to a file and run mypy:

f = filter(lambda ch: chr(ch), b"hello")
bytes(f)

bytes(filter(lambda ch: chr(ch), b"hello"))

Mypy reports an error on line 4 but not on lines 1/2, despite the two sections being functionally identical. The expected result is that there should be no errors in this code example.

> mypy /tmp/test.py
/tmp/test.py:4: error: Argument 1 to "chr" has incompatible type "SupportsIndex"; expected "int"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.931
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: python 3.10.2
  • Operating system and version: Arch linux and windows 10

Activity

  1. ion201 commented on Feb 23, 2022

    @ion201
    Author

    After rolling back mypy on windows, this error first appears in v0.920

  2. armoha commented on Dec 31, 2022

    @armoha

    The issue still exists with mypy 0.991, Python 3.11.1 and Windows 10.

    def IgnoreColor(s: bytes) -> bytes:
        bytes(filter(lambda x: not (0x01 <= x <= 0x1F or x == 0x7F), s))
    error: Argument 1 to "filter" has incompatible type "Callable[[Any], bool]"; expected "Callable[[int], TypeGuard[SupportsIndex]]"  [arg-type]
    error: Unsupported operand types for >= ("int" and "SupportsIndex")  [operator]
    error: Unsupported operand types for <= ("int" and "SupportsIndex")  [operator]
    
  3. randolf-scholz commented on Nov 4, 2026

    @randolf-scholz
    Contributor

    It seems this is fixed since version 1.16

  4. ion201 commented on Nov 4, 2026

    @ion201
    Author

    Confirmed this is fixed

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-contextType context / bidirectional inference

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions