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

Adopt PEP 585 and PEP 604 #8434

Closed
3 of 13 tasks
bswck opened this issue Dec 23, 2023 · 3 comments
Closed
3 of 13 tasks

Adopt PEP 585 and PEP 604 #8434

bswck opened this issue Dec 23, 2023 · 3 comments

Comments

@bswck
Copy link

bswck commented Dec 23, 2023

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

I suggest adopting (either by adding a dependency or, more safely, incorporating into pydantic internally) a PEP 585 (allow subscripting built-in generic types, e.g. dict[str, int]) and PEP 604 (X | Y union type syntax) backport for Python <3.10 runtime type evaluation, so that the freshest type hinting style can be used for pydantic models, without having to import the deprecated type aliases from typing.

If this feature request is accepted, the developers that use pydantic, could ditch the old, confusing for Python beginners, type hinting format:

import collections.abc
from typing import DefaultDict, Dict, FrozenSet, List, Set, Tuple, Optional, Union

from pydantic import BaseModel

class Foo(BaseModel):
    a: Dict[str, int]
    b: List[int]
    c: Set[int]
    d: Optional[Tuple[int, ...]]
    e: FrozenSet[int]
    f: DefaultDict[str, int]
    g: Optional[str]
    h: Union[str, int]
    i: Optional[Union[str, int]]
    j: str
    k: collections.abc.Mapping[str, int]
    l: Optional[collections.abc.Mapping[str, int]]
    m: Optional[collections.abc.Mapping[str, Optional[int]], float]

and use the newest, prettiest & recommended one:

import collections.abc
from collections import defaultdict

from pydantic import BaseModel

class Foo:
    a: dict[str, int]
    b: list[int]
    c: set[int]
    d: tuple[int, ...] | None
    e: frozenset[int]
    f: defaultdict[str, int]
    g: str | None
    h: str | int
    i: str | int | None
    j: str
    k: collections.abc.Mapping[str, int]
    l: collections.abc.Mapping[str, int] | None
    m: collections.abc.Mapping[str, int | None] | float | None

in Python 3.8 and 3.9.

The new fashion of type hints is broadly used for example in pandas (pandas.DataFrame.__init__—very likely people would see it often), poetry, FastUI and many, many more modern projects that do support Python 3.8+ already. But only in the typing scope that doesn't get evaluated at runtime—this feature request aims to open up a possibility to migrate the codebase fully to the newest fashion.

I am suggesting it mainly because I know it is doable by altering the namespaces used by ForwardRef's with the help of AST—and would love to see it as a default behavior of pydantic. I could work on it, document it and test it against built-in types and pydantic extra types.

Merry Christmas! 🎄

Affected Components

@bswck
Copy link
Author

bswck commented Dec 23, 2023

I tried to suggest this as a feature for typing_extensions, but I don't think it would be accepted any soon, if ever.

@samuelcolvin
Copy link
Member

It's already a WIP, see #8209.

@bswck
Copy link
Author

bswck commented Dec 23, 2023

It's already a WIP, see #8209.

Thanks, I completely missed this! I will see if I can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants