refactor: replace ValueError and KeyError with custom exception types#385
Conversation
mgovers
left a comment
There was a problem hiding this comment.
Hi @jinukuntlaakhilakumargoud-web,
Thank you for your contribution! Overall, it seems to be in a good state, but I will not be able to do a full review today. Here's some initial feedback on a couple things that caught my eye.
Hope that helps.
|
Need to update tests as well, as the tests are currently expecting |
|
Hi @mgovers and @furqan463, Thank you for the feedback! I have pushed a new commit that addresses all the points:
|
Great, and please make signed commits to avoid DCO failure. |
…closes PowerGridModel#384) Signed-off-by: jinukuntlaakhilakumargoud-web <jinukuntlaakhilakumargoud@gmail.com>
- Updated tests in test_tabular_converter to expect InvalidDataFormatError and InvalidComponentTypeError - Un-exported custom exception classes from __init__.py as per maintainer feedback - Renamed exceptions.py to errors.py to prevent potential shadowing - Reverted NotImplementedError replacements in test_serialize_data Signed-off-by: jinukuntlaakhilakumargoud-web <jinukuntlaakhilakumargoud@gmail.com>
b48efe5 to
60f40f0
Compare
|
@furqan463 Done! I've rebased the commits and added the |
ceef25a to
adb56e7
Compare
|
I have addressed all the comments:
|
|
adb56e7 to
b0b6124
Compare
Signed-off-by: jinukuntlaakhilakumargoud-web <jinukuntlaakhilakumargoud@gmail.com>
b0b6124 to
367b993
Compare
import_diagnostic.txt
Outdated
There was a problem hiding this comment.
these auto-generated files should not be present in the git repo
There was a problem hiding this comment.
Thanks for catching that! I've removed the auto-generated diagnostic files (import_diagnostic.txt and export_diagnostic.txt) from the repository and updated .gitignore so they aren't accidentally committed again. The changes have been pushed to the PR!
There was a problem hiding this comment.
@jinukuntlaakhilakumargoud-web text files are still there.
There was a problem hiding this comment.
@furqan463 You're right, I accidentally committed those test_collect_output*.txt files. I have removed them now, so no extra text files should remain.
Signed-off-by: jinukuntlaakhilakumargoud-web <jinukuntlaakhilakumargoud@gmail.com>
|
@jinukuntlaakhilakumargoud-web no need to add these to .gitignore. |
There was a problem hiding this comment.
did the line endings change? were they \r\n before instead of plain \n (in that case, you can keep it as-is) or did you change them to \r\n?
There was a problem hiding this comment.
I've fixed the line endings back to \n. Thanks for catching that.
.gitignore
Outdated
| docs/Makefile | ||
|
|
||
| # Ignore diagnostic output | ||
| *diagnostic.txt |
There was a problem hiding this comment.
were the test_collect_output files auto-generated or did you manually add them yourself? if the former, please also add test_collect_output*.txt here. If the latter, please do not add it here.
There was a problem hiding this comment.
The test_collect_output*.txt files were manually generated when I was redirecting test output and were accidentally committed. I have removed them from the repository entirely and reverted the change to .gitignore that added them
figueroa1395
left a comment
There was a problem hiding this comment.
Mainly nitpicks. Thanks for the contribution!
| # Ignore diagnostic output | ||
| *diagnostic.txt |
There was a problem hiding this comment.
I believe this is very specific to your workflow. Can you remove it and just manually delete or not commit these files?
There was a problem hiding this comment.
Thank you for the feedback. I've removed the diagnostic file entries from .gitignore.
|
|
||
|
|
||
| class ComponentNotFoundError(KeyError, PowerGridModelIoError): | ||
| """Raised when a required component or attribute is missing.""" |
There was a problem hiding this comment.
Let's separate components from attributes.
| """Raised when a required component or attribute is missing.""" | |
| """Raised when a required component is missing.""" |
There was a problem hiding this comment.
Good catch. I've updated the docstring to only refer to components, as suggested.
| class AttributeNotFoundError(ComponentNotFoundError): | ||
| """Raised when an attribute is missing from a component/dataset.""" |
There was a problem hiding this comment.
With reference to https://github.com/PowerGridModel/power-grid-model-io/pull/385/changes#r2883076143. Let's keep the attribute related error independent:
| class AttributeNotFoundError(ComponentNotFoundError): | |
| """Raised when an attribute is missing from a component/dataset.""" | |
| class AttributeNotFoundError(KeyError, PowerGridModelIoError): | |
| """Raised when a required component attribute is missing.""" |
There was a problem hiding this comment.
Thank you for the suggestion. I've updated AttributeNotFoundError to inherit from KeyError, PowerGridModelIoError directly.
|
|
||
|
|
||
| class InvalidDatasetTypeError(ValueError, PowerGridModelIoError): | ||
| """Raised when an invalid dataset type (e.g. input/output) is encountered.""" |
There was a problem hiding this comment.
| """Raised when an invalid dataset type (e.g. input/output) is encountered.""" | |
| """Raised when an invalid dataset type (e.g. other than input, output, or update) is encountered.""" |
There was a problem hiding this comment.
Updated the docstring as suggested. Thank you.
| raise InvalidDatasetTypeError( | ||
| f"Invalid component type '{component_str}' or data type '{data_type_str}'" | ||
| ) from ex |
There was a problem hiding this comment.
This exception lies in between InvalidDatasetTypeError and InvalidComponentTypeError. Can you split it?
There was a problem hiding this comment.
I've split the exception handling to raise InvalidDatasetTypeError for invalid data types and InvalidComponentTypeError for invalid component types separately.
|
|
||
| if ComponentType.line in self.pgm_input_data: | ||
| raise ValueError("Line component already exists in pgm_input_data") | ||
| raise ComponentAlreadyExistsError("Line component already exists in pgm_input_data") |
There was a problem hiding this comment.
Seeing all the repetition here for the messages when raising ComponentAlreadyExistsError, maybe it's a good idea if we could do something like:
| raise ComponentAlreadyExistsError("Line component already exists in pgm_input_data") | |
| raise ComponentAlreadyExistsError("Line", "pgm_input_data") |
By modifying ComponentAlreadyExistsError to something along the lines of:
class ComponentAlreadyExistsError(ValueError, PowerGridModelIoError):
def __init__(self, component: str, dataset: str):
super().__init__(f"'{component}' component already exists in {dataset}")
I didn't recommend using ComponentType because that would probably be a breaking change, but please take a look.
Similar suggestion applies below in this file.
There was a problem hiding this comment.
Great suggestion. I've updated ComponentAlreadyExistsError to accept component and dataset arguments, and updated all call sites accordingly.
| key = (pp_table, name) | ||
| if key in self.idx_lookup: | ||
| raise KeyError(f"Indexes for '{key}' already exist!") | ||
| raise ComponentAlreadyExistsError(f"Indexes for '{key}' already exist!") |
There was a problem hiding this comment.
I don't think this one belongs to this error type.
There was a problem hiding this comment.
Agreed. I've introduced IndexAlreadyExistsError for this case instead.
| return {"table": table, "name": name, "index": indices[pgm_id]} | ||
| return {"table": table, "index": indices[pgm_id]} | ||
| raise KeyError(pgm_id) | ||
| raise ComponentNotFoundError(pgm_id) |
There was a problem hiding this comment.
Neither does this one. Perhaps this one and the above can be put into something like IndexToComponentNotFoundError or something similar.
My suggestion is a bit long, but that's the idea.
There was a problem hiding this comment.
Thank you for the suggestion. I've introduced IndexToComponentNotFoundError for this case.
| obj_id = obj["id"] | ||
| if not isinstance(obj_id, int): | ||
| raise ValueError(f"Invalid 'id' value for {component.value} {data_type.value} data") | ||
| raise InvalidDataFormatError(f"Invalid 'id' value for {component.value} {data_type.value} data") |
There was a problem hiding this comment.
This one perhaps belongs to what I suggested in https://github.com/PowerGridModel/power-grid-model-io/pull/385/changes#r2883220795 instead.
There was a problem hiding this comment.
Noted. I've kept InvalidDataFormatError for this case as it relates to data format validation rather than component type issues.
…et) signature and fix test assertions - Updated all ComponentAlreadyExistsError calls in pandapower_converter.py to use the new (component, dataset) constructor signature - Replaced ValueError with ComponentAlreadyExistsError in _pp_load_elements_output - Updated pytest.raises assertions in test_pandapower_converter_input.py and test_pandapower_converter_output.py to use ComponentAlreadyExistsError without match arguments - All 207 pandapower converter tests pass
Closes #384
This PR implements the custom error types discussed in Issue #384.
Replaced generic ValueError and KeyError exceptions with specific, custom-built exception types in the conversion module.
Introduced exceptions.py featuring: