function is lacking a `__signature__` attribute
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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