generics

Large code in ThingsDB is running slow, how to increase performance?

Large code in ThingsDB is running slow, how to increase performance? Question: For testing purposes, I use a script which is rather long. It basically creates a workspace for every test with as name the test together with mock data (this is the large part). This is the idea of the query in python: await …

Total answers: 1

Getting the generic arguments of a subclass

Getting the generic arguments of a subclass Question: I have a generic base class and I want to be able to inspect the provided type for it. My approach was using typing.get_args which works like so: from typing import Generic, Tuple, TypeVarTuple, get_args T = TypeVarTuple("T") class Base(Generic[*T]): values: Tuple[*T] Example = Base[int, str] print(get_args(Example)) …

Total answers: 2

How can I handle 400 bad request error using DRF in Django

How can I handle 400 bad request error using DRF in Django Question: I am trying to perform a POST request using DRF in Django, the program is raising a 400 error (this is the error, Bad Request: /api/menu_items/, the frontend is raising the following error (This field is required) the problem is I cannot …

Total answers: 1

Python: make subclass of generic class generic by default

Python: make subclass of generic class generic by default Question: I want to be able to define what the contents of a subclass of a subclass of typing.Iterable have to be. Type hints are critical for this project so I have to find a working solution Here is a snip code of what I’ve already …

Total answers: 1

How to specify type for function parameter (Python)

How to specify type for function parameter (Python) Question: I want to restrict scope of functions that can be passed as parameter to another function. For example, to restrict functions to be only one from two specified, or from particular module, or by signature. I tried the code below but in it there is now …

Total answers: 3

Python generics: user defined generic in a callable

Python generics: user defined generic in a callable Question: I have the following setup: T = TypeVar("T") @dataclass class Type(Generic[T]): name: str data: T @dataclass class Aspect: name: str from: Type[str] to: Type[int] func: Callable[[Type[str]], Type[int]] This works great, but I’ve tied my Callable to str and int, and I would want something even more …

Total answers: 1

What's the correct way to check if an object is a typing.Generic?

What's the correct way to check if an object is a typing.Generic? Question: I’m trying to write code that validates type hints, and in order to do so I have to find out what kind of object the annotation is. For example, consider this snippet that’s supposed to tell the user what kind of value …

Total answers: 6

How to access the type arguments of typing.Generic?

How to access the type arguments of typing.Generic? Question: The typing module provides a base class for generic type hints: The typing.Generic class. Subclasses of Generic accept type arguments in square brackets, for example: list_of_ints = typing.List[int] str_to_bool_dict = typing.Dict[str, bool] My question is, how can I access these type arguments? That is, given str_to_bool_dict …

Total answers: 5

Generic many-to-many relationships

Generic many-to-many relationships Question: I’m trying to create a messaging system where a message’s sender and recipients can be generic entities. This seems fine for the sender, where there is only object to reference (GenericForeignKey) but I can’t figure out how to go about this for the recipients (GenericManyToManyKey ??) Below is a simplified example. …

Total answers: 3