As far as I can tell, the implementation can't just be
{{{
__radd__ = new_method_proxy(operator.radd)
}}}
because that doesn't exist, which is rubbish.
{{{
__radd__ = new_method_proxy(operator.attrgetter("__radd__"))
}}}
also won't work because types may not have that attr, and attrgetter
doesn't supress the exception (correctly)
The minimal implementation I've found that works for me is:
{{{
def __radd__(self, other):
if self._wrapped is empty:
self._setup()
return other + self._wrapped
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26287>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Could you please give some sample code with your use case?
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:1>
Comment (by kezabelle):
In a boiled-down nutshell:
{{{
def lazy_consumer():
# something more complex, obviously.
return [1, 3, 5]
consumer = SimpleLazyObject(lazy_consumer)
# inside third party code ...
def some_func(param):
third_party_code = [...]
# then, through parameter passing, my value is provided to be used.
# param is at this point, `consumer`
third_party_code_plus_mine = third_party_code + param
}}}
which ultimately yields:
{{{
TypeError: unsupported operand type(s) for +: 'list' and
'SimpleLazyObject'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:2>
* stage: Unreviewed => Accepted
Comment:
Seems okay, although I'm not an expert on the `SimpleLazyObject` class.
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:3>
Comment (by Uran198):
Replying to [comment:2 kezabelle]:
> {{{
> def lazy_consumer():
> # something more complex, obviously.
> return [1, 3, 5]
> consumer = SimpleLazyObject(lazy_consumer)
> }}}
If you know what is the resulting type or possible resulting types of your
expression, I think you better use `django.utils.functional.lazy` which
will provide all the necessary methods.
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:4>
Comment (by debanshuk):
Replying to [ticket:26287 kezabelle]:
> As far as I can tell, the implementation can't just be
> {{{
> __radd__ = new_method_proxy(operator.radd)
> }}}
> because that doesn't exist, which is rubbish.
> {{{
> __radd__ = new_method_proxy(operator.attrgetter("__radd__"))
> }}}
> also won't work because types may not have that attr, and attrgetter
doesn't supress the exception (correctly)
Wouldn't the following code work?
{{{
__add__ = new_method_proxy(operator.add)
__radd__ = new_method_proxy(lambda a, b: operator.add(b, a))
}}}
I have tested this and it seems to work as excepted.
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:5>
* cc: debanshuk2007@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:6>
* owner: nobody => Theofilos Alexiou
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:7>
* has_patch: 0 => 1
* stage: Accepted => Ready for checkin
Comment:
PR here: [https://github.com/django/django/pull/15400]. Tried the
suggestion by @Debanshu Kundu and it seems to work.
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:8>
* stage: Ready for checkin => Accepted
Comment:
RFC should be set by reviewer, see
[https://docs.djangoproject.com/en/stable/internals/contributing/triaging-
tickets/#ready-for-checkin docs].
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:9>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:10>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"f9ec777a826816e20e68021c0e73b5a76be650af" f9ec777a]:
{{{
#!CommitTicketReference repository=""
revision="f9ec777a826816e20e68021c0e73b5a76be650af"
Fixed #26287 -- Added support for addition operations to SimpleLazyObject.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26287#comment:11>