python / python/typeshed

function is lacking a `__signature__` attribute

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

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

PEP-362 specifies that in order to cache the signature of any function, it is possible for the user to set its __signature__ attribute.

Actual

This attribute is not set in the builtins module over at https://github.com/python/typeshed/blob/main/stdlib/builtins.pyi#L1009

As a result a typechecker might reject the following piece of code

from inspect import signature


def main():

    def f():
        pass

    f.__signature__ = signature(f)


if __name__ == "__main__":
    main()

e.g. with pyright I have a false positive

/Users/leogermond/Lab/python/signature/main.py
  /Users/leogermond/Lab/python/signature/main.py:9:7 - error: Cannot assign to attribute "__signature__" for class "function"
    Attribute "__signature__" is unknown (reportFunctionMemberAccess)
1 error, 0 warnings, 0 informations

Expected

I would the __signature__ attribute to be specified in the function class (with a special marker indicating it's optional?) of the builtins module. A type checker such as pyright could then infer that setting this attribute is possible and accept the piece of code given in example.

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 at stdlib/builtins.pyi around line 1009 and inspect how the function class is declared. Compare the requested optional signature support with PEP 362 and the provided assignment example, then verify that a type checker such as pyright accepts it when the stub change is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.