File: type_adapter.py

package info (click to toggle)
pydantic 2.12.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,640 kB
  • sloc: python: 75,984; javascript: 181; makefile: 115; sh: 38
file content (27 lines) | stat: -rw-r--r-- 925 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# to be removed with PEP 747:
# mypy: disable_error_code=var-annotated
from typing import Annotated

from typing_extensions import assert_type

from pydantic import TypeAdapter

ta1 = TypeAdapter(int)
assert_type(ta1, TypeAdapter[int])

assert_type(ta1.validate_python('1'), int)
ta1.dump_python(1)
ta1.dump_python('1')  # type: ignore[arg-type]  # pyright: ignore[reportArgumentType]
ta1.dump_json(1)
ta1.dump_json('1')  # type: ignore[arg-type]  # pyright: ignore[reportArgumentType]

# The following use cases require PEP 747: TypeExpr:

ta2 = TypeAdapter(Annotated[int, ...])
assert_type(ta2, TypeAdapter[int])  # type: ignore[assert-type]  # pyright: ignore[reportAssertTypeFailure]

ta3: TypeAdapter[int] = TypeAdapter(Annotated[int, ...])
assert_type(ta3, TypeAdapter[int])

ta4 = TypeAdapter(int | str)
assert_type(ta4, TypeAdapter[int | str])  # type: ignore[assert-type]  # pyright: ignore[reportAssertTypeFailure]