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

Inheritance issues with generic collections #202

Closed
bintoro opened this issue Apr 16, 2016 · 4 comments
Closed

Inheritance issues with generic collections #202

bintoro opened this issue Apr 16, 2016 · 4 comments

Comments

@bintoro
Copy link
Contributor

bintoro commented Apr 16, 2016

I have a problem with how __extra__ is currently inherited:

>>> class CustomMapping(typing.Mapping): ...
... 
>>> issubclass(dict, CustomMapping)
True
>>> class CustomDict(typing.Dict): ...
... 
>>> issubclass(dict, CustomDict)
True

This can't be right.

My initial impression is that __extra__ should only be propagated on parameterization, not when subclassing, and that the concrete types List, Dict, etc. should have their own extras (list, dict, and so on). Thoughts? A tentative patch is here.

Another thing: The module distributed on PyPI still has the issue where a generic doesn't include itself in the subtype's MRO during parameterization. This was fixed for Python 3.5.1 in aab2c59. It would be great if we could get a more recent revision on PyPI as well.

3.5.1:

>>> issubclass(Iterable[int], Iterable)
True

3.5.0:

>>> issubclass(Iterable[int], Iterable)
False

And finally, a question: The collections ABC replacements do not provide the abstract (or any other) methods of the originals. In particular, Iterable is technically not abstract at all. Is this intentional or just a temporary state of affairs?

@gvanrossum
Copy link
Member

  • I'll look over your patch.
  • I'll see if I can release a 3.5.1 version -- it looks like 3.5.2 is still a while away.
  • It's probably an oversight that Iterable isn't abstract; or perhaps there was a technical issue.

@gvanrossum
Copy link
Member

gvanrossum commented Apr 16, 2016 via email

@bintoro
Copy link
Contributor Author

bintoro commented Apr 16, 2016

Updated the typing gpackage to 3.5.1.

Thanks!

As for the abstract methods, adding __iter__ to Iterable seems to have no ill effects.

Container, Awaitable, and AsyncIterable and their derivatives are also affected — in other words everything that doesn't inherit from Sized or Hashable.

One option is to just copy everything over during class construction:

 class GenericMeta(TypingMeta, abc.ABCMeta):

     def __new__(cls, name, bases, namespace,
                 tvars=None, args=None, origin=None, extra=None):
+        if extra and origin is None:
+            for methodname in getattr(extra, '__abstractmethods__', ()):
+                if methodname in extra.__dict__:
+                    namespace.setdefault(methodname, extra.__dict__[methodname])
+
         self = super().__new__(cls, name, bases, namespace, _root=True)

@gvanrossum
Copy link
Member

I would like to go slow here. I have other priorities (see #203 (comment)). I will eventually get to this but I'm not sure I want to rush anything into 3.5.2 beyond what's already on deck.

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

Successfully merging a pull request may close this issue.

2 participants