Skip to content

function is lacking a __signature__ attribute #13953

Open
@leogermond

Description

@leogermond

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions