Skip to content

Commit 1cb6a18

Browse files
Add the ability to support pydantic 1 and 2 (#281)
* 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>
1 parent f0d9a04 commit 1cb6a18

File tree

6 files changed

+280
-247
lines changed

6 files changed

+280
-247
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ jobs:
114114
fail-fast: true
115115
matrix:
116116
python-version: ["3.8", "3.9", "3.10", "3.11"]
117+
pydantic: ["2.x"]
118+
include:
119+
- python-version: "3.11"
120+
pydantic: "1.x"
117121
runs-on: "ubuntu-20.04"
118122
env:
119123
INVOKE_LOCAL: "True"
@@ -128,6 +132,9 @@ jobs:
128132
poetry-install-options: "--with dev"
129133
- name: "Run poetry Install"
130134
run: "poetry install"
135+
- name: "Run poetry Install"
136+
run: "pip install pydantic==1.10.13"
137+
if: matrix.pydantic == '1.x'
131138
- name: "Run Tests"
132139
run: "poetry run invoke pytest --local"
133140
needs:

circuit_maintenance_parser/output.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88

99
from typing import List
1010

11-
from pydantic import field_validator, BaseModel, StrictStr, StrictInt, PrivateAttr
11+
try:
12+
from pydantic import field_validator
13+
except ImportError:
14+
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
15+
from pydantic import validator as field_validator # type: ignore
16+
17+
18+
from pydantic import BaseModel, StrictStr, StrictInt, PrivateAttr
1219

1320

1421
class Impact(str, Enum):

circuit_maintenance_parser/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ class Parser(BaseModel):
4242
@classmethod
4343
def get_data_types(cls) -> List[str]:
4444
"""Return the expected data type."""
45-
return cls._data_types.get_default()
45+
try:
46+
return cls._data_types.get_default()
47+
except AttributeError:
48+
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
49+
return cls._data_types
4650

4751
@classmethod
4852
def get_name(cls) -> str:

circuit_maintenance_parser/provider.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,38 @@ def get_maintenances(self, data: NotificationData) -> Iterable[Maintenance]:
151151
@classmethod
152152
def get_default_organizer(cls) -> str:
153153
"""Expose default_organizer as class attribute."""
154-
return cls._default_organizer.get_default() # type: ignore
154+
try:
155+
return cls._default_organizer.get_default() # type: ignore
156+
except AttributeError:
157+
# TODO: This exception handling is required for Pydantic 1.x compatibility. To be removed when the dependency is deprecated.
158+
return cls._default_organizer
155159

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

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

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

171187
@classmethod
172188
def get_extended_data(cls):

0 commit comments

Comments
 (0)