Skip to content

Commit

Permalink
Add the ability to support pydantic 1 and 2 (#281)
Browse files Browse the repository at this point in the history
* Add the ability to support pydantic 1 and 2

* make mypy happy

* fix black

* pydocstyle

* fix lint

* Add deprecation comments

---------

Co-authored-by: Christian Adell <chadell@gmail.com>
  • Loading branch information
itdependsnetworks and chadell authored Apr 4, 2024
1 parent f0d9a04 commit 1cb6a18
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 247 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ jobs:
fail-fast: true
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
pydantic: ["2.x"]
include:
- python-version: "3.11"
pydantic: "1.x"
runs-on: "ubuntu-20.04"
env:
INVOKE_LOCAL: "True"
Expand All @@ -128,6 +132,9 @@ jobs:
poetry-install-options: "--with dev"
- name: "Run poetry Install"
run: "poetry install"
- name: "Run poetry Install"
run: "pip install pydantic==1.10.13"
if: matrix.pydantic == '1.x'
- name: "Run Tests"
run: "poetry run invoke pytest --local"
needs:
Expand Down
9 changes: 8 additions & 1 deletion circuit_maintenance_parser/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

from typing import List

from pydantic import field_validator, BaseModel, StrictStr, StrictInt, PrivateAttr
try:
from pydantic import field_validator
except ImportError:
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
from pydantic import validator as field_validator # type: ignore


from pydantic import BaseModel, StrictStr, StrictInt, PrivateAttr


class Impact(str, Enum):
Expand Down
6 changes: 5 additions & 1 deletion circuit_maintenance_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ class Parser(BaseModel):
@classmethod
def get_data_types(cls) -> List[str]:
"""Return the expected data type."""
return cls._data_types.get_default()
try:
return cls._data_types.get_default()
except AttributeError:
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
return cls._data_types

@classmethod
def get_name(cls) -> str:
Expand Down
24 changes: 20 additions & 4 deletions circuit_maintenance_parser/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,38 @@ def get_maintenances(self, data: NotificationData) -> Iterable[Maintenance]:
@classmethod
def get_default_organizer(cls) -> str:
"""Expose default_organizer as class attribute."""
return cls._default_organizer.get_default() # type: ignore
try:
return cls._default_organizer.get_default() # type: ignore
except AttributeError:
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
return cls._default_organizer

@classmethod
def get_default_processors(cls) -> List[GenericProcessor]:
"""Expose default_processors as class attribute."""
return cls._processors.get_default() # type: ignore
try:
return cls._processors.get_default() # type: ignore
except AttributeError:
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
return cls._processors

@classmethod
def get_default_include_filters(cls) -> Dict[str, List[str]]:
"""Expose include_filter as class attribute."""
return cls._include_filter.get_default() # type: ignore
try:
return cls._include_filter.get_default() # type: ignore
except AttributeError:
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
return cls._include_filter

@classmethod
def get_default_exclude_filters(cls) -> Dict[str, List[str]]:
"""Expose exclude_filter as class attribute."""
return cls._exclude_filter.get_default() # type: ignore
try:
return cls._exclude_filter.get_default() # type: ignore
except AttributeError:
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
return cls._exclude_filter

@classmethod
def get_extended_data(cls):
Expand Down
Loading

0 comments on commit 1cb6a18

Please sign in to comment.