2

I am trying to make a class object that is essentially a list of dictionaries formatted by a given TypedDict. The example Attendings class can be seen below:

from typing import TypedDict, List, get_args


class Database(List):

    def __init__(self, *args):
        super().__init__(args)
        self.name = self.__class__.__name__

    def add_entry(self, entry: dict, **kwargs) -> dict:
        for key, value in kwargs.items():
            entry[key] = value
        self.append(entry)
        return entry

    def search(self, lastest=True, **kwargs) -> dict:
        <method stuff>


class Attending(TypedDict):
    attending_username: str
    attending_email: str
    attending_phone: str


class Attendings(Database[Attending]):
    pass


attendings = Attendings()

This works in 3.9, but in 3.8 it gives me the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Jakda\pyfiles\class\repos\heart-rate-sentinel-server-richardson-reyes\database.py", line 51, in <module>
    class Attendings(Database[Attending]):
  File "C:\Users\Jakda\.conda\envs\serv\lib\typing.py", line 261, in inner
    return func(*args, **kwds)
  File "C:\Users\Jakda\.conda\envs\serv\lib\typing.py", line 897, in __class_getitem__
    _check_generic(cls, params)
  File "C:\Users\Jakda\.conda\envs\serv\lib\typing.py", line 211, in _check_generic
    raise TypeError(f"{cls} is not a generic class")
TypeError: <class 'database.Database'> is not a generic class 

I'm sort of new to classes so I don't really understand what is going on here. Is there a better way to write this? Why is this checking whether this is a generic class?

0 Answers0