Skip to content

Why does subclassing TypedDict work with a field_validator even though this explicitly violates PEP 589 and type hinting guidelines? #6517

Answered by adriangb
prrao87 asked this question in Question
Discussion options

You must be logged in to vote

The short answer is that what you are using works at runtime but type checkers don't like it. Because it wasn't much extra work on our end / at runtime we thought it better to make it work even if users have to add a # type: ignore and figure out down the road if we can convince type checkers to allow it or look for alternatives.

I'll point out that there is an alternative for that particular example that:

  1. Makes type checkers happy.
  2. Is more composable and reusable.
  3. Is much more human-readable and better at transmitting information of what is being validated.
import math
from typing import Annotated, TypeVar, Union

from annotated_types import Gt
from typing_extensions import NotRequired, 

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@adriangb
Comment options

@alfawal
Comment options

Comment options

You must be logged in to vote
1 reply
@prrao87
Comment options

Answer selected by prrao87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants