File: base_model.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 (38 lines) | stat: -rw-r--r-- 1,350 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
"""
This file is used to test pyright's ability to check Pydantic's `BaseModel` related code.
"""

from typing_extensions import assert_type

from pydantic import BaseModel, Field
from pydantic.fields import ComputedFieldInfo, FieldInfo


class MyModel(BaseModel):
    x: str
    y: list[int]
    z: int = 1


m1 = MyModel(x='hello', y=[1, 2, 3])

m2 = MyModel(x='hello')  # type: ignore[call-arg]  # pyright: ignore[reportCallIssue]

m3 = MyModel(x='hello', y=[1, '2', b'3'])  # type: ignore[list-item]  # pyright: ignore[reportArgumentType]

m1.z + 'not an int'  # type: ignore[operator]  # pyright: ignore[reportOperatorIssue]

m1.foobar  # type: ignore[attr-defined]  # pyright: ignore[reportAttributeAccessIssue]


class Knight(BaseModel):
    title: str = Field(default='Sir Lancelot')  # this is okay
    age: int = Field(23)  # this works fine at runtime but will case an error for pyright


k = Knight()  # type: ignore[call-arg]  # pyright: ignore[reportCallIssue]

assert_type(Knight.model_fields, dict[str, FieldInfo])
assert_type(Knight.model_computed_fields, dict[str, ComputedFieldInfo])
assert_type(k.model_fields, dict[str, FieldInfo])  # type: ignore[deprecated]  # pyright: ignore[reportDeprecated]
assert_type(k.model_computed_fields, dict[str, ComputedFieldInfo])  # type: ignore[deprecated]  # pyright: ignore[reportDeprecated]