python / python/mypy

False positive with disallow-any-explicit and `...` in `Callable` in bound of type var

Open
#9,496 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-2-low topic-calls topic-disallow-any
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I want a generic function type, but I don't want to get spurious Any errors on every usage.

from typing import Callable, TypeVar

Fn = TypeVar("Fn", bound=Callable[..., object])

mypy --disallow-any-explicit .

error: Explicit "Any" is not allowed
    Fn = TypeVar("Fn", bound=Callable[..., object])
    ^

I understand that this is actually usable as unsafe Any garbage:

def foo(fn: Fn) -> Fn:  # type safe usage
    fn(1)  # not type safe usage

So this should be the only place where the error appears.

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 issue with mypy --disallow-any-explicit on the shown TypeVar("Fn", bound=Callable[..., object]) example, then trace how explicit Any diagnostics handle ... in a Callable bound. Done means the bound is diagnosed as the intentional unsafe Any case without producing spurious errors at every use of Fn.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.