Unterklasse von Generic schlägt mit AttributeError fehl: Objekt hat kein Attribut „__parameters__“ – Verwendung von GenePython

Python-Programme
Anonymous
 Unterklasse von Generic schlägt mit AttributeError fehl: Objekt hat kein Attribut „__parameters__“ – Verwendung von Gene

Post by Anonymous »

Ich habe ein Setup wie das folgende

Code: Select all

from typing import Generic, TypeVar
T = TypeVar("T")

class ThirdParty:
def __init_subclass__(cls):
...  # does not call super()

class Mine(ThirdParty, Generic[T]):
...

class Sub(Mine[int]):
...
Es schlägt jedoch mit dem folgenden fehl

Code: Select all

Traceback (most recent call last):
File "", line 1, in 
File "/home/dsperber/master/repos/SYMPOL/rllib_port/core/stated_flax_model.py", line 27, in 
class Sub(Mine[ModelType]):
File "/home/xxxx/miniconda3/envs/python3.10/lib/python3.10/typing.py", line 309, in inner
return cached(*args, **kwds)
File "/home/xxxx/miniconda3/envs/python3.10/lib/python3.10/typing.py", line 1342, in __class_getitem__
if any(isinstance(t, ParamSpec) for t in cls.__parameters__):

AttributeError: type object 'Mine' has no attribute '__parameters__'
Ich habe herausgefunden, dass das daran liegt, dass ThirdParty eine __init_subclass__-Funktion hat, aber super().__init_subclass__ nicht aufruft, daher wird Generic.__init_subclass__ nicht aufgerufen.
Gibt es einen Weg, dies zu umgehen? Das Hinzufügen von __init_subclass__ zu Mine funktioniert nicht oder Folgendes schlägt ebenfalls fehl:

Code: Select all

def __init_subclass__(cls):
Generic.__init_subclass__(cls)
# TypeError: Generic.__init_subclass__() takes 1 positional argument but 2 were given

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post