python / python/mypy

Difference in behaviour between overload and TypeVars when using functools.update_wrapper

Open
#17,196 3 comments 2 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

When calling functools.update_wrapper(f, g) where f has a type of TypeVar('T', Type1, Type2), mypy errors incorrectly, saying it expects Callable[[Never], Never] in that spot.

To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=8b79db2fe7eb042e15122536a393dd43

Expected Behavior

I think f1 and f2 are identical functions. I expect both checks to succeed (as they did on mypy 1.9.0), or possibly both to fail (but if this is an error, I don't see why...).

Actual Behavior

update_wrapper(f1, g) succeeds and update_wrapper(f2, g) errors, with the message Argument 1 to "update_wrapper" has incompatible type "Callable[[T], T]"; expected "Callable[[Never], Never]" [arg-type]

Your Environment

  • Mypy version used: 1.9.0 -> 1.10.0
  • Python version used: 3.10, 3.12

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 linked mypy-play reproduction and compare the overload and TypeVar cases around functools.update_wrapper across mypy 1.9.0 and 1.10.0. Trace the callable typing behavior that produces the reported Never expectation; done means the two equivalent checks have a consistent, intended result when the reproduction is rerun.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.