Skip to content

callable() fails to narrow type to Callable #20748

@cjwatson

Description

@cjwatson

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions