Skip to content

Commit

Permalink
Make compatible with pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Dec 19, 2023
1 parent 3151b2b commit b41fe35
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 27 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_long_description() -> str:
"scipy",
"pyyaml",
"pandas",
"pydantic<2",
"pydantic",
"sympy",
"typing_extensions",
],
Expand Down
7 changes: 5 additions & 2 deletions src/open_petro_elastic/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import numpy as np
import pandas as pd
import yaml
from pydantic import parse_obj_as

import open_petro_elastic
try:
from pydantic.v1 import parse_obj_as
except ImportError:
from pydantic import parse_obj_as

import open_petro_elastic
from open_petro_elastic.config.input import ColumnInsertionError, Input
from open_petro_elastic.material import fluid_substitution

Expand Down
5 changes: 4 additions & 1 deletion src/open_petro_elastic/config/coefficients.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Optional, Union

from pydantic.dataclasses import dataclass
try:
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic.dataclasses import dataclass

from open_petro_elastic.material import polyval_material

Expand Down
9 changes: 8 additions & 1 deletion src/open_petro_elastic/config/constituent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from typing import Optional
from typing_extensions import Literal

import numpy as np
from pydantic import validator
from pydantic.dataclasses import dataclass
from typing_extensions import Literal

try:
from pydantic.v1 import validator
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import validator
from pydantic.dataclasses import dataclass

from open_petro_elastic.config.fluid_model_providers import fluid_model_providers
from open_petro_elastic.material import Material
Expand Down
7 changes: 6 additions & 1 deletion src/open_petro_elastic/config/depth_trend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import Optional

import numpy as np
from pydantic.dataclasses import dataclass

try:
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic.dataclasses import dataclass

from typing_extensions import Literal

from .coefficients import PolyfitCoefficients
Expand Down
9 changes: 7 additions & 2 deletions src/open_petro_elastic/config/dry_rock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
from typing import List, Union

import numpy as np
from pydantic import root_validator
from pydantic.dataclasses import dataclass

try:
from pydantic.v1 import root_validator
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import root_validator
from pydantic.dataclasses import dataclass

from .depth_trend import DepthTrend
from .model import Polyfit2dModel, SandstoneModel
Expand Down
13 changes: 10 additions & 3 deletions src/open_petro_elastic/config/fluids.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from dataclasses import field

import numpy as np
from pydantic import conlist, validator
from pydantic.dataclasses import dataclass
from typing_extensions import Literal

try:
from pydantic.v1 import conlist, validator
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import conlist, validator
from pydantic.dataclasses import dataclass

from typing import List, Optional

from typing_extensions import Literal

from open_petro_elastic.material import brie_fluid_mixing, wood_fluid_mixing

from .constituent import Constituent, fix_one_fraction
Expand Down
9 changes: 7 additions & 2 deletions src/open_petro_elastic/config/input.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from dataclasses import field

import pandas as pd
from pydantic import root_validator
from pydantic.dataclasses import dataclass

try:
from pydantic.v1 import root_validator
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import root_validator
from pydantic.dataclasses import dataclass

from .dry_rock import DryRock
from .fluids import Fluids
Expand Down
6 changes: 5 additions & 1 deletion src/open_petro_elastic/config/material_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from typing import Optional, Union

from pydantic.dataclasses import dataclass
try:
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic.dataclasses import dataclass

from typing_extensions import Literal

from open_petro_elastic.material import Material
Expand Down
9 changes: 7 additions & 2 deletions src/open_petro_elastic/config/minerals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from dataclasses import field

import numpy as np
from pydantic import conlist
from pydantic.dataclasses import dataclass

try:
from pydantic.v1 import conlist
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import conlist
from pydantic.dataclasses import dataclass

from open_petro_elastic.float_vectorize import float_vectorize
from open_petro_elastic.material import Material, hashin_shtrikman_walpole
Expand Down
12 changes: 9 additions & 3 deletions src/open_petro_elastic/config/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import abc
from typing import Optional, Union
from dataclasses import field
from typing import Optional, Union

import numpy as np
from pydantic import parse_obj_as
from pydantic.dataclasses import dataclass

try:
from pydantic.v1 import parse_obj_as
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import parse_obj_as
from pydantic.dataclasses import dataclass

from typing_extensions import Literal

from open_petro_elastic.material import Material, hashin_shtrikman_walpole
Expand Down
9 changes: 7 additions & 2 deletions src/open_petro_elastic/config/pressure.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from typing import Optional

import numpy as np
from pydantic import root_validator
from pydantic.dataclasses import dataclass

try:
from pydantic.v1 import root_validator
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import root_validator
from pydantic.dataclasses import dataclass

from .pydantic_config import PetroElasticConfig
from .vector_type import Array
Expand Down
6 changes: 5 additions & 1 deletion src/open_petro_elastic/config/pressure_dependency.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from pydantic.dataclasses import dataclass
try:
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic.dataclasses import dataclass

from typing_extensions import Literal

from .model import PressureDependencyModel
Expand Down
15 changes: 10 additions & 5 deletions tests/test_coefficients.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import numpy as np
import pydantic

try:
from pydantic.v1 import ValidationError, parse_obj_as
except ImportError:
from pydantic import parse_obj_as, ValidationError

import pytest

from open_petro_elastic.config.coefficients import Coefficients


def test_read_coefficients_use_moduli_true_order():
c = pydantic.parse_obj_as(
c = parse_obj_as(
Coefficients, {"bulk_modulus": [2], "shear_modulus": [3], "density": [1]}
)
assert c.use_moduli
assert np.all(c.as_list == [[1], [2], [3]])


def test_read_coefficients_use_velocity_false():
c = pydantic.parse_obj_as(
c = parse_obj_as(
Coefficients,
{"primary_velocity": [2], "secondary_velocity": [3], "density": [1]},
)
Expand All @@ -23,7 +28,7 @@ def test_read_coefficients_use_velocity_false():


def test_read_coefficients_use_velocity_mix_throws():
with pytest.raises(pydantic.ValidationError):
pydantic.parse_obj_as(
with pytest.raises(ValidationError):
parse_obj_as(
Coefficients, {"some other string": [], "shear_modulus": [], "density": []}
)

0 comments on commit b41fe35

Please sign in to comment.