glyph,
@glyph@mastodon.social avatar

PEP 593 is a bit vague on how you’re supposed to actually consume arguments to Annotated; here is my proposal.

https://blog.glyph.im/2023/12/annotated-at-runtime.html

#python #programming #mypy

mambocab,
@mambocab@alcove.website avatar

@glyph Thanks for the writeup. Small typo:

> However, I think that this is a slightly misframing of the problem.

glyph,
@glyph@mastodon.social avatar

@mambocab your attention to detail is much appreciated. Will fix

glyph,
@glyph@mastodon.social avatar

@mambocab correction should be rolling out momentarily

elventear,

@glyph coincidently I have been trying to implement a solution that uses information encoded in the Annotated arguments. Your code works great.

One thing I have noticed is that mypy doesn’t seem to type check inside the Annotated declarations. Is that your experience as well?

glyph,
@glyph@mastodon.social avatar

@elventear Huh. I haven't seen that yet. Probably worth reporting that as a bug though, that doesn't sound good

ubernostrum,

@glyph Might also compare to how some of the type-hint-oriented frameworks do it. I believe this is Litestar's implementation here:

https://github.com/litestar-org/litestar/blob/8f2cbe630ad9d71521f0477c6f0a3c0ed1c9a4a5/litestar/typing.py#L481

glyph,
@glyph@mastodon.social avatar

@ubernostrum yeah I linked to pydantic’s implementation and if anything litestar’s looks even jankier. That is a lot of Any

jcristharif,

@glyph @ubernostrum There are also a few other types-that-transparently-ish-wrap-other-types (Final, NewType, Annotated, TypeAlias) - properly traversing and stripping metadata from these is a pain.

Due to <reasons>, I maintain two gross implementations of this code in msgspec - one in python (https://github.com/jcrist/msgspec/blob/ba316c44089104873da550700973845bddd167f5/msgspec/inspect.py#L611), and one in C (https://github.com/jcrist/msgspec/blob/ba316c44089104873da550700973845bddd167f5/msgspec/_core.c#L4384-L4398). My kingdom for cleaner (but still efficient) public APIs here.

ubernostrum,

@jcristharif @glyph My stance on type hints for a while now has been that:

  1. Many of the most interesting projects involving type hints are doing things with them at runtime, rather than static ahead-of-time checking, and
  2. The people writing type-hinting PEPs have seemed to be almost completely unaware of this.

This discussion is an example of both of those points.

(also, the fact that it was all about nominal typing for such a long time was annoying and kept me from really wanting to bother adopting)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • DreamBathrooms
  • mdbf
  • ngwrru68w68
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • osvaldo12
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • InstantRegret
  • tacticalgear
  • provamag3
  • ethstaker
  • cisconetworking
  • modclub
  • tester
  • GTA5RPClips
  • cubers
  • everett
  • normalnudes
  • megavids
  • Leos
  • anitta
  • JUstTest
  • lostlight
  • All magazines