Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating type aliases with type checked metadata attached #555

Open
joslarson opened this issue Apr 25, 2018 · 6 comments
Open

Allow creating type aliases with type checked metadata attached #555

joslarson opened this issue Apr 25, 2018 · 6 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@joslarson
Copy link

joslarson commented Apr 25, 2018

Annotations in Python have many use cases beyond static type checking. I'd really like to be able to create a type that is an alias to another type while also attaching metadata to it. The idea is to be able to use MyPy's type system but also use annotations for other use cases such as runtime validation at the same time. I'm thinking something along these lines (though I have no idea how this would work in practice):

from datetime import datetime
from mypy_extensions import TypedDict
from somewhere import AliasWithMetadata

# define metadata type
Options = TypedDict('Options', {'min': int, 'max': int})
# define type alias with allowed metadata
MinMaxInt = AliasWithMetadata(int, Options)

# define age arg as type int with metadata attached
def get_rough_birth_year(age: MinMaxInt(min=0, max=120)):
    return datetime.today().year - age

# call function with int without mypy complaining
get_rough_birth_year(105)
@joslarson
Copy link
Author

To be clear, I'm not expecting MyPy to do anything with the metadata other than type check the metadata object itself.

@ilevkivskyi
Copy link
Member

This is essentially fixed with PEP 593, Annotated is already available in typing_extensions (also mypy will likely support it in near future).

@joslarson
Copy link
Author

joslarson commented Nov 18, 2019

@ilevkivskyi I just saw this got closed. This is great news. Question though, is it possible yet to create something like MinMaxInt(min=0, max=120) above where you define the structure of an annotated type then can dynamically pass in different metadata values per usage site?

@ilevkivskyi
Copy link
Member

The currently supported syntax is a bit more verbose, but I think this is possible. For example:

from typing_extensions import Annotated as An

def get_rough_birth_year(age: An[int, Range(min=0, max=120)]):
    return datetime.today().year - age

The problem with the original proposal is that it is hard to define a general enough API/syntax for user-defined annotations.

@joslarson
Copy link
Author

joslarson commented Nov 26, 2019

@ilevkivskyi That makes sense. The part that is missing that would be really valuable as a library creator would be to be able to define and export strictly typed annotation types (if that makes sense). In your example the user could pass in something other than Range(...), and the type checker wouldn't care.

What if you could define a generic annotation type like this:

MinMaxOptions = TypedDict('MinMaxOptions', {'min': int, 'max': int})

def Range(min: int, max: int) -> MinMaxOptions:
    return {'min': min, 'max': max}

MinMaxInt = GenericAnnotation[int, MinMaxOptions]

def get_rough_birth_year(age: MinMaxInt[Range(min=0, max=120)]):
    return datetime.today().year - age

With that the shape of the annotation could be anything, but once defined could be enforced. Or am I still missing something?

@ilevkivskyi
Copy link
Member

With that the shape of the annotation could be anything, but once defined could be enforced. Or am I still missing something?

Yes, this is something that goes beyond PEP 593. But it is also a harder sale. Anyway, let's re-open to track this particular case.

@ilevkivskyi ilevkivskyi reopened this Nov 27, 2019
@joslarson joslarson changed the title Allow creating type aliases with metadata attached Allow creating type aliases with type checked metadata attached Nov 27, 2019
@srittau srittau added the topic: feature Discussions about new features for Python's type annotations label Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

3 participants