Uh oh!
There was an error while loading. Please reload this page.
Add abstractmethods to backend classes - #7460
Conversation
for more information, see https://pre-commit.ci
TomNicholas
commented
Jan 20, 2023
This seems like a good idea to me, but I don't know much about this part of the codebase. We should at minimum state in the docstring of these method whether they are required or optional. From the errors thrown in the tests it seems |
jhamman
left a comment
There was a problem hiding this comment.
Thanks for working on this. Two small comments.
Uh oh!
There was an error while loading. Please reload this page.
| txt += f"\n Learn more at {self.url}" | ||
| return txt | ||
| @abstractmethod |
There was a problem hiding this comment.
Per #7437 (comment), we may not want to require all of these methods. We may end up in a situation where BackendEntrypoints must define one or more of open_dataarray, open_dataset, and open_datatree.
There was a problem hiding this comment.
This seems pretty doable, e.g.
classMyBaseClass:
def__init__(self):
super().__init__()
# Check that at least one of the methods has been overriddenifall(getattr(self.__class__, m) isgetattr(MyBaseClass, m) formin ['method1', 'method2', 'method3']):
raiseTypeError("You must implement at least one of 'method1', 'method2', 'method3'")
defmethod1(self):
passdefmethod2(self):
passdefmethod3(self):
passclassMyDerivedClass(MyBaseClass):
defmethod1(self):
print("Method1 implemented")
# This will not raise an errord=MyDerivedClass()
classMyInvalidDerivedClass(MyBaseClass):
pass# This will raise a TypeErrori=MyInvalidDerivedClass()TypeError: You must implement at least one of 'method1', 'method2', 'method3'Co-authored-by: Joe Hamman <jhamman1@gmail.com>
It's been unclear to me what methods are necessary to implement or not. I think decorating with
@abstractmethodwill help with that. It's a breaking change though and it could be disruptive.whats-new.rstapi.rst