python / python/mypy

New type inference does not support using singledispatch to decorate a function in which the first argument is a union indexed by a constrained type variable that also appears in the return type

Open
#16,531 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

Apologies for the wordy title, but this issue appears to be somewhat of a corner case.
In some unpublished code, I have a singledispatch function that doesn't pass the new type inference code.
It appears that the problem is that it's going from a union which depends on a constrained typevar to a type that also depends on that typevar.
If it's not a union, not constrained, or doesn't appear in the return type, it passes fine.

To Reproduce

from functools import singledispatch, singledispatchmethod
from typing import AnyStr, TypeVar


T = TypeVar("T")


@singledispatch
def func(arg: list[AnyStr] | None) -> AnyStr:
    raise NotImplementedError

@singledispatch
def func_no_union(arg: list[AnyStr]) -> AnyStr:
    raise NotImplementedError

@singledispatch
def func_no_return(arg: list[AnyStr] | None) -> None:
    raise NotImplementedError

@singledispatch
def func_unconstrained(arg: list[T] | None) -> T:
    raise NotImplementedError

https://mypy-play.net/?mypy=master&python=3.11&gist=3bd3f48cd0e1aca5dcdd03cfd44d3d3e

(The use of list does not trigger the bug, but I figured it'd make things less ambiguous in the unconstrained case. Also, it can be any union. The union that triggered this issue in the first place did not include None. Also, singledispatchmethod on a similarly-typed method will also trigger the issue.)

Actual Behavior

Passes successfully pre-1.7.0 so long as --new-type-inference is not passed.

Otherwise, decorating func fails. All others succeed.

main.py:8: error: Argument 1 to "singledispatch" has incompatible type "Callable[[list[AnyStr] | None], AnyStr]"; expected "Callable[..., Never]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.7.0 and master (failed), 1.6.1 (passed with old inference)
  • Python version used: 3.11

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 running the supplied reproducer with --new-type-inference and compare the failing singledispatch case with the three cases that pass. Trace the new type-inference and singledispatch handling, then verify that the union, constrained type variable, return-type case and analogous singledispatchmethod case type-check without regressing the controls.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.