python / python/mypy

How to inherit argument types from a specific wrapped function

Open
#13,617 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

It would be great if there were a way to inherit the (argument) type signatures from one function to another. The use case here is something like:

def foo(
    something: str,
    *args: *requests.get.args,
    **kwargs: **requests.get.kwargs
):
    print(something)
    return requests.get(*args, **kwargs)

I've found https://github.com/python/mypy/issues/2003, though that was closed a while back. I'm not totally clear why that was closed (from the comments I can't work out what the solution was, nor if agreement was reached on an approach). Additionally a lot has changed since then with regards to argument typing -- notably ParamSpec, TypeVarTuple & Unpack now exist, though I'm not sure if any of them actually help here as the wrapped function is static rather than also being part of the call signature.

Ideally I'd like to be able to do this in cases where the original function is annotated, though without needing to change that function's annotations (e.g: because it's in a library), so using e.g: Unpack[TypedDict] would require me to repeat the definitions of the original function's signature, which is partly what I'm trying to avoid by using passthrough *args, **kwargs in the first place.

Aside: is "inherit" the right term for this? I wasn't sure what to search for.

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 reviewing the proposed syntax in this issue and the prior discussion in mypy issue #2003. Compare whether ParamSpec, TypeVarTuple, or Unpack address inheriting annotations from a static wrapped function such as requests.get; done would require an agreed design and documented type-checking behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.