Skip to content

Commit 5f9e5c1

Browse files
ngnpopefelixxm
authored andcommitted
Refs #34822, Refs #34986 -- Fixed migrations serializer support for functools.lru_cache().
It turns out that `functools._lru_cache_wrapper` is only a class when CPython's _functools C module provides it, otherwise it is a function. PyPy also provides it as a function.
1 parent 76280b4 commit 5f9e5c1

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

django/db/migrations/serializer.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
from django.utils.functional import LazyObject, Promise
1919
from django.utils.version import PY311, get_docs_version
2020

21+
FUNCTION_TYPES = (types.FunctionType, types.BuiltinFunctionType, types.MethodType)
22+
23+
if isinstance(functools._lru_cache_wrapper, type):
24+
# When using CPython's _functools C module, LRU cache function decorators
25+
# present as a class and not a function, so add that class to the list of
26+
# function types. In the pure Python implementation and PyPy they present
27+
# as normal functions which are already handled.
28+
FUNCTION_TYPES += (functools._lru_cache_wrapper,)
29+
2130

2231
class BaseSerializer:
2332
def __init__(self, value):
@@ -346,12 +355,7 @@ class Serializer:
346355
(bool, int, types.NoneType, bytes, str, range): BaseSimpleSerializer,
347356
decimal.Decimal: DecimalSerializer,
348357
(functools.partial, functools.partialmethod): FunctoolsPartialSerializer,
349-
(
350-
types.FunctionType,
351-
types.BuiltinFunctionType,
352-
types.MethodType,
353-
functools._lru_cache_wrapper,
354-
): FunctionTypeSerializer,
358+
FUNCTION_TYPES: FunctionTypeSerializer,
355359
collections.abc.Iterable: IterableSerializer,
356360
(COMPILED_REGEX_TYPE, RegexObject): RegexSerializer,
357361
uuid.UUID: UUIDSerializer,

0 commit comments

Comments
 (0)