python / python/mypy

mypy does not narrow between Generic type[_T] and Callable[[Any], _T]

Open
#20,395 0 comments 0 reactions 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

MyPy doesn't recognize that isinstance(type_var, type) and isinstance(another_var, type_var) narrows another_var to whatever type_var is.

To Reproduce

class RemoteFunction(Generic[_T]):
    def __init__(self, return_type: type[_T] | Callable[[Any], _T]):
        self._return_type = return_type

    def _validate(self, response: Any) -> _T:
        if isinstance(self._return_type, type):
            reveal_type(self._return_type)
            # Revealed type is "builtins.type" Mypy
            # Type of "self._return_type" is "type[object] | type" Pylance
            if not isinstance(response, self._return_type):
                raise TypeError
            reveal_type(response)
            # Revealed type is "builtins.object"Mypy
            # Type of "response" is "Any" Pylance
            return response # Incompatible return value type (got "object", expected "_T")

        return self._return_type(response)

    def __call__(self) -> _T:
        remote_response: Any = None # actually from network/db/etc
        return self._validate(remote_response)

# Usage example
my_remote_func = RemoteFunction(int)
response = my_remote_func()
reveal_type(response)
# Type of "response" is "int" Pylance
# Revealed type is "builtins.int" Mypy

Expected Behavior

mypy should correctly type response as _T

Actual Behavior

test333.py: note: In member "_validate" of class "RemoteFunction":
test333.py:20: note: Revealed type is "builtins.type"
test333.py:25: note: Revealed type is "builtins.object"
test333.py:28: error: Incompatible return value type (got "object", expected "_T")  [return-value]
                return response # Incompatible return value type (got "object", expected "_T")

Interestingly, pyright shows similar types for reveal_type, as annotated above, but does not give an error. Both correctly infer the type of the response in the outer scope, presumably because of the __call__ return type annotation.

Your Environment

  • Mypy version used: 1.19.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.4

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 by reproducing the RemoteFunction example with mypy --strict, focusing on _validate, the isinstance checks, and the reveal_type results. Trace how generic type variables are narrowed when the union contains type[_T] and Callable[[Any], _T]. Done means the example returns response as _T without the reported incompatible return error while preserving the outer call inference.

Written by the indexing model from the issue text.

Assessment

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