Thanks to visit codestin.com
Credit goes to github.com

Skip to content

function is lacking a __signature__ attribute #13953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
leogermond opened this issue May 6, 2025 · 1 comment
Open

function is lacking a __signature__ attribute #13953

leogermond opened this issue May 6, 2025 · 1 comment

Comments

@leogermond
Copy link

leogermond commented May 6, 2025

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.

@srittau
Copy link
Collaborator

srittau commented May 6, 2025

Please note that builtins.function will be removed in the foreseeable future. Any changes should be made to types.FunctionType.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants