Skip to content

pytypes does not preserve the methods' signatures #30

Open
@smarie

Description

@smarie
from pytypes import typechecked
from inspect import signature

class Foo:
    def __init__(self, foo: str = 'hello'):
        self.foo = foo

s = signature(Foo.__init__)
print(s.parameters)
# > OrderedDict([('self', <Parameter "self">), ('foo', <Parameter "foo:str='hello'">)])
# parameter names, type hints and default values are preserved

Whereas

@typechecked
class Foo:
    def __init__(self, foo: str):
        self.foo = foo

s = signature(Foo.__init__)
print(s.parameters)
# > OrderedDict([('args', <Parameter "*args">), ('kw', <Parameter "**kw">)])
# everything has been removed ! 

This is annoying as it does not allow other libraries to automatically discover what is required to build an object. For example with parsyfiles.

The solution is to use a signature-preserving decorator library in pytypes, I personally use decorator but you can also use wrapt I think.

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