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

Attempt lite migration to pydantic v2 #1796

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion optimade/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import re
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union

from pydantic import BaseModel # pylint: disable=no-name-in-module
from pydantic.v1 import BaseModel # pylint: disable=no-name-in-module

from optimade.adapters.logger import LOGGER
from optimade.models.entries import EntryResource
Expand Down
2 changes: 1 addition & 1 deletion optimade/models/baseinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from typing import Dict, List, Optional

from pydantic import AnyHttpUrl, BaseModel, Field, root_validator, validator
from pydantic.v1 import AnyHttpUrl, BaseModel, Field, root_validator, validator

from optimade.models.jsonapi import Resource
from optimade.models.utils import SemanticVersion, StrictField
Expand Down
2 changes: 1 addition & 1 deletion optimade/models/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from typing import Dict, List, Optional

from pydantic import BaseModel, validator # pylint: disable=no-name-in-module
from pydantic.v1 import BaseModel, validator # pylint: disable=no-name-in-module

from optimade.models.jsonapi import Attributes, Relationships, Resource
from optimade.models.optimade_json import DataType, Relationship
Expand Down
2 changes: 1 addition & 1 deletion optimade/models/index_metadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from typing import Dict, Union

from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
from pydantic.v1 import BaseModel, Field # pylint: disable=no-name-in-module

from optimade.models.baseinfo import BaseInfoAttributes, BaseInfoResource
from optimade.models.jsonapi import BaseResource
Expand Down
2 changes: 1 addition & 1 deletion optimade/models/jsonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Type, Union

