Add support for Annotated in `is_subtype` (transparently ignoring the annotations)
See original GitHub issueIn my project I am using pydantic and use its Field(...)
configuration e.g. to define alias names for keys within Annotated
, e.g.
x: Annotated[int, Field(alias="y")]
but I need to check is_subtype
on the wrapped types (I don’t use the annotation for any type-domain related info).
It would be great if Annotated
could be transparently ignored.
I understand there are many “semantics” one could give to checking Annotated
types, but as the annotation is quite arbitrary and tool specific, I think that simply ignoring the annotation and recursing down the actual type would be the “right” default behavior.
A more conservative solution I would also be happy with would be to have this as some keyword argument to is_subtype
, called e.g. ignore_annotations
to have that behavior and do with them whatever you believe would make more sense as the default.
But in my case, the annotation object (pydantic.FieldInfo) does not have even equality, so this would not work for me to check that the annotations must all be equal or something like that. Thats why I think it does not make any sense to try doing anything with the annotations but ignoring them, as it probably would not work in the general case.
Issue Analytics
- State:
- Created 9 months ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Sorry, I misunderstood, I thought you’re talking about a feature that’s specific to
pydantic
.Yeah, I agree, runtype should cover everything that’s in
typing
. I’ll see if I can find time for it soon.In Python 3.8 Annotated is in typing_extensions and it does not seem to work:
is_subtype(List[int], Annotated[List[Annotated[int, "hello"]], "world"]))
yields(expected: types are equivalent, i.e. True in both directions)