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

Add support for generic classes #21

Open
chadrik opened this issue Aug 8, 2017 · 7 comments
Open

Add support for generic classes #21

chadrik opened this issue Aug 8, 2017 · 7 comments

Comments

@chadrik
Copy link

chadrik commented Aug 8, 2017

It would be great to be able to do runtime type checking of generic classes, e.g.:

from typing import Generic, TypeVar
T = TypeVar('T')

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

class IntA(A[int]):
    pass

a = IntA(7)  # good!
a = IntA('foo')  # bad!

I sketched out something like this already: https://gist.github.com/chadrik/54d7b3d051839f90983676a47368526b

@Stewori
Copy link

Stewori commented Aug 15, 2017

You mean

a = IntA(7)  # good!
a = IntA('foo')  # bad!

don't you?

@chadrik
Copy link
Author

chadrik commented Aug 15, 2017

d'oh, yes. lemme fix that.

@Stewori
Copy link

Stewori commented Aug 16, 2017

I would be interested to get this feature implemented in pytypes. Unfortunately I don't have time to work on this right now and it is not a trivial thing to implement.
Should the self-object be the carrier of T's value? Or the class? Does self carry T's value to supermethod?
I don't say these questions could not be properly and consistently answered. I just suggest to give it some thought, e.g. how would you suggest these calls to behave:

from typing import Generic, TypeVar
T = TypeVar('T')

class A(Generic[T]):
    @typechecked
    def method(self, obj: T):
        # whatever

class IntA(A[int]):
    def method(self, obj: T):
        # whatever

obj_IntA = IntA()
obj_IntA.method(7)  # good!
A.method(obj_IntA, 'text') # bad (?)
A.method(obj_IntA, 7) # good (?)
A[str].method(obj_IntA, 'text') # bad?
A[str].method(obj_IntA, 7) # bad?
A[int].method(obj_IntA, 7) # good?

@Stewori
Copy link

Stewori commented Oct 26, 2017

As of Stewori/pytypes@c1c207b the described functionality is provided by pytypes.

@chadrik
It would be good if you could check whether it works like you would expect.
I am going to file a new pytypes release soon, so now would be ideal time to make eventual adjustments.

@supersergiy
Copy link
Contributor

Bump on this -- @agronholm how feasible would it be to add support for generic classes?

@agronholm
Copy link
Owner

It could be, but likely won't be implemented any time soon. I have my hands full with the other features coming in v3.0, and then other projects that urgently require my attention.

@hoorelbeke-jimmy
Copy link

Hello @agronholm
Do you think you'll implement this feature in the future ?
I already use typeguard, which is awesome to encourage my team maintaining type hinting in our projects. But we are starting to use Generic types, and it's a shame that they aren't supported by typeguard
Thanks

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

No branches or pull requests

5 participants