Skip to content

Provide better typing of VariableFactory #504

Open
@williambdean

Description

@williambdean

The VariableFactory just requires the create_variable method and dims attribute to a class. However, if the class has to_dict and is registered for deserialize function, then that gives the additional benefit of serialization and deserialization. I don't know the best way to type this or if it is even an issue

Maybe there a protocol with optional methods? or something like below should also be specified:

@runtime_checkable
class SerializableVariableFactory(Protocol):
    dims: tuple[str, ...]

    def create_variable(self, name: str) -> Variable:
        ...

    def to_dict(self) -> dict[str, Any]:
        ...

This issue was transferred from pymc-labs/pymc-marketing#1341

Comments

lucianopaz

The typing of this kind of methods is like this:

Self = TypeVar("Self", bound="SerializableVariableFactory")

class SerializableVariableFactory(Protocol):
    def to_dict(self) -> dict[str, Any]:
        return {}

    @classmethod
    def from_dict(cls: type[Self], data: dict[str, Any]) -> Self:

Starting from python 3.11, typing now includes Self, which makes this easier to do.


williambdean

from_dict isn't required

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions