Skip to content
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

__init__ signature is not preserved for Generic classes #67

Open
smarie opened this issue Feb 12, 2018 · 1 comment
Open

__init__ signature is not preserved for Generic classes #67

smarie opened this issue Feb 12, 2018 · 1 comment

Comments

@smarie
Copy link

smarie commented Feb 12, 2018

from inspect import signature

from enforce import runtime_validation
from typing import Generic, TypeVar


T = TypeVar('T')


class A(Generic[T]):
    def __init__(self, foo: T):
        self.foo = foo


A(foo=1)
s = signature(A.__init__)
print(s.parameters)

A[int](foo=1)
s = signature(A[int].__init__)
print(s.parameters)


@runtime_validation
class A(Generic[T]):
    def __init__(self, foo: T):
        self.foo = foo


A(foo=1)
s = signature(A.__init__)
print(s.parameters)

A[int](foo=1)
s = signature(A[int].__init__)
print(s.parameters)

yields

OrderedDict([('self', <Parameter "self">), ('foo', <Parameter "foo:~T">)])
OrderedDict([('self', <Parameter "self">), ('foo', <Parameter "foo:~T">)])
OrderedDict([('wrapped', <Parameter "wrapped">), ('settings', <Parameter "settings=None">)])
OrderedDict([('wrapped', <Parameter "wrapped">), ('settings', <Parameter "settings=None">)])

This prevents type constructor introspection, for example with the parsyfiles parsing library.

@smarie
Copy link
Author

smarie commented Feb 12, 2018

I had a look in the code: in the case of generics, it seems that you are proxying the whole class with GenericProxy(ObjectProxy), which seems overkill: applying runtime_validation on the class as usual and simply replacing the __getitem__ class method so that it applies it on generated classes should be sufficient. Or did I miss some important functionality that is specific to Generic classes ?

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

1 participant