question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fix inheritance of generic types (PEP-560)

See original GitHub issue

it seems like #3518 did not entirely fix this issue – there will be an error if you try to inherit from the subscripted class.

For example, with the following in example.py, and building the same way as above:

from typing import Generic, TypeVar

T = TypeVar("T")

print(Generic[T])

class MyGeneric(Generic[T]):
  pass

will give:

>>> import example
typing.Generic[~T]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example.py", line 7, in init example
    class MyGeneric(Generic[T]):
TypeError: __init__() takes 3 positional arguments but 4 were given

_Originally posted by @nikhilmishra000 in https://github.com/cython/cython/issues/2753#issuecomment-616874842_

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
da-woodscommented, Sep 17, 2020

@nekokatt As said above, this is most likely a change to the “Py3ClassCreate” utility code: https://github.com/cython/cython/blob/aa8b0d9b89030c0b9ace5698ae5131c623acefa4/Cython/Utility/ObjectHandling.c#L1034 (I’ve linked to the broad section). This C code is largely just copied into each generated Cython file.

We currently don’t handle __mro_entries__ according to PEP-560 so it’s largely a case of working out what stage that should be called at and calling it.

The change will then need some tests, which are probably best done by copying the cpython tests as much as possible. We already have the equivalent file but the MRO tests were omitted since they weren’t yet supported.

1reaction
wyfocommented, Jul 24, 2020

I’ve started to investigate some weeks ago, and I found that this issue was cause by the lack of handling for __mro_entries__ (I would have won some time by better reading https://github.com/cython/cython/issues/2753#issuecomment-616928891) In fact, in Cython, there is no equivalent of __build_class__, where __mro_entries__ is evaluated. That’s why i’m not sure @scoder that a modification of ObjectHandling.c would be enough to add this mecanism in class building.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 560 – Core support for typing module and generic types
abc inheritance chain in typing . Instantiation of generic classes will be faster (this is minor however). Metaclass conflicts. All generic types are...
Read more >
Given PEP 560, why do generic types still use a metaclass in ...
Given PEP 560, why do generic types still use a metaclass in Python? In [8]: from typing import Generic, TypeVar In [10]: type(Generic[T]) ......
Read more >
Generics, Inheritance, and Subtypes (The Java™ Tutorials ...
For information on how to create a subtype-like relationship between two generic classes when the type parameters are related, see Wildcards and Subtyping....
Read more >
Classes that are generic or contained in a generic type cannot ...
To correct this error. Change the base class to something other than an attribute class, or remove the Inherits statement entirely. See also....
Read more >
java - Designing generic type inheritance hierarchy
Primarily I feel that I am doing it right, its just that CommonSpeedPostageProcessor needs to be parameterized with generic type parameter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found