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

[DOP-14540] Fix Pydantic v2 warnings #47

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ jobs:
${{ runner.os }}-python-${{ env.DEFAULT_PYTHON }}-changelog-${{ hashFiles('**/poetry.lock') }}
${{ runner.os }}-python-${{ env.DEFAULT_PYTHON }}-changelog-
${{ runner.os }}-python
${{ runner.os }}-

- name: Install dependencies
run: |
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ jobs:
${{ runner.os }}-python-${{ env.DEFAULT_PYTHON }}-codeql-${{ hashFiles('**/poetry.lock') }}
${{ runner.os }}-python-${{ env.DEFAULT_PYTHON }}-codeql-
${{ runner.os }}-python
${{ runner.os }}-

- name: Install dependencies
run: |
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ jobs:
uses: actions/cache@v4
with:
path: ~/.cache/pypoetry
key: pypi-release-${{ runner.os }}-py-${{ hashFiles('**/poetry.lock') }}-${{ env.DEFAULT_PYTHON }}
key: ${{ runner.os }}-python-${{ env.DEFAULT_PYTHON }}-release-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
pypi-release-${{ runner.os }}-py-${{ hashFiles('**/poetry.lock') }}-
pypi-release-${{ runner.os }}-py-
${{ runner.os }}-python-${{ env.DEFAULT_PYTHON }}-release-${{ hashFiles('**/poetry.lock') }}
${{ runner.os }}-python-${{ env.DEFAULT_PYTHON }}-release-
${{ runner.os }}-python

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
run: |
poetry install --no-root --all-extras --with test --without dev,docs
if [ "${{ matrix.pydantic-version }}" == "1" ]; then
pip install "pydantic<2.0.0"
poetry run pip install "pydantic<2.0.0"
fi

- name: Run Tests
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/next_release/47.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix Pydantic v2 model warnings while starting backend.
1 change: 1 addition & 0 deletions horizon/backend/api/v1/router/hwm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2023-2024 MTS (Mobile Telesystems)
# SPDX-License-Identifier: Apache-2.0

# mypy: disable-error-code="pydantic-orm"

from fastapi import APIRouter, Depends, status
from typing_extensions import Annotated
Expand Down
2 changes: 2 additions & 0 deletions horizon/backend/api/v1/router/namespaces.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2023-2024 MTS (Mobile Telesystems)
# SPDX-License-Identifier: Apache-2.0

# mypy: disable-error-code="pydantic-orm"

from fastapi import APIRouter, Depends, HTTPException, status
from typing_extensions import Annotated

Expand Down
2 changes: 2 additions & 0 deletions horizon/backend/api/v1/router/users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2023-2024 MTS (Mobile Telesystems)
# SPDX-License-Identifier: Apache-2.0

# mypy: disable-error-code="pydantic-orm"

from typing import Union

from fastapi import APIRouter, Depends
Expand Down
12 changes: 8 additions & 4 deletions horizon/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from pydantic import AnyHttpUrl, BaseModel, PrivateAttr, ValidationError
from pydantic import __version__ as pydantic_version
from pydantic import parse_obj_as, validator
from pydantic.generics import GenericModel

if pydantic_version >= "2":
from pydantic import BaseModel as GenericModel
else:
from pydantic.generics import GenericModel # type: ignore[no-redef] # noqa: WPS440
from typing_extensions import Protocol

import horizon
Expand Down Expand Up @@ -58,9 +62,9 @@ class Config:
@classmethod
def session_class(cls) -> type[SessionClass]:
# Get `Session` from `SyncClient(BaseClient[Session])`
if pydantic_version < "2":
return cls.__bases__[0].__annotations__["session"].__args__[0]
return cls.model_fields["session"].annotation.__args__[0]
if pydantic_version >= "2":
return cls.model_fields["session"].annotation.__args__[0] # type: ignore[union-attr]
return cls.__bases__[0].__annotations__["session"].__args__[0]

@validator("base_url")
def _validate_url(cls, value: AnyHttpUrl):
Expand Down
6 changes: 5 additions & 1 deletion horizon/commons/errors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

from pydantic import BaseModel
from pydantic import __version__ as pydantic_version
from pydantic.generics import GenericModel

if pydantic_version >= "2":
from pydantic import BaseModel as GenericModel
else:
from pydantic.generics import GenericModel # type: ignore[no-redef] # noqa: WPS440

from horizon.commons.dto.unset import Unset

Expand Down
1 change: 1 addition & 0 deletions horizon/commons/errors/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
"NotAuthorizedSchema",
"NotFoundSchema",
"BadRequestError",
"PermissionDeniedSchema",
]
8 changes: 4 additions & 4 deletions horizon/commons/errors/schemas/invalid_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class InvalidRequestBaseErrorSchema(BaseModel):
input: Any = Field(default=None)

class Config:
# pydantic v1
allow_population_by_field_name = True
# pydantic v2
populate_by_name = True
if pydantic_version >= "2":
populate_by_name = True
else:
allow_population_by_field_name = True


