You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mypy is incorrectly complaining that the arguments to a function have an incompatible type, when used in the context of a class attribute.
In the example below, we are creating a library of fields (StringField, IntegerField, etc.), to be used in a runtime schema validation library. In addition, we create a OneOfField that takes a list of sub-fields via the with_ function. Creating a OneOfField works fine by itself, but when assigned to a class attribute, mypy incorrectly complains about a type error.
fromtypingimportGeneric, TypeVar, Union, assert_type, cast, overload, SelfInputType=TypeVar("InputType")
OutputType=TypeVar("OutputType")
classField(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.defvalidate(self, input: Any) ->InputType:
returncast(InputType, input)
defrender(self, input: InputType) ->OutputType:
returncast(OutputType, input)
defserialize(self, output: OutputType) ->str:
return""classStringField(Field[str, str]):
passclassIntegerField(Field[int, int]):
passclassBooleanField(Field[bool, bool]):
passdefinput_type(field: Field[InputType, OutputType]) ->InputType:
returncast(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")
classOneOfField(
Generic[OneOfInputType, OneOfOutputType],
Field[OneOfInputType, OneOfOutputType],
):
fields: list[Field]
def__init__(self) ->None:
super().__init__()
self.fields= []
@overloaddefwith_(
self,
a: Field[AInputType, AOutputType],
b: Field[BInputType, BOutputType],
) ->"OneOfField[Union[AInputType, BInputType], Union[AOutputType, BOutputType]]": ...
@overloaddefwith_(
self,
a: Field[AInputType, AOutputType],
b: Field[BInputType, BOutputType],
c: Field[CInputType, COutputType],
) ->"OneOfField[Union[AInputType, BInputType, CInputType], Union[AOutputType, BOutputType, COutputType]]": ...
defwith_(
self,
a: Field[AInputType, AOutputType],
b: Field[BInputType, BOutputType],
c: Field[CInputType, COutputType] |None=None,
) ->"OneOfField":
ifcisNone:
self.fields= [a, b]
else:
self.fields= [a, b, c]
returnselfdefreturn_self(self) ->Self:
returnself# 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:classMyModel:
# 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?classMyModel2:
foo: str|int=input_type(OneOfField().with_(StringField(), IntegerField()).return_self())
Bug Report
mypy is incorrectly complaining that the arguments to a function have an incompatible type, when used in the context of a class attribute.
In the example below, we are creating a library of fields (
StringField
,IntegerField
, etc.), to be used in a runtime schema validation library. In addition, we create aOneOfField
that takes a list of sub-fields via thewith_
function. Creating aOneOfField
works fine by itself, but when assigned to a class attribute, mypy incorrectly complains about a type error.To Reproduce
https://gist.github.com/mypy-play/4619ce6d001d7c7c6994d2b6c912424a
This does not repro in Pyright: link
Expected Behavior
No errors.
Actual Behavior
mypy raises an invalid error saying the argument to
with_
is invalid.Your Environment
mypy.ini
(and other config files): N/AThe text was updated successfully, but these errors were encountered: