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

Describe generic classes as base classes #41

Closed
JukkaL opened this issue Jan 15, 2015 · 7 comments
Closed

Describe generic classes as base classes #41

JukkaL opened this issue Jan 15, 2015 · 7 comments

Comments

@JukkaL
Copy link
Contributor

JukkaL commented Jan 15, 2015

The PEP should probably mention what happens if a generic class is used as a base class.

Examples to consider:

class StrList(List[str]): ...  # StrList is not generic

T = TypeVar('T')

class MyList(List[T]): ...   # Mypy requires Generic[T] as a base class, but it's ugly

x = Undefined(MyList[int])
@gvanrossum
Copy link
Member

Yes, the allowed forms for base classes and the idioms to use for various
cases are not yet sorted out. I also don't like that mypy has both
Generic[T] and AbstractGeneric[T].

On Wed, Jan 14, 2015 at 11:09 PM, Jukka Lehtosalo notifications@github.com
wrote:

The PEP should probably mention what happens if a generic class is used as
a base class.

Examples to consider:

class StrList(List[str]): ... # StrList is not generic

T = TypeVar('T')

class MyList(List[T]): ... # Mypy requires Generic[T] as a base class, but it's ugly

x = Undefined(MyList[int])


Reply to this email directly or view it on GitHub
#41.

--Guido van Rossum (python.org/~guido)

@vlasovskikh
Copy link
Member

See also https://github.com/JukkaL/typing/issues/36 about Generic and AbstractGeneric.

@JukkaL
Copy link
Contributor Author

JukkaL commented Jan 17, 2015

Currently this is not valid (due to Python semantics):

class A(metaclass=ABCMeta): pass
class B(Generic[T], A): pass   # metaclass conflict

This is okay, however:

class A(metaclass=ABCMeta): pass
class B(AbstractGeneric[T], A): pass   # good

Maybe we should just replace Generic with AbstractGeneric and remove AbstractGeneric? This way most reasonable inheritance patterns should work okay. With the current mypy approach, inheritance hierarchies are inherently fragile -- if some base class adds ABCMeta as metaclass (due to adding an abstract base class, for example), it can break subclasses that are generic.

@gvanrossum
Copy link
Member

So, what shall we do? If at all possible I'd like to only have Generic.

@JukkaL
Copy link
Contributor Author

JukkaL commented Feb 8, 2015

I updated mypy and typing to only have Generic, which also supports defining ABCs. It seems to be okay to me, but I need to do more testing.

@JukkaL
Copy link
Contributor Author

JukkaL commented Apr 14, 2015

Added section 'Arbitrary generic types as base classes' to the PEP. Not closing this issue this yet until somebody has reviewed it.

@vlasovskikh
Copy link
Member

Everything looks good to me, except for #85.

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

3 participants