Skip to content

Commit

Permalink
Python3.12: fix imports in kombu/utils/objects.py
Browse files Browse the repository at this point in the history
Consider the following piece of code, very similar to what can be found
in kombu/utils/objects.py:
---8<------------------------------------------------------------------
$ cat  /tmp/x.py
try:
    from functools import _NOT_FOUND
    from functools import cached_property as _cached_property
except ImportError:
    from cached_property import threaded_cached_property as _cached_property
    _NOT_FOUND = object()

print("OK!")
---8<------------------------------------------------------------------

This works well in Python3.11:
---8<------------------------------------------------------------------
$ podman run -it --rm -v /tmp:/tmp python:3.11.4  python /tmp/x.py
OK!
---8<------------------------------------------------------------------

But fails in Python3.12:
---8<------------------------------------------------------------------
$ podman run -it --rm -v /tmp:/tmp python:3.12.0b2  python /tmp/x.py
Traceback (most recent call last):
  File "/tmp/x.py", line 2, in <module>
    from functools import _NOT_FOUND
ImportError: cannot import name '_NOT_FOUND' from 'functools' (/usr/local/lib/python3.12/functools.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/x.py", line 6, in <module>
    from cached_property import threaded_cached_property as _cached_property
ModuleNotFoundError: No module named 'cached_property'
---8<------------------------------------------------------------------

This is because Python3.12 removed functools._NOT_FOUND (see commit
056dfc71dce15f81887f0bd6da09d6099d71f979), which prevents
cached_property from being imported from functools in our code. If the
cached_property library is not installed, then the imports fail.

We should be using two different try/except blocks, but since
functools._NOT_FOUND was defined as "object()" in the standard library
anyway, let's just not bother importing it.
  • Loading branch information
CyrilRoelandteNovance committed Jun 19, 2023
1 parent 18edd4c commit bed2d62
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions kombu/utils/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
__all__ = ('cached_property',)

try:
from functools import _NOT_FOUND
from functools import cached_property as _cached_property
except ImportError:
# TODO: Remove this fallback once we drop support for Python < 3.8
from cached_property import threaded_cached_property as _cached_property

_NOT_FOUND = object()
_NOT_FOUND = object()


class cached_property(_cached_property):
Expand Down

0 comments on commit bed2d62

Please sign in to comment.