Use pythonic abstract classes instead of just raising NotImplementedError for unimplemented methods #8294
Steps to reproduce:
Quick and dirty example on the python console:
textInfos.TextInfo can't be initialized as it is too abstract. An error like this is raised:
TypeError: Can't instantiate abstract class TextInfo with abstract methods _get_bookmark, _get_text, collapse, compareEndPoints, copy, expand, move, setEndPoint
Getting the text raises a NotImplementedError
There are several classes in NVDA that are quite abstract, i.e. in the contentRecog, mathPres, NVDAObjects and textInfos modules. I think making the methods we consider abstract real abstract methods can make debugging much more easier. It will also ease the work flow for external contributors who want to derive from abstract classes.
Imagine a case where a whole new TextInfo derivative has to be made. Actually the only way to do this properly seems to be inspection of the source code of the base class and implementing all the methods that are not implemented. If you might happen to forget implementing a crucial method, you might even not notice it, unless you stumble upon a NotImplementedError that isn't caught. Marking methods that require an override as abstract, will automatically raise an error if there is no override of an abstract method in a derivative.
I'm not suggesting to go over the whole source code and mark every abstract method as such. However, I think it is important to make a decision about whether using abstract methods like this is the way to go. If so, it might be good to file it as a standard for future pull requests that implement abstract classes.
I think it is quite easy to adopt abstract classes. Making baseObject.AutoPropertyType derive from abc.ABCMeta already seems to do the trick. I'm a bit worried about abstract auto properties (i.e. _get_text). In the example above, i made _get_text an abstract method, however I have no idea whether that suffices.
The text was updated successfully, but these errors were encountered: