python / python/mypy

Unexpected behavior with type narrowing and `Callable`

Open
#16,738 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

I can't really describe the bug well, but hopefully the gist below illustrates it. I'm attempting to go from a list of Callables that includes types (e.g. int, float, etc.) and narrow that to a list of types.

To Reproduce

https://mypy-play.net/?mypy=1.8.0&python=3.11&gist=05a1003c1414446f7196ec4c7c57ded7

Expected Behavior

The gist has no type errors.

Actual Behavior

main.py:5: error: List comprehension has incompatible type List[Callable[..., SupportsComplex]]; expected List[type]  [misc]
Found 1 error in 1 file (checked 1 source file)

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

Start with the linked mypy-play gist, using mypy 1.8.0 and Python 3.11, and reproduce the error reported at main.py:5. Trace the type narrowing involving Callable and type, then verify that the example produces no type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
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.