@register_error_response(
Expand Down
12 changes: 7 additions & 5 deletions horizon/commons/schemas/v1/hwm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from datetime import datetime
from typing import Any, List, Optional, Union

from pydantic import BaseModel, Field, root_validator, validator
from pydantic import BaseModel, Field
from pydantic import __version__ as pydantic_version
from pydantic import root_validator, validator

from horizon.commons.dto import Unset
from horizon.commons.schemas.v1.pagination import PaginateQueryV1
Expand All @@ -30,10 +32,10 @@ class HWMResponseV1(BaseModel):
changed_by: Optional[str] = Field(default=None, description="Latest user who changed the HWM data")

class Config:
# pydantic v1
orm_mode = True
# pydantic v2
from_attributes = True
if pydantic_version >= "2":
from_attributes = True
else:
orm_mode = True


class HWMListResponseV1(BaseModel):
Expand Down
9 changes: 5 additions & 4 deletions horizon/commons/schemas/v1/hwm_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any, Optional

from pydantic import BaseModel, Field
from pydantic import __version__ as pydantic_version

from horizon.commons.schemas.v1.pagination import PaginateQueryV1

Expand Down Expand Up @@ -37,7 +38,7 @@ class HWMHistoryResponseV1(BaseModel):
changed_by: Optional[str] = Field(default=None, description="User who changed the HWM data")

class Config:
# pydantic v1
orm_mode = True
# pydantic v2
from_attributes = True
if pydantic_version >= "2":
from_attributes = True
else:
orm_mode = True
12 changes: 7 additions & 5 deletions horizon/commons/schemas/v1/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from enum import Enum
from typing import Optional, Union

from pydantic import BaseModel, Field, root_validator
from pydantic import BaseModel, Field
from pydantic import __version__ as pydantic_version
from pydantic import root_validator

from horizon.commons.dto import Unset
from horizon.commons.schemas.v1.pagination import PaginateQueryV1
Expand All @@ -29,10 +31,10 @@ class NamespaceResponseV1(BaseModel):
changed_by: Optional[str] = Field(default=None, description="Latest user who changed the namespace data")

class Config:
# pydantic v1
orm_mode = True
# pydantic v2
from_attributes = True
if pydantic_version >= "2":
from_attributes = True
else:
orm_mode = True


class NamespacePaginateQueryV1(PaginateQueryV1):
Expand Down
9 changes: 5 additions & 4 deletions horizon/commons/schemas/v1/namespace_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Optional

from pydantic import BaseModel, Field
from pydantic import __version__ as pydantic_version

from horizon.commons.schemas.v1.pagination import PaginateQueryV1

Expand All @@ -30,7 +31,7 @@ class NamespaceHistoryResponseV1(BaseModel):
changed_by: Optional[str] = Field(default=None, description="User who changed the namespace data")

class Config:
# pydantic v1
orm_mode = True
# pydantic v2
from_attributes = True
if pydantic_version >= "2":
from_attributes = True
else:
orm_mode = True
7 changes: 6 additions & 1 deletion horizon/commons/schemas/v1/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from typing import Generic, List, Optional, TypeVar

from pydantic import BaseModel, Field
from pydantic.generics import GenericModel
from pydantic import __version__ as pydantic_version

if pydantic_version >= "2":
from pydantic import BaseModel as GenericModel
else:
from pydantic.generics import GenericModel # type: ignore[no-redef] # noqa: WPS440

from horizon.commons.dto import Pagination

Expand Down
5 changes: 4 additions & 1 deletion horizon/commons/schemas/v1/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class PermissionsUpdateRequestV1(BaseModel):
)

@validator("permissions")
def _ensure_unique_usernames_and_single_owner(cls, permissions):
def _ensure_unique_usernames_and_single_owner(
cls,
permissions: List[PermissionUpdateRequestItemV1],
) -> List[PermissionUpdateRequestItemV1]:
seen: Set[str] = set()
owner_count = 0
for perm in permissions:
Expand Down
9 changes: 5 additions & 4 deletions horizon/commons/schemas/v1/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

from pydantic import BaseModel, Field
from pydantic import __version__ as pydantic_version


class UserResponseV1(BaseModel):
Expand All @@ -12,10 +13,10 @@ class UserResponseV1(BaseModel):
username: str = Field(description="User name, unique in the entire database")

class Config:
# pydantic v1
orm_mode = True
# pydantic v2
from_attributes = True
if pydantic_version >= "2":
from_attributes = True
else:
orm_mode = True


class UserResponseV1WithAdmin(UserResponseV1):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ known_first_party = ["horizon", "tests"]
[tool.mypy]
python_version = "3.8"
plugins = ["pydantic.mypy", "sqlalchemy.ext.mypy.plugin"]
strict_optional = true

[[tool.mypy.overrides]]
module = "sqlalchemy_utils"
Expand Down
Loading
Loading