Fix inheritance of generic types (PEP-560)
See original GitHub issueit 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:
- Created 3 years ago
- Reactions:5
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
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 ofObjectHandling.c
would be enough to add this mecanism in class building.