[Python] How to use a generic function defined in Python?

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I have a class and a corresponding factory function that holds some state in a closure (event_bus):

    from typing import TypeVar, Generic, Callable, Any

    class EventBus:

    def subscribe(self):
    print("Subscribed")

    def publish(self):
    print("Published")

    T = TypeVar('T')

    def create_property(event_bus: EventBus):
    class Property(Generic[T]):
    def __init__(self, validator: Callable[[T], bool]):
    self._validator = validator

    def __set_name__(self, obj: Any, name: str):
    self.name = name

    def __get__(self, obj: Any, type: Any) -> T:
    return obj.__dict__.get(self.name)

    def __set__(self, obj: Any, value: T):
    if not self._validator(value):
    raise ValueError("Invalid value")
    obj.__dict__[self.name] = value
    event_bus.publish()

    def property(validator: Callable[[T], bool] = lambda x: True) -> Property[T]:
    return Property(validator)

    return property


    Now this compiles, and the property function has the correct type: Callable[..., Property[T]]. Property can be used as a descriptor, but when I try to do so I get compiler errors:

    property: Callable[..., Property[T]] = create_property(event_bus.EventBus())


    class MyComponent:

    width = property() # Type of "width" is partially unknown
    # Type of "width" is "Property[Unknown]
    height: int = property(lambda x: x > 0) # Expression of type "Property[Unknown]" is
    # incompatible with declared type "int"


    What am I doing wrong? How do I call the generic property function properly?

    Continue reading...

Compartilhe esta Página