Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide better typing of VariableFactory #1341

Open
wd60622 opened this issue Jan 4, 2025 · 2 comments
Open

Provide better typing of VariableFactory #1341

wd60622 opened this issue Jan 4, 2025 · 2 comments

Comments

@wd60622
Copy link
Contributor

wd60622 commented Jan 4, 2025

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]:
        ...
@lucianopaz
Copy link
Contributor

lucianopaz commented Jan 9, 2025

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.

@wd60622
Copy link
Contributor Author

wd60622 commented Jan 9, 2025

from_dict isn't required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants