Skip to content
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

Merged
merged 10 commits into from Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -626,7 +626,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 @@ -2036,7 +2036,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 @@ -165,7 +165,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_computed_fields(self) -> dict[str, ComputedFieldInfo]:
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
Copy link
Member

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?

Copy link
Contributor Author

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 that reportIncompatibleMethodOverride was recently reported by default to be compliant with the typing spec.

"""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
42 changes: 8 additions & 34 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

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 @@ -120,40 +121,13 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we basically loose all typing for TypeAdapter(int) by doing this?


# 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:
...

def __init__(
self, type: Any, *, config: ConfigDict | None = None, _parent_depth: int = 2, module: str | None = None
self,
type: type[T],
Copy link
Member

@adriangb adriangb Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work now? What about passing in int | str with mypy as per comment above?

Copy link
Member

Choose a reason for hiding this comment

The 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])

-->

test.py:5: error: Argument 1 to "TypeAdapter" has incompatible type "object"; expected "Union[Type[int], Type[str]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Viicos,

Is there any downside of leaving the above overloads within the if TYPE_CHECKING block? I think it'd be preferred to leave those in place.

*,
config: ConfigDict | None = None,
_parent_depth: int = 2,
module: str | None = None,
) -> None:
"""Initializes the TypeAdapter object.

Expand Down