question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Use decorator instead of get_classes to mark classes extensible

See original GitHub issue

While struggling with the pain that is get_classes statements spanning across multiple lines, I wished for a cleaner way to handle Oscar’s dynamic class loading

Proposal

Introduce a class decorator that mimics get_class. If a customised version of the decorated class exists, return that one instead of the decorated class.

# catalogue/forms.py
from oscar.core.loading import extensible

class ProductForm(forms.Form):
    ...

@extensible
class ExtensibleProductForm(ProductForm):
    pass

# catalogue/views.py

from .forms import ExtensibleProductForm

Pro & Con

Pro:

  • more readable import statements
  • IDE support for importing, and detecting unused or wrong imports
  • avoids accidentally using a direct import, class will always be extensible
  • a lot DRYer than the current solution

Cons:

  • It’s not really “decorating”, it’s a straight-out monkey patch.
  • Makes class names longer

One could also just decorate all extensible classes, but then it’s not immediately obvious from other code if the used class is extensible or not. And it makes it harder to access the core Oscar class.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
craigloftuscommented, Jan 9, 2018

I agree that the current approach is more explicit and therefore better than the proposal.

If it were to change I’d rather it went the other way and became more explicit, i.e., as well as using get_class, using a registry pattern rather than using magic paths.

0reactions
solarissmokecommented, Feb 10, 2018

Closing this on the basis that the consensus seems to be to make overriding more explicit, not less. I think the solution will be related to #2544.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decorator pattern versus sub classing - Stack Overflow
The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time, and the change affects all instances of the original...
Read more >
5.3-Decorator Design Pattern - GitHub Pages
ConcreteComponent is a representative for all classes whose objects should be dynamically extensible with new functionality. Component can be: an interface that ...
Read more >
This Is How You Extend Your Classes Without Modifying Them
Plain and simple extension over modification using decorators. Ever had new requirements thrown at you where you'd need to add new functionality ...
Read more >
Head First Design Patterns
“'Head First Design Patterns' manages to mix fun, belly-laughs, insight, technical depth and great practical advice in one entertaining and thought provoking ...
Read more >
Annotations vs Decorators · Issue #115 - GitHub
Java Annotations are just that: annotations. A kind of comment. It is a kind of metadata that expresses the properties of classes, attributes, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found