#35648: SafeString and overriding addition do not work well together
-------------------------------------+-------------------------------------
Reporter: Matthias | Owner: Matthias Kestenholz
Kestenholz |
Type: Bug | Status: assigned
Component: Utilities | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When you have an object which is added to a string you can override
`__radd__`.
However, the current implementation of `SafeString.__add__`
unconditionally calls `super().__add__`, which means that the RHS doesn't
get a chance at handling the addition. I propose making
`SafeString.__add__` a bit more defensive and only call `super().__add__`
if it actually knows that it can handle the type.
The full diff of my proposed change is here:
https://github.com/django/django/compare/main...matthiask:django:safe-
string-add-safety
The first commit is
https://github.com/django/django/commit/2a118c2bec3e2952b7ab344e12e95cf42554cd5b
It shows that overriding `__radd__` works with `str` objects on the LHS
but doesn't work with `SafeString` objects.
The second commit is
https://github.com/django/django/commit/61767c66c00323b7b862d812679879a4fdc47a43
It allows the test to pass. I'm not 100% sure the proposed `__add__`
implementation handles everything it should.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35648>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.