Monday, June 20, 2011

Override/implements templates on PyDev

One thing I miss when working on Python is a way to let people know when some method is overridden (and having it raise an error if it's not)... I'm thinking about adding a decoration for that in PyDev (and probably a way to navigate to the super with that implementation), so, that'll work when using PyDev, but still, many times I look to the source code outside of PyDev (i.e.: review board, github, etc), and to sort that out, what I'm currently using is the following:

1. Create overrides (for when a method is overridden from a subclass) and implements (to indicate that some method conforms to an interface, even if not actually overriding a method) decorators as follows:



2. Create templates (in PyDev) as follows:

Name: overrides
Description: @overrides
Pattern: @overrides(${superclass}.${next_class_or_method})

Name: implements
Description: @implements
@implements(${interface_class}.${next_class_or_method})


3. Use it in your code:



To use it, just start typing 'overrides' before a 'def method' and apply the completion (and it should complete the class/method properly on overrides -- for the implements, the name of the implementing class will still be needed later on).

2 comments:

Michael Foord said...

Would Python Abstract Base Classes help with this?

Fabio Zadrozny said...

I think they're complimentary (in the same way that in java interfaces are complimentary to the @Override).

An example where the ABC would fail is if a method is removed and the implementor still implements it, even if it's not needed anymore.

Also, just by looking at the class implementing the ABC, it's hard to know which are the actual implemented methods.