python / python/mypy

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

Open
#12,247 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-context
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report by writing the shown examples to /tmp/test.py and running mypy with no flags. Compare the inferred types for the split and chained forms, then trace the relevant type-checking path; done means both forms produce no errors and the regression is covered by an appropriate test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.