from pydantic import ( # pylint: disable=no-name-in-module
from pydantic.v1 import ( # pylint: disable=no-name-in-module
AnyUrl,
BaseModel,
parse_obj_as,
Expand Down
2 changes: 1 addition & 1 deletion optimade/models/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from typing import Optional, Union

from pydantic import AnyUrl, root_validator # pylint: disable=no-name-in-module
from pydantic.v1 import AnyUrl, root_validator # pylint: disable=no-name-in-module

from optimade.models.entries import EntryResource
from optimade.models.jsonapi import Attributes, Link
Expand Down
2 changes: 1 addition & 1 deletion optimade/models/optimade_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional, Type, Union

from pydantic import AnyHttpUrl, AnyUrl, BaseModel, EmailStr, root_validator
from pydantic.v1 import AnyHttpUrl, AnyUrl, BaseModel, EmailStr, root_validator

from optimade.models import jsonapi
from optimade.models.utils import SemanticVersion, StrictField
Expand Down
7 changes: 6 additions & 1 deletion optimade/models/references.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# pylint: disable=line-too-long,no-self-argument
from typing import List, Optional

from pydantic import AnyUrl, BaseModel, validator # pylint: disable=no-name-in-module
from pydantic.v1 import (
AnyUrl,
BaseModel,
validator,
)

# pylint: disable=no-name-in-module
from optimade.models.entries import EntryResource, EntryResourceAttributes
from optimade.models.utils import OptimadeField, SupportLevel

Expand Down
2 changes: 1 addition & 1 deletion optimade/models/responses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pylint: disable=no-self-argument
from typing import Any, Dict, List, Optional, Union

from pydantic import Field, root_validator
from pydantic.v1 import Field, root_validator

from optimade.models.baseinfo import BaseInfoResource
from optimade.models.entries import EntryInfoResource, EntryResource
Expand Down
2 changes: 1 addition & 1 deletion optimade/models/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum, IntEnum
from typing import List, Optional, Union

from pydantic import BaseModel, conlist, root_validator, validator
from pydantic.v1 import BaseModel, conlist, root_validator, validator

from optimade.models.entries import EntryResource, EntryResourceAttributes
from optimade.models.utils import (
Expand Down
4 changes: 2 additions & 2 deletions optimade/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from functools import reduce
from typing import TYPE_CHECKING, List, Optional, Union

from pydantic import Field
from pydantic.fields import FieldInfo
from pydantic.v1 import Field
from pydantic.v1.fields import FieldInfo

if TYPE_CHECKING: # pragma: no cover
from typing import Any
Expand Down
19 changes: 9 additions & 10 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Tuple, Union

from pydantic import ( # pylint: disable=no-name-in-module
from pydantic.v1 import ( # pylint: disable=no-name-in-module
AnyHttpUrl,
BaseSettings,
Field,
root_validator,
validator,
)
from pydantic.env_settings import SettingsSourceCallable
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource

from optimade import __api_version__, __version__
from optimade.models import Implementation, Provider
Expand Down Expand Up @@ -71,14 +70,14 @@ def config_file_settings(settings: BaseSettings) -> Dict[str, Any]:
"""Configuration file settings source.

Based on the example in the
[pydantic documentation](https://pydantic-docs.helpmanual.io/usage/settings/#adding-sources),
[pydantic documentation](https://docs.pydantic.dev/latest/usage/pydantic_settings/#customise-settings-sources),
this function loads ServerConfig settings from a configuration file.

The file must be of either type JSON or YML/YAML.

Parameters:
settings: The `pydantic.BaseSettings` class using this function as a
`pydantic.SettingsSourceCallable`.
settings: The `pydantic_settings.BaseSettings` class using this function as a
`pydantic_settings.PydanticBaseSettingsSource`.

Returns:
Dictionary of settings as read from a file.
Expand Down Expand Up @@ -345,10 +344,10 @@ class Config:
@classmethod
def customise_sources(
cls,
init_settings: SettingsSourceCallable,
env_settings: SettingsSourceCallable,
file_secret_settings: SettingsSourceCallable,
) -> Tuple[SettingsSourceCallable, ...]:
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
"""
**Priority of config settings sources**:

Expand Down
2 changes: 1 addition & 1 deletion optimade/server/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError, StarletteHTTPException
from lark.exceptions import VisitError
from pydantic import ValidationError
from pydantic.v1 import ValidationError

from optimade.exceptions import BadRequest, OptimadeHTTPException
from optimade.models import ErrorResponse, ErrorSource, OptimadeError
Expand Down
2 changes: 1 addition & 1 deletion optimade/server/query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from warnings import warn

from fastapi import Query
from pydantic import EmailStr # pylint: disable=no-name-in-module
from pydantic.v1 import EmailStr # pylint: disable=no-name-in-module

from optimade.exceptions import BadRequest
from optimade.server.config import CONFIG
Expand Down
2 changes: 1 addition & 1 deletion optimade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
from typing import Container, Iterable, List, Optional

from pydantic import ValidationError
from pydantic.v1 import ValidationError

from optimade.models.links import LinksResource

Expand Down
2 changes: 1 addition & 1 deletion optimade/validator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from typing import Any, Container, Dict, List, Set

from pydantic import BaseSettings, Field
from pydantic.v1 import BaseSettings, Field

from optimade.models import (
DataType,
Expand Down
4 changes: 2 additions & 2 deletions optimade/validator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from typing import Any, Callable, Dict, List, Optional, Tuple

import requests
from pydantic import Field, ValidationError
from pydantic.v1 import Field, ValidationError

from optimade import __version__
from optimade.models import (
Expand Down Expand Up @@ -366,7 +366,7 @@ def wrapper(
else:
message = msg.split("\n")
if validator.verbosity > 1:
# ValidationErrors from pydantic already include very detailed errors
# ValidationErrors from pydantic.v1 already include very detailed errors
# that get duplicated in the traceback
if not isinstance(result, ValidationError):
message += traceback.split("\n")
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ classifiers = [
requires-python = ">=3.9"
dependencies = [
"lark~=1.1",
"pydantic~=1.10,>=1.10.2,!=1.10.7",
"pydantic~=2.0",
"pydantic-settings~=2.0",
"email_validator>=1.2",
"requests~=2.28",
]
Expand Down Expand Up @@ -65,7 +66,6 @@ elastic = ["elasticsearch-dsl~=7.4,<8.0", "elasticsearch~=7.17"]
mongo = ["pymongo~=4.0", "mongomock~=4.1"]
server = [
"uvicorn[standard]~=0.19",
"fastapi>=0.103.1",
"pyyaml~=6.0",
"optimade[mongo]",
]
Expand Down Expand Up @@ -150,6 +150,7 @@ filterwarnings = [
"ignore:.*has an unrecognised prefix.*:",
"ignore:.*pkg_resources is deprecated as an API*:DeprecationWarning",
"ignore:.*Deprecated call to `pkg_resources.declare_namespace*:DeprecationWarning",
"ignore:.*Support for class-based `config` is deprecated, use ConfigDict instead*:DeprecationWarning",
]
testpaths = "tests"
addopts = "-rs"
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
email_validator==2.0.0.post2
lark==1.1.7
pydantic==1.10.9
pydantic==2.4.1
pydantic_settings==2.0.3
pyyaml==6.0
requests==2.31.0
uvicorn==0.23.2
2 changes: 1 addition & 1 deletion tests/models/test_entries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from pydantic import ValidationError
from pydantic.v1 import ValidationError

from optimade.models.entries import EntryRelationships

Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_jsonapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from pydantic import ValidationError
from pydantic.v1 import ValidationError


def test_hashability():
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_good_links(starting_links, mapper):

def test_bad_links(starting_links, mapper):
"""Check badly formed links"""
from pydantic import ValidationError
from pydantic.v1 import ValidationError

bad_links = [
{"aggregate": "wrong"},
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_optimade_json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from pydantic import ValidationError
from pydantic.v1 import ValidationError

from optimade.models import DataType, Provider

Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_references.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=no-member
import pytest
from pydantic import ValidationError
from pydantic.v1 import ValidationError

from optimade.models.references import ReferenceResource

Expand All @@ -22,7 +22,7 @@ def test_more_good_references(good_references, mapper):

def test_bad_references(mapper):
"""Check badly formed references"""
from pydantic import ValidationError
from pydantic.v1 import ValidationError

bad_refs = [
{"id": "AAAA", "type": "references", "doi": "10.1234/1234"}, # bad id
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import itertools

import pytest
from pydantic import ValidationError
from pydantic.v1 import ValidationError

from optimade.models.structures import CORRELATED_STRUCTURE_FIELDS, StructureResource
from optimade.warnings import MissingExpectedField
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Callable, List

import pytest
from pydantic import BaseModel, Field, ValidationError
from pydantic.v1 import BaseModel, Field, ValidationError

from optimade.models.utils import OptimadeField, StrictField, SupportLevel

Expand Down
Loading