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

Skip to content

Custom abstract generic sub-classes #2291

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

Closed
chadrik opened this issue Oct 20, 2016 · 5 comments
Closed

Custom abstract generic sub-classes #2291

chadrik opened this issue Oct 20, 2016 · 5 comments

Comments

@chadrik
Copy link
Contributor

chadrik commented Oct 20, 2016

The second class definition fails type-checking with Invalid type "mymodule.T":

T = TypeVar('T')

class MyAbstractGeneric(Generic[T]):
    pass

class MyGeneric(MyAbstractGeneric[T]):
    pass

There's no documentation on creating custom abstract generic classes, and mypy and the typing tests have no examples of this either. The most obvious examples are those in typing itself, but it apparently uses techniques that are off limits outside of typing. I would love to know how to use multiple levels of inheritance to build up custom generics.

This is related to #606 but I did not want to derail the discussion there as my issue is more focused on creating generics rather than aliasing them. Maybe just user error.

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 20, 2016

You can write it like this:

T = TypeVar('T')

class MyAbstractGeneric(Generic[T]):
    pass

class MyGeneric(MyAbstractGeneric[T], Generic[T]):
    pass

You have to declare the type variables using a Generic[...] base class, even if another base class is generic. There is an open issue for fixing this (#302).

Would you like to contribute a fix (either a fix to #302 or a documentation update)?

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 20, 2016

Leaving this open as a missing documentation issue.

@chadrik
Copy link
Contributor Author

chadrik commented Oct 20, 2016

@JukkaL thanks for the fast response. I think I can make time for the documentation update, but I'll probably wait until I've answered a few more questions for myself.

@gvanrossum gvanrossum added this to the 0.4.x milestone Oct 20, 2016
@gvanrossum
Copy link
Member

Um, PEP 484 is explicit that this should be allowed:

The Generic[T] base class is redundant in simple cases where you
subclass some other generic class and specify type variables for its
parameters

  from typing import TypeVar, Iterator

  T = TypeVar('T')

  class MyIter(Iterator[T]):
      ...

That class definition is equivalent to:

  class MyIter(Iterator[T], Generic[T]):
      ...

So I rather think that we should fix this instead of documenting it. (Though I'm okay with the fix being scheduled for Milestone 0.5 and the docs updated temporarily to explain the issue.)

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 31, 2016

Yeah, I was aware of this -- #302 that I referred to above is about this.

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

3 participants