New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to pyright==1.1.345
#8453
Changes from all commits
220cdc2
64c6082
66a320e
dec5b1f
9120cfa
84c1e75
7502d29
572e457
52c1ba6
bc9fe04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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]] | ||
|
@@ -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. | ||
|
||
|
@@ -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]: | ||
... | ||
Comment on lines
-125
to
-127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we basically loose all typing for |
||
|
||
# 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], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work now? What about passing in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following up, this doesn't work: from pydantic import TypeAdapter
from typing import Union
adapter: TypeAdapter[Union[int, str]] = TypeAdapter(Union[int, str]) -->
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any downside of leaving the above overloads within the |
||
*, | ||
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. | ||
|
||
|
@@ -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`. | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the problem here / what error is raised?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method "model_construct" overrides class "BaseModel" in an incompatible manner
, due to the fact thatreportIncompatibleMethodOverride
was recently reported by default to be compliant with the typing spec.