Skip to content

Commit

Permalink
Fix various exceptions under Pydantic 1.x (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmatthews authored Jun 4, 2024
1 parent c9e6bb8 commit 15446a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
10 changes: 8 additions & 2 deletions circuit_maintenance_parser/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ def validate_empty_strings(cls, value):
@classmethod
def validate_empty_circuits(cls, value, values):
"""Validate non-cancel notifications have a populated circuit list."""
values = values.data
try:
values = values.data # pydantic 2.x
except AttributeError: # pydantic 1.x
pass
if len(value) < 1 and values["status"] not in (Status.CANCELLED, Status.COMPLETED):
raise ValueError("At least one circuit has to be included in the maintenance")
return value
Expand All @@ -213,7 +216,10 @@ def validate_empty_circuits(cls, value, values):
@classmethod
def validate_end_time(cls, end, values):
"""Validate that End time happens after Start time."""
values = values.data
try:
values = values.data # pydantic 2.x
except AttributeError: # pydantic 1.x
pass
if "start" not in values:
raise ValueError("Start time is a mandatory attribute.")
start = values["start"]
Expand Down
2 changes: 1 addition & 1 deletion circuit_maintenance_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_data_types(cls) -> List[str]:
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
return cls()._data_types

@classmethod
def get_name(cls) -> str:
Expand Down
8 changes: 4 additions & 4 deletions circuit_maintenance_parser/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def get_default_organizer(cls) -> str:
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
return cls()._default_organizer

@classmethod
def get_default_processors(cls) -> List[GenericProcessor]:
Expand All @@ -164,7 +164,7 @@ def get_default_processors(cls) -> List[GenericProcessor]:
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
return cls()._processors

@classmethod
def get_default_include_filters(cls) -> Dict[str, List[str]]:
Expand All @@ -173,7 +173,7 @@ def get_default_include_filters(cls) -> Dict[str, List[str]]:
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
return cls()._include_filter

@classmethod
def get_default_exclude_filters(cls) -> Dict[str, List[str]]:
Expand All @@ -182,7 +182,7 @@ def get_default_exclude_filters(cls) -> Dict[str, List[str]]:
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
return cls()._exclude_filter

@classmethod
def get_extended_data(cls):
Expand Down

0 comments on commit 15446a0

Please sign in to comment.