I want to create a list with any ServiceManager (within generic bounds).
First I tried using the base type in my list, but I get errors with invariance.
Then I tried using Any, but it made the type system too lax, and no longer catching errors.
Do you have any suggestions?
Thanks in advance 😄
Example:
from dataclasses import dataclass from typing import Any # service definition @dataclass class ServiceState: pass class Service[SS: ServiceState]: def __init__(self, state: SS): self.state = state def get_state(self) -> SS: return self.state # create services @dataclass class PizzaServiceState(ServiceState): topping: str sauce: str class PizzaService(Service[PizzaServiceState]): def __init__(self, state: PizzaServiceState): super().__init__(state) @dataclass class IceCreamServiceState(ServiceState): flavor: str topping: str class IceCreamService(Service[IceCreamServiceState]): def __init__(self, state: IceCreamServiceState): super().__init__(state) # service manager class ServiceManager[SS: ServiceState]: def __init__(self, service: type[Service[SS]], state: SS): self.service = service(state) def get_state(self) -> SS: return self.service.get_state() # usage service_list: list[ServiceManager[ServiceState]] = [] service_list.append( ServiceManager(PizzaService, PizzaServiceState(topping='cheese', sauce='tomato')) ) # Pyright error: Type parameter "SS@ServiceManager" is invariant, but "PizzaServiceState" is not the same as "ServiceState" service_list_any: list[ServiceManager[Any]] = [] service_list_any.append( ServiceManager(PizzaService, PizzaServiceState(topping='cheese', sauce='tomato')) ) # this is fine service_list_any.append( ServiceManager(IceCreamService, int) ) # this is fine, but should not be allowed
submitted by /u/gerska_
[link] [comments]
​r/learnpython I want to create a list with any ServiceManager (within generic bounds). First I tried using the base type in my list, but I get errors with invariance. Then I tried using Any, but it made the type system too lax, and no longer catching errors. Do you have any suggestions? Thanks in advance 😄 Example: from dataclasses import dataclass from typing import Any # service definition @dataclass class ServiceState: pass class Service[SS: ServiceState]: def __init__(self, state: SS): self.state = state def get_state(self) -> SS: return self.state # create services @dataclass class PizzaServiceState(ServiceState): topping: str sauce: str class PizzaService(Service[PizzaServiceState]): def __init__(self, state: PizzaServiceState): super().__init__(state) @dataclass class IceCreamServiceState(ServiceState): flavor: str topping: str class IceCreamService(Service[IceCreamServiceState]): def __init__(self, state: IceCreamServiceState): super().__init__(state) # service manager class ServiceManager[SS: ServiceState]: def __init__(self, service: type[Service[SS]], state: SS): self.service = service(state) def get_state(self) -> SS: return self.service.get_state() # usage service_list: list[ServiceManager[ServiceState]] = [] service_list.append( ServiceManager(PizzaService, PizzaServiceState(topping=’cheese’, sauce=’tomato’)) ) # Pyright error: Type parameter “SS@ServiceManager” is invariant, but “PizzaServiceState” is not the same as “ServiceState” service_list_any: list[ServiceManager[Any]] = [] service_list_any.append( ServiceManager(PizzaService, PizzaServiceState(topping=’cheese’, sauce=’tomato’)) ) # this is fine service_list_any.append( ServiceManager(IceCreamService, int) ) # this is fine, but should not be allowed submitted by /u/gerska_ [link] [comments]Â
I want to create a list with any ServiceManager (within generic bounds).
First I tried using the base type in my list, but I get errors with invariance.
Then I tried using Any, but it made the type system too lax, and no longer catching errors.
Do you have any suggestions?
Thanks in advance 😄
Example:
from dataclasses import dataclass from typing import Any # service definition @dataclass class ServiceState: pass class Service[SS: ServiceState]: def __init__(self, state: SS): self.state = state def get_state(self) -> SS: return self.state # create services @dataclass class PizzaServiceState(ServiceState): topping: str sauce: str class PizzaService(Service[PizzaServiceState]): def __init__(self, state: PizzaServiceState): super().__init__(state) @dataclass class IceCreamServiceState(ServiceState): flavor: str topping: str class IceCreamService(Service[IceCreamServiceState]): def __init__(self, state: IceCreamServiceState): super().__init__(state) # service manager class ServiceManager[SS: ServiceState]: def __init__(self, service: type[Service[SS]], state: SS): self.service = service(state) def get_state(self) -> SS: return self.service.get_state() # usage service_list: list[ServiceManager[ServiceState]] = [] service_list.append( ServiceManager(PizzaService, PizzaServiceState(topping='cheese', sauce='tomato')) ) # Pyright error: Type parameter "SS@ServiceManager" is invariant, but "PizzaServiceState" is not the same as "ServiceState" service_list_any: list[ServiceManager[Any]] = [] service_list_any.append( ServiceManager(PizzaService, PizzaServiceState(topping='cheese', sauce='tomato')) ) # this is fine service_list_any.append( ServiceManager(IceCreamService, int) ) # this is fine, but should not be allowed
submitted by /u/gerska_
[link] [comments]Â