python / python/mypy

`callable()` fails to narrow type to `Callable`

Open
#20,748 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-type-narrowing
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

I'm sorry if this is a duplicate; it's possible it's covered by one of the existing issues about type narrowing, but I couldn't find anything that explicitly covers this case.

https://mypy.readthedocs.io/en/stable/type_narrowing.html#type-narrowing-expressions explicitly says that callable(obj) will narrow obj to a callable type. However, in my code this doesn't seem to work; the argument to callable is still left as Any.

To Reproduce

With mypy 1.19.1 and --strict:

from collections.abc import Callable

class A:
    def get_callback(self, name: str) -> Callable[[], bool]:
        callback = getattr(self, name)
        assert callable(callback)
        return callback

Also demonstrated in this playground.

Actual Behavior

t.py:7: error: Returning Any from function declared to return "Callable[[], bool]"  [no-any-return]

Your Environment

  • Mypy version used: 1.19.1
  • Mypy command-line flags: --strict
  • Python version used: 3.13

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 t.py reproducer under mypy 1.19.1 and --strict, then read the type-narrowing expressions documentation linked in the report, focusing on callable(obj). Trace how the callable assertion is handled and verify that the example no longer reports no-any-return while returning Callable[[], bool].

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.