Skip to content

Commit 13b9792

Browse files
committed
docs(pydantic): update examples and simplify TypeIDField typing
1 parent f19cd85 commit 13b9792

2 files changed

Lines changed: 10 additions & 16 deletions

File tree

docs/integrations/pydantic.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ from typeid.integrations.pydantic import TypeIDField
6161
class User(BaseModel):
6262
id: TypeIDField[Literal["user"]]
6363

64-
tid = TypeID.from_str("user_01ke82dtesfn9bjcrzyzz54ya9")
64+
tid = TypeID.from_string("user_01ke82dtesfn9bjcrzyzz54ya9")
6565
u = User(id=tid)
6666

6767
assert u.id == tid
@@ -104,7 +104,7 @@ class User(BaseModel):
104104
id: TypeIDField[Literal["user"]]
105105

106106
u = User(id="user_01ke82dtesfn9bjcrzyzz54ya9")
107-
data = u.model_dump()
107+
data = u.model_dump(mode="json")
108108

109109
assert data == {"id": "user_01ke82dtesfn9bjcrzyzz54ya9"}
110110
```

typeid/integrations/pydantic/v2.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Any, ClassVar, Generic, Literal, Optional, TypeVar, get_args, get_origin, overload
2+
from typing import Any, ClassVar, Generic, Literal, Optional, TypeVar, get_args, get_origin
33

44
from pydantic_core import core_schema
55
from pydantic.json_schema import JsonSchemaValue
@@ -109,19 +109,13 @@ class User(BaseModel):
109109
This returns a specialized *type* that Pydantic will validate into your core TypeID.
110110
"""
111111

112-
@overload
113-
def __class_getitem__(cls, prefix: str) -> type[TypeID]:
114-
...
115-
116-
@overload
117-
def __class_getitem__(cls, prefix: tuple[str]) -> type[TypeID]:
118-
...
119-
120-
def __class_getitem__(cls, item: Any) -> type[TypeID]:
121-
# Support:
122-
# - TypeIDField["user"]
123-
# - TypeIDField[Literal["user"]]
124-
# - TypeIDField[("user",)]
112+
def __class_getitem__(cls, item: str | tuple[str]) -> type[TypeID]:
113+
"""
114+
Support:
115+
- TypeIDField["user"]
116+
- TypeIDField[Literal["user"]]
117+
- TypeIDField[("user",)]
118+
"""
125119
if isinstance(item, tuple):
126120
if len(item) != 1:
127121
raise TypeError("TypeIDField[...] expects a single prefix")

0 commit comments

Comments
 (0)