[Python-ideas] @Override decorator

Neil Girdhar mistersheik at gmail.com
Mon Jul 18 18:15:09 EDT 2016


I would really like this as I currently comment all my overridden methods 
to remind myself what is an override.

However, I suggest two overrides decorators: one for pure virtuals, and one 
where the pattern is call super.

The assertion for argspecs should only apply when the superclass has a pure 
virtual method.

The other pattern is with multiple inheritance where it's common for 
subclasses to pull off the arguments they need and forward the rest to the 
superclass.  In that case, it would be nice to check (as best as possible, 
by for example walking the parsed code) that the member calls super.  A 
common bug is to forget to do this, which might not be detected until 
multiple inheritance causes the mro to change the superclass, and the new 
superclass has important behavior that's not being run.

Best,

Neil


On Monday, July 18, 2016 at 1:45:40 PM UTC-4, Shrey Desai wrote:
>
> Python Ideas,
>
> While working on some object-oriented projects in Java, I noticed that 
> Python does not have an *@override* decorator for methods that are 
> derived from a parent class but overridden for the unique purposes of a 
> base class. With the creation of the *@abstractmethod* decorator, the 
> override decorator could follow in clearly distinguishing the logic and 
> design between parent/base classes.
>
> Why I would think an override decorator might be useful:
>
>    1. For other people reading the code, it would be great to distinguish 
>    between methods that are unique to a class and methods that are inherited 
>    from a parent class. Not all methods inherited might be overridden, so 
>    keeping track of which inherited methods are overridden and which are not 
>    would be nice.
>    2. With the advent of static typing from mypy:
>       1. Having the decorator could corroborate the fact that the given 
>       method overrides the parent method correctly (correct name + parameter 
>       list).
>       2. When the parent class changes, such as the name or parameter 
>       list of an abstract method, the children classes should be updated as well. 
>       mypy could easily target the methods that need to be altered with the 
>       correct method signature.
>       3. If you don’t have an override decorator and overrode a parent 
>       method, then there could be some error complaining about this. This would 
>       be extremely useful to prevent accidental errors.
>    
>
> There is some interest for this as expressed on Stack Overflow (
> http://stackoverflow.com/questions/1167617/in-python-how-do-i-indicate-im-overriding-a-method) 
> and some people have also made packages for this, but having it in the 
> standard distribution would be nice. Thoughts?
>
> Sincerely,
> Shrey Desai
> https://github.com/shreydesai
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160718/c4b2c724/attachment.html>


More information about the Python-ideas mailing list