question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for Annotated in `is_subtype` (transparently ignoring the annotations)

See original GitHub issue

In 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:open
  • Created 9 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, Dec 16, 2022

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.

0reactions
apirogovcommented, Dec 18, 2022

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

NotImplementedError: ('No support for type:', typing_extensions.Annotated[typing.List[typing_extensions.Annotated[int, 'hello']], 'world'])

(expected: types are equivalent, i.e. True in both directions)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add support for external annotations in the typing module #600
We propose adding an Annotated type to the typing module to decorate existing types with context-specific metadata.
Read more >
Annotations on an overridden method are ignored in Java 8 ...
The test calls the following code to see if the annotation is present: CompositeProperty compositeProperty = propd.getReadMethod().getAnnotation ...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
If a library (or tool) encounters a typehint Annotated[T, x] and has no special logic for metadata x , it should ignore it...
Read more >
Predefined Annotation Types - Java™ Tutorials
SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler. RetentionPolicy.CLASS – The marked annotation...
Read more >
AnnotationUtils (Spring Framework 6.0.3 API)
All public methods in this class that return annotations, arrays of annotations, or AnnotationAttributes transparently support attribute aliases configured ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found