Skip to content

Commit

Permalink
Update to pyright==1.1.345 (#8453)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jan 12, 2024
1 parent 54a1576 commit 953bbea
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -261,7 +261,7 @@ jobs:
node-version: '18'

- name: install pyright
run: npm install -g pyright@1.1.335 # try to keep this in sync with .pre-commit-config.yaml
run: npm install -g pyright@1.1.345 # try to keep this in sync with .pre-commit-config.yaml

- name: run pyright tests
run: make test-pyright
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Expand Up @@ -36,4 +36,4 @@ repos:
types: [python]
language: node
pass_filenames: false
additional_dependencies: ["pyright@1.1.335"] # try to keep this in sync with .github/workflows/ci.yml
additional_dependencies: ["pyright@1.1.345"] # try to keep this in sync with .github/workflows/ci.yml
2 changes: 1 addition & 1 deletion pydantic/_internal/_decorators.py
Expand Up @@ -188,7 +188,7 @@ def __get__(self, obj: object | None, obj_type: type[object] | None = None) -> P

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

def __getattr__(self, __name: str) -> Any:
"""Forward checks for __isabstractmethod__ and such."""
Expand Down
4 changes: 2 additions & 2 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -651,7 +651,7 @@ def _generate_schema_from_property(self, obj: Any, source: Any) -> core_schema.C
schema = self._unpack_refs_defs(schema)

if is_function_with_inner_schema(schema):
ref = schema['schema'].pop('ref', None)
ref = schema['schema'].pop('ref', None) # pyright: ignore[reportGeneralTypeIssues]
if ref:
schema['ref'] = ref
else:
Expand Down Expand Up @@ -2061,7 +2061,7 @@ def _extract_get_pydantic_json_schema(tp: Any, schema: CoreSchema) -> GetJsonSch

has_custom_v2_modify_js_func = (
js_modify_function is not None
and BaseModel.__get_pydantic_json_schema__.__func__
and BaseModel.__get_pydantic_json_schema__.__func__ # type: ignore
not in (js_modify_function, getattr(js_modify_function, '__func__', None))
)

Expand Down
2 changes: 1 addition & 1 deletion pydantic/main.py
Expand Up @@ -171,7 +171,7 @@ def __init__(self, /, **data: Any) -> None: # type: ignore
self.__pydantic_validator__.validate_python(data, self_instance=self)

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

@property
def model_extra(self) -> dict[str, Any] | None:
Expand Down
6 changes: 3 additions & 3 deletions pydantic/root_model.py
Expand Up @@ -62,10 +62,10 @@ def __init__(self, /, root: RootModelRootType = PydanticUndefined, **data) -> No
root = data # type: ignore
self.__pydantic_validator__.validate_python(root, self_instance=self)

__init__.__pydantic_base_init__ = True
__init__.__pydantic_base_init__ = True # pyright: ignore[reportFunctionMemberAccess]

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

if typing.TYPE_CHECKING:

def model_dump(
def model_dump( # type: ignore
self,
*,
mode: Literal['json', 'python'] | str = 'python',
Expand Down
77 changes: 44 additions & 33 deletions pydantic/type_adapter.py
Expand Up @@ -3,7 +3,7 @@

import sys
from dataclasses import is_dataclass
from typing import TYPE_CHECKING, Any, Dict, Generic, Iterable, Set, TypeVar, Union, cast, overload
from typing import TYPE_CHECKING, Any, Dict, Generic, Iterable, Set, TypeVar, Union, cast, final, overload

from pydantic_core import CoreSchema, SchemaSerializer, SchemaValidator, Some
from typing_extensions import Literal, get_args, is_typeddict
Expand All @@ -24,6 +24,7 @@

T = TypeVar('T')


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


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

if TYPE_CHECKING:

@overload
def __new__(cls, __type: type[T], *, config: ConfigDict | None = ...) -> TypeAdapter[T]:
...

# this overload is for non-type things like Union[int, str]
# Pyright currently handles this "correctly", but MyPy understands this as TypeAdapter[object]
# so an explicit type cast is needed
@overload
def __new__(cls, __type: T, *, config: ConfigDict | None = ...) -> TypeAdapter[T]:
...

def __new__(cls, __type: Any, *, config: ConfigDict | None = None) -> TypeAdapter[T]:
"""A class representing the type adapter."""
raise NotImplementedError

@overload
def __init__(
self, type: type[T], *, config: ConfigDict | None = None, _parent_depth: int = 2, module: str | None = None
) -> None:
...

# this overload is for non-type things like Union[int, str]
# Pyright currently handles this "correctly", but MyPy understands this as TypeAdapter[object]
# so an explicit type cast is needed
@overload
def __init__(
self, type: T, *, config: ConfigDict | None = None, _parent_depth: int = 2, module: str | None = None
) -> None:
...
@overload
def __init__(
self,
type: type[T],
*,
config: ConfigDict | None = ...,
_parent_depth: int = ...,
module: str | None = ...,
) -> None:
...

# This second overload is for unsupported special forms (such as Union). `pyright` handles them fine, but `mypy` does not match
# them against `type: type[T]`, so an explicit overload with `type: T` is needed.
@overload
def __init__( # pyright: ignore[reportOverlappingOverload]
self,
type: T,
*,
config: ConfigDict | None = ...,
_parent_depth: int = ...,
module: str | None = ...,
) -> None:
...

def __init__(
self, type: Any, *, config: ConfigDict | None = None, _parent_depth: int = 2, module: str | None = None
self,
type: type[T] | T,
*,
config: ConfigDict | None = None,
_parent_depth: int = 2,
module: str | None = None,
) -> None:
"""Initializes the TypeAdapter object.
Expand All @@ -173,6 +172,18 @@ def __init__(
It may be deprecated in a minor version, so we only recommend using it if you're
comfortable with potential change in behavior / support.
??? tip "Compatibility with `mypy`"
Depending on the type used, `mypy` might raise an error when instantiating a `TypeAdapter`. As a workaround, you can explicitly
annotate your variable:
```py
from typing import Union
from pydantic import TypeAdapter
ta: TypeAdapter[Union[str, int]] = TypeAdapter(Union[str, int]) # type: ignore[arg-type]
```
Returns:
A type adapter configured for the specified `type`.
"""
Expand Down

0 comments on commit 953bbea

Please sign in to comment.