Skip to content

Commit 953bbea

Browse files
authored
Update to pyright==1.1.345 (pydantic#8453)
1 parent 54a1576 commit 953bbea

File tree

7 files changed

+53
-42
lines changed

7 files changed

+53
-42
lines changed

Diff for: .github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ jobs:
261261
node-version: '18'
262262

263263
- name: install pyright
264-
run: npm install -g [email protected].335 # try to keep this in sync with .pre-commit-config.yaml
264+
run: npm install -g [email protected].345 # try to keep this in sync with .pre-commit-config.yaml
265265

266266
- name: run pyright tests
267267
run: make test-pyright

Diff for: .pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ repos:
3636
types: [python]
3737
language: node
3838
pass_filenames: false
39-
additional_dependencies: ["[email protected].335"] # try to keep this in sync with .github/workflows/ci.yml
39+
additional_dependencies: ["[email protected].345"] # try to keep this in sync with .github/workflows/ci.yml

Diff for: pydantic/_internal/_decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def __get__(self, obj: object | None, obj_type: type[object] | None = None) -> P
188188

189189
def __set_name__(self, instance: Any, name: str) -> None:
190190
if hasattr(self.wrapped, '__set_name__'):
191-
self.wrapped.__set_name__(instance, name)
191+
self.wrapped.__set_name__(instance, name) # pyright: ignore[reportFunctionMemberAccess]
192192

193193
def __getattr__(self, __name: str) -> Any:
194194
"""Forward checks for __isabstractmethod__ and such."""

Diff for: pydantic/_internal/_generate_schema.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def _generate_schema_from_property(self, obj: Any, source: Any) -> core_schema.C
651651
schema = self._unpack_refs_defs(schema)
652652

653653
if is_function_with_inner_schema(schema):
654-
ref = schema['schema'].pop('ref', None)
654+
ref = schema['schema'].pop('ref', None) # pyright: ignore[reportGeneralTypeIssues]
655655
if ref:
656656
schema['ref'] = ref
657657
else:
@@ -2061,7 +2061,7 @@ def _extract_get_pydantic_json_schema(tp: Any, schema: CoreSchema) -> GetJsonSch
20612061

20622062
has_custom_v2_modify_js_func = (
20632063
js_modify_function is not None
2064-
and BaseModel.__get_pydantic_json_schema__.__func__
2064+
and BaseModel.__get_pydantic_json_schema__.__func__ # type: ignore
20652065
not in (js_modify_function, getattr(js_modify_function, '__func__', None))
20662066
)
20672067

Diff for: pydantic/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def __init__(self, /, **data: Any) -> None: # type: ignore
171171
self.__pydantic_validator__.validate_python(data, self_instance=self)
172172

173173
# The following line sets a flag that we use to determine when `__init__` gets overridden by the user
174-
__init__.__pydantic_base_init__ = True
174+
__init__.__pydantic_base_init__ = True # pyright: ignore[reportFunctionMemberAccess]
175175

176176
@property
177177
def model_extra(self) -> dict[str, Any] | None:

Diff for: pydantic/root_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def __init__(self, /, root: RootModelRootType = PydanticUndefined, **data) -> No
6262
root = data # type: ignore
6363
self.__pydantic_validator__.validate_python(root, self_instance=self)
6464

65-
__init__.__pydantic_base_init__ = True
65+
__init__.__pydantic_base_init__ = True # pyright: ignore[reportFunctionMemberAccess]
6666

6767
@classmethod
68-
def model_construct(cls: type[Model], root: RootModelRootType, _fields_set: set[str] | None = None) -> Model:
68+
def model_construct(cls: type[Model], root: RootModelRootType, _fields_set: set[str] | None = None) -> Model: # type: ignore
6969
"""Create a new model using the provided root object and update fields set.
7070
7171
Args:
@@ -110,7 +110,7 @@ def __deepcopy__(self: Model, memo: dict[int, Any] | None = None) -> Model:
110110

111111
if typing.TYPE_CHECKING:
112112

113-
def model_dump(
113+
def model_dump( # type: ignore
114114
self,
115115
*,
116116
mode: Literal['json', 'python'] | str = 'python',

Diff for: pydantic/type_adapter.py

+44-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import sys
55
from dataclasses import is_dataclass
6-
from typing import TYPE_CHECKING, Any, Dict, Generic, Iterable, Set, TypeVar, Union, cast, overload
6+
from typing import TYPE_CHECKING, Any, Dict, Generic, Iterable, Set, TypeVar, Union, cast, final, overload
77

88
from pydantic_core import CoreSchema, SchemaSerializer, SchemaValidator, Some
99
from typing_extensions import Literal, get_args, is_typeddict
@@ -24,6 +24,7 @@
2424

2525
T = TypeVar('T')
2626

27+
2728
if TYPE_CHECKING:
2829
# should be `set[int] | set[str] | dict[int, IncEx] | dict[str, IncEx] | None`, but mypy can't cope
2930
IncEx = Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any]]
@@ -106,6 +107,7 @@ def _type_has_config(type_: Any) -> bool:
106107
return False
107108

108109

110+
@final
109111
class TypeAdapter(Generic[T]):
110112
"""Type adapters provide a flexible way to perform validation and serialization based on a Python type.
111113
@@ -120,40 +122,37 @@ class TypeAdapter(Generic[T]):
120122
serializer: The schema serializer for the type.
121123
"""
122124

123-
if TYPE_CHECKING:
124-
125-
@overload
126-
def __new__(cls, __type: type[T], *, config: ConfigDict | None = ...) -> TypeAdapter[T]:
127-
...
128-
129-
# this overload is for non-type things like Union[int, str]
130-
# Pyright currently handles this "correctly", but MyPy understands this as TypeAdapter[object]
131-
# so an explicit type cast is needed
132-
@overload
133-
def __new__(cls, __type: T, *, config: ConfigDict | None = ...) -> TypeAdapter[T]:
134-
...
135-
136-
def __new__(cls, __type: Any, *, config: ConfigDict | None = None) -> TypeAdapter[T]:
137-
"""A class representing the type adapter."""
138-
raise NotImplementedError
139-
140-
@overload
141-
def __init__(
142-
self, type: type[T], *, config: ConfigDict | None = None, _parent_depth: int = 2, module: str | None = None
143-
) -> None:
144-
...
145-
146-
# this overload is for non-type things like Union[int, str]
147-
# Pyright currently handles this "correctly", but MyPy understands this as TypeAdapter[object]
148-
# so an explicit type cast is needed
149-
@overload
150-
def __init__(
151-
self, type: T, *, config: ConfigDict | None = None, _parent_depth: int = 2, module: str | None = None
152-
) -> None:
153-
...
125+
@overload
126+
def __init__(
127+
self,
128+
type: type[T],
129+
*,
130+
config: ConfigDict | None = ...,
131+
_parent_depth: int = ...,
132+
module: str | None = ...,
133+
) -> None:
134+
...
135+
136+
# This second overload is for unsupported special forms (such as Union). `pyright` handles them fine, but `mypy` does not match
137+
# them against `type: type[T]`, so an explicit overload with `type: T` is needed.
138+
@overload
139+
def __init__( # pyright: ignore[reportOverlappingOverload]
140+
self,
141+
type: T,
142+
*,
143+
config: ConfigDict | None = ...,
144+
_parent_depth: int = ...,
145+
module: str | None = ...,
146+
) -> None:
147+
...
154148

155149
def __init__(
156-
self, type: Any, *, config: ConfigDict | None = None, _parent_depth: int = 2, module: str | None = None
150+
self,
151+
type: type[T] | T,
152+
*,
153+
config: ConfigDict | None = None,
154+
_parent_depth: int = 2,
155+
module: str | None = None,
157156
) -> None:
158157
"""Initializes the TypeAdapter object.
159158
@@ -173,6 +172,18 @@ def __init__(
173172
It may be deprecated in a minor version, so we only recommend using it if you're
174173
comfortable with potential change in behavior / support.
175174
175+
??? tip "Compatibility with `mypy`"
176+
Depending on the type used, `mypy` might raise an error when instantiating a `TypeAdapter`. As a workaround, you can explicitly
177+
annotate your variable:
178+
179+
```py
180+
from typing import Union
181+
182+
from pydantic import TypeAdapter
183+
184+
ta: TypeAdapter[Union[str, int]] = TypeAdapter(Union[str, int]) # type: ignore[arg-type]
185+
```
186+
176187
Returns:
177188
A type adapter configured for the specified `type`.
178189
"""

0 commit comments

Comments
 (0)