Created
December 8, 2024 04:54
-
-
Save mypy-play/4619ce6d001d7c7c6994d2b6c912424a to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Generic, TypeVar, Union, assert_type, cast, overload, Self | |
| InputType = TypeVar("InputType") | |
| OutputType = TypeVar("OutputType") | |
| class Field(Generic[InputType, OutputType]): | |
| # We want both InputType and OutputType to be used in functions like the | |
| # ones below, so they can't be marked covariant or contravariant. | |
| def validate(self, input: Any) -> InputType: | |
| return cast(InputType, input) | |
| def render(self, input: InputType) -> OutputType: | |
| return cast(OutputType, input) | |
| def serialize(self, output: OutputType) -> str: | |
| return "" | |
| class StringField(Field[str, str]): | |
| pass | |
| class IntegerField(Field[int, int]): | |
| pass | |
| class BooleanField(Field[bool, bool]): | |
| pass | |
| def input_type(field: Field[InputType, OutputType]) -> InputType: | |
| return cast(InputType, field) | |
| OneOfInputType = TypeVar("OneOfInputType") | |
| OneOfOutputType = TypeVar("OneOfOutputType") | |
| AInputType = TypeVar("AInputType") | |
| AOutputType = TypeVar("AOutputType") | |
| BInputType = TypeVar("BInputType") | |
| BOutputType = TypeVar("BOutputType") | |
| CInputType = TypeVar("CInputType") | |
| COutputType = TypeVar("COutputType") | |
| class OneOfField( | |
| Generic[OneOfInputType, OneOfOutputType], | |
| Field[OneOfInputType, OneOfOutputType], | |
| ): | |
| fields: list[Field] | |
| def __init__(self) -> None: | |
| super().__init__() | |
| self.fields = [] | |
| @overload | |
| def with_( | |
| self, | |
| a: Field[AInputType, AOutputType], | |
| b: Field[BInputType, BOutputType], | |
| ) -> "OneOfField[Union[AInputType, BInputType], Union[AOutputType, BOutputType]]": ... | |
| @overload | |
| def with_( | |
| self, | |
| a: Field[AInputType, AOutputType], | |
| b: Field[BInputType, BOutputType], | |
| c: Field[CInputType, COutputType], | |
| ) -> "OneOfField[Union[AInputType, BInputType, CInputType], Union[AOutputType, BOutputType, COutputType]]": ... | |
| def with_( | |
| self, | |
| a: Field[AInputType, AOutputType], | |
| b: Field[BInputType, BOutputType], | |
| c: Field[CInputType, COutputType] | None = None, | |
| ) -> "OneOfField": | |
| if c is None: | |
| self.fields = [a, b] | |
| else: | |
| self.fields = [a, b, c] | |
| return self | |
| def return_self(self) -> Self: | |
| return self | |
| # This works: | |
| field = OneOfField().with_(StringField(), IntegerField()) | |
| assert_type(field, OneOfField[Union[str, int], Union[str, int]]) | |
| assert_type(input_type(field), Union[str, int]) | |
| # This doesn't work: | |
| class MyModel: | |
| # error: Argument 1 to "with_" of "OneOfField" has incompatible type "StringField"; expected "Field[str | int, str | int]" [arg-type] | |
| # error: Argument 2 to "with_" of "OneOfField" has incompatible type "IntegerField"; expected "Field[str | int, str | int]" [arg-type] | |
| foo: str | int = input_type(OneOfField().with_(StringField(), IntegerField())) | |
| # This works? | |
| class MyModel2: | |
| foo: str | int = input_type(OneOfField().with_(StringField(), IntegerField()).return_self()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment