Where are the Type annotation constraints (`ValueRange`/`MinLen` etc) in python 3.9?

Viewed 1602

After seeing the (awesome) new Annotated type annotation in python 3.9 (varaidic type constraints!), I rushed to upgrade so I could check them out. (https://docs.python.org/3/library/typing.html?highlight=valuerange)

But when I tried using ValueRange[min,max] or MaxLen[n] - I couldn't seem to find them anywhere.. PyCharm didn't offer me any help, and they don't seem to be in the typing module where I might expect them.

The docs feature them but googling I can't find any reference online as to how to actually import them.

Are they not in the language yet? or just in some new module I'm not aware of?

1 Answers

As others have said, these classes are just an example of what can be annotated. Annotation just grabs a certain variable and adds some "hints" to them via the class you created (MaxLen, ValueRange, etc).

You can then obtain which "hints" are related to every parameter using get_type_hints, and crawl one per one the parameters with its hits and do the checks you want (not the idea Annotation was intended, though I also found it interesting). You have a full example in this other post.

Related