ionelmc/python-lazy-object-proxy

How to deal with `is`?

emiel opened this issue · 4 comments

emiel commented

I have run in to the following:

p = Proxy(..)

if p is None:
  """Always false"""

I don't know that there is a way to proxy is (identity) but it would be nice to have a way to ensure the proxy is evaluated.

Now I'm using:

p = Proxy(..)

if p.__wrapped__ is None:
    pass

But that looks a bit ugly? Any suggestions? Perhaps something in "utils.py" to force evaluation?

Thanks!

The is operator basically does a pointer check - "are those objects at the same memory address?" basically. There's no way to hook into this process. Using p.__wrapped__ is probably the best thing to do.

I guess there could be an unproxy or similar in utils somewhere tho. Why do you need this? How often do you have this usecase?

emiel commented

Thanks for your reply. I ran into lazy_object_proxy while working with Airflow. In particular while using Context: https://github.com/apache/airflow/blob/39e395f9816c04ef2f033eb0b4f635fc3018d803/airflow/utils/context.py#L252

In short I believe it is something like this:

import lazy_object_proxy

p1 = lazy_object_proxy.Proxy(lambda: None)


def unproxy(proxy):
    return proxy.__wrapped__


if p1 is None:
    print("None is None")

if unproxy(p1) is None:
    print("None is None")

if not p1:
    print("p1 is falsy")

I've already rewrote my code where I don't need an "unproxy" but it seemed to be missing from the library. I thought proxy.__wrappped__ was quite ugly. ;)

Feel free to close this issue as I'm moving on. Just a bit of friendly feedback. :)

@ionelmc I've encountered the same problem now. In brief I was trying to organize a fallback for cases when prev_execution_date_success macro is None and inability to check the value for being None or not was bewildering to say at least. Airflow should definitely mention this somewhere in the macros related doc and maybe even provide an example of how to deal with macros returning None.

So do I need to do something in LOP about this?