Category : type-hinting

Let’s say I have the following Django models: class Toolbox(models.Model): name = models.CharField(max_length=255) tools = models.ManyToManyField("Tool") class Tool(models.Model): class Size(models.TextChoices): SMALL = "S" MEDIUM = "M" LARGE = "L" name = models.CharField(max_length=255) size = models.CharField(max_length=10, choices=Size.choices) I have a function to get all small tools for each toolbox. The @queries_disabled() comes from django-zen-queries to ensure ..

Read more

I have a set of classes, Lets call them Foo and Bar, where both inherit from a base class Father that is defined outside of the current scope (not by me). I have definied a protocol class DummyProtocol that has a function do_something. class DummyProtocol(Protocol): def do_something(self): … class Foo(Father): def do_something(self): pass class Bar(Father): ..

Read more

Normally, if I want type checking (in IDE) to work the way I expect with methods that have multiple return types, I would use Python’s typing.overload decorator. Something like this: from typing import overload, Tuple, Literal class myclass: def __init__ (self, param1: int) -> None: self.member1 = param1 @overload def gimmie(self, param1: Tuple[str,…], param2: Literal[True]) ..

Read more

I have a class that looks as following: from typing import Optional, Tuple import numpy as np class TestClass(): def __init__(self, a: Optional[float] = None, b: Optional[float] = None): self.a = 1 if a is None else a self.b = b @property def parameters(self) -> Tuple: return (self.a, self.b) @property def test_property(self) -> np.ndarray: if ..

Read more