python / python/typing

Allow creating type aliases with type checked metadata attached

Open
#555 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

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)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or implementation entry point is identified. Start by reviewing the proposed AliasWithMetadata example and existing Python typing and MyPy behavior, then clarify the required static-checking and runtime metadata semantics. Done would require an agreed design and documented behavior for defining and using such aliases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.