-
Notifications
You must be signed in to change notification settings - Fork 29
Refactor validator return type to support warnings via ValidationResult wrapper (#1479) #1480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
885f7a1
8e46bd4
3ee3ea3
c1225d4
39499cf
669261e
88bb8e9
964acbf
6670324
6b88ba9
c0b0464
9eff3b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,9 @@ | |
| "MissingDataType", | ||
| "IllegalLinkError", | ||
| "IncorrectDataType", | ||
| "IncorrectQuantityError" | ||
| "IncorrectQuantityError", | ||
| "ValidationWarning", | ||
| "ValidationResult" | ||
| ] | ||
|
|
||
|
|
||
|
|
@@ -85,6 +87,81 @@ def __eq__(self, other): | |
| return hash(self) == hash(other) | ||
|
|
||
|
|
||
| class ValidationWarning: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since One minor thing is that because To address this, I would change def __eq__(self, other):
return type(self) is type(other) and hash(self) == hash(other) |
||
|
|
||
| @docval({'name': 'name', 'type': str, 'doc': 'the name of the component that is erroneous'}, | ||
| {'name': 'reason', 'type': str, 'doc': 'the reason for the warning'}, | ||
| {'name': 'location', 'type': str, 'doc': 'the location of the warning', 'default': None}) | ||
| def __init__(self, **kwargs): | ||
| self.__name = getargs('name', kwargs) | ||
| self.__reason = getargs('reason', kwargs) | ||
| self.__location = getargs('location', kwargs) | ||
|
|
||
| @property | ||
| def name(self): | ||
| return self.__name | ||
|
|
||
| @property | ||
| def reason(self): | ||
| return self.__reason | ||
|
|
||
| @property | ||
| def location(self): | ||
| return self.__location | ||
|
|
||
| @location.setter | ||
| def location(self, loc): | ||
| self.__location = loc | ||
|
|
||
| def __str__(self): | ||
| return self.__format_str(self.name, self.location, self.reason) | ||
|
|
||
| @staticmethod | ||
| def __format_str(name, location, reason): | ||
| if location is not None: | ||
| return "%s (%s): %s" % (name, location, reason) | ||
| else: | ||
| return "%s: %s" % (name, reason) | ||
|
|
||
| def __repr__(self): | ||
| return self.__str__() | ||
|
|
||
| def __hash__(self): | ||
| return hash(self.__equatable_str()) | ||
|
|
||
| def __equatable_str(self): | ||
| if self.location is not None: | ||
| equatable_name = self.name.split('/')[-1] | ||
| else: | ||
| equatable_name = self.name | ||
| return self.__format_str(equatable_name, self.location, self.reason) | ||
|
|
||
| def __eq__(self, other): | ||
| return type(self) is type(other) and hash(self) == hash(other) | ||
|
|
||
|
|
||
| class ValidationResult: | ||
|
|
||
| def __init__(self, errors = None, warnings = None): | ||
| self.errors = list(errors) if errors is not None else [] | ||
| self.warnings = list(warnings) if warnings is not None else [] | ||
|
|
||
| def __iter__(self): | ||
| return iter(self.errors) | ||
|
|
||
| def __len__(self): | ||
| return len(self.errors) | ||
|
|
||
| def __bool__(self): | ||
| return bool(self.errors) | ||
|
|
||
| def __getitem__(self, i): | ||
| return self.errors[i] | ||
|
rly marked this conversation as resolved.
|
||
|
|
||
| def __repr__(self): | ||
| return "ValidationResult(errors=%r, warnings=%r)" % (self.errors, self.warnings) | ||
|
|
||
|
|
||
| class DtypeError(Error): | ||
|
|
||
| @docval({'name': 'name', 'type': str, 'doc': 'the name of the component that is erroneous'}, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.