Category : protocols

from typing_extensions import Protocol class IFoo(Protocol): value: int class Foo(IFoo): @property def value(self) -> int: return 2 _value: int @value.setter def value(self, value: int): self._value = value Pylance in strict mode(basic mode doesn’t) is giving an error at the getter and the setter saying that: "value" overrides symbol of the same name in class "IFoo" ..

Read more

I am trying to write a callback protocol that is a supertype of all functions that have a return type ‘str’ and also are able to accept calls with arguments like (first_arg=some_int, second_arg=some_string, third_arg=some_float). The only requirement for functions in this family is that calls that conform to this pattern must be valid. (Some of ..

Read more

My question is simple. I have this protocol: class LauncherTemplateConfig(Protocol): … def launch_program_cmd(self, **kwargs) -> list[str]: pass … And this implementation of the protocol, which I would expect mypy passes, but it does not: class MyLauncherTemplateConfig: … def launch_program_cmd( self, some_arg: Optional[Path] = None, another_arg=1 ) -> list[str]: … I would expect the parameters in ..

Read more

Let’s say I have PEP-544 protocol called Summable : class Summable(Protocol): total_amount: Decimal And I have model Item that implements the Protocol class Item(Summable, models.Model): discount = models.DecimalField( decimal_places=2, validators=[MaxValueValidator(1)], default=Decimal(‘0.00’), max_digits=10 ) price = models.DecimalField( decimal_places=4, validators=[MinValueValidator(0)], max_digits=10 ) @property def total_amount(self): return sel.price – self.price * self.discount class Meta: ordering = [‘id’] I ..

Read more