Refs #34233 -- Used @functools.cache.

Python 3.9+ supports @functools.cache as an alias for
@functools.lru_cache(maxsize=None).
This commit is contained in:
Nick Pope 2023-01-18 18:23:18 +00:00 committed by GitHub
parent 23e8868862
commit 4470c2405c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 16 additions and 16 deletions

View File

@ -165,7 +165,7 @@ class Apps:
raise LookupError(message)
# This method is performance-critical at least for Django's test suite.
@functools.lru_cache(maxsize=None)
@functools.cache
def get_models(self, include_auto_created=False, include_swapped=False):
"""
Return a list of all installed models.
@ -280,14 +280,14 @@ class Apps:
raise LookupError("Model '%s.%s' not registered." % (app_label, model_name))
return model
@functools.lru_cache(maxsize=None)
@functools.cache
def get_swappable_settings_name(self, to_string):
"""
For a given model string (e.g. "auth.User"), return the name of the
corresponding settings name if it refers to a swappable model. If the
referred model is not swappable, return None.
This method is decorated with lru_cache because it's performance
This method is decorated with @functools.cache because it's performance
critical when it comes to migrations. Since the swappable settings don't
change after Django has loaded the settings, there is no reason to get
the respective settings attribute over and over again.

View File

@ -20,7 +20,7 @@ def i18n_patterns(*urls, prefix_default_language=True):
]
@functools.lru_cache(maxsize=None)
@functools.cache
def is_language_prefix_patterns_used(urlconf):
"""
Return a tuple of two booleans: (

View File

@ -17,7 +17,7 @@ from django.utils.translation import gettext as _
from django.utils.translation import ngettext
@functools.lru_cache(maxsize=None)
@functools.cache
def get_default_password_validators():
return get_password_validators(settings.AUTH_PASSWORD_VALIDATORS)

View File

@ -312,7 +312,7 @@ def get_finders():
yield get_finder(finder_path)
@functools.lru_cache(maxsize=None)
@functools.cache
def get_finder(import_path):
"""
Import the staticfiles finder class described by import_path, where

View File

@ -49,7 +49,7 @@ def load_command_class(app_name, name):
return module.Command()
@functools.lru_cache(maxsize=None)
@functools.cache
def get_commands():
"""
Return a dictionary mapping command names to their callback applications.

View File

@ -96,7 +96,7 @@ def make_style(config_string=""):
return style
@functools.lru_cache(maxsize=None)
@functools.cache
def no_style():
"""
Return a Style object with no color scheme.

View File

@ -311,7 +311,7 @@ class Command(BaseCommand):
fixture_files_in_dir.append((candidate, fixture_dir, fixture_name))
return fixture_files_in_dir
@functools.lru_cache(maxsize=None)
@functools.cache
def find_fixtures(self, fixture_label):
"""Find fixture files for a given label."""
if fixture_label == READ_STDIN:

View File

@ -857,7 +857,7 @@ class ForeignObject(RelatedField):
return self.get_reverse_path_info()
@classmethod
@functools.lru_cache(maxsize=None)
@functools.cache
def get_class_lookups(cls):
bases = inspect.getmro(cls)
bases = bases[: bases.index(ForeignObject) + 1]

View File

@ -213,7 +213,7 @@ class RegisterLookupMixin:
def _get_lookup(self, lookup_name):
return self.get_lookups().get(lookup_name, None)
@functools.lru_cache(maxsize=None)
@functools.cache
def get_class_lookups(cls):
class_lookups = [
parent.__dict__.get("class_lookups", {}) for parent in inspect.getmro(cls)

View File

@ -1,5 +1,5 @@
import functools
import uuid
from functools import lru_cache
class IntConverter:
@ -57,7 +57,7 @@ def register_converter(converter, type_name):
get_converters.cache_clear()
@lru_cache(maxsize=None)
@functools.cache
def get_converters():
return {**DEFAULT_CONVERTERS, **REGISTERED_CONVERTERS}

View File

@ -108,12 +108,12 @@ def get_resolver(urlconf=None):
return _get_cached_resolver(urlconf)
@functools.lru_cache(maxsize=None)
@functools.cache
def _get_cached_resolver(urlconf=None):
return URLResolver(RegexPattern(r"^/"), urlconf)
@functools.lru_cache(maxsize=None)
@functools.cache
def get_ns_resolver(ns_pattern, resolver, converters):
# Build a namespaced resolver for the given parent URLconf pattern.
# This makes it possible to have captured parameters in the parent

View File

@ -5,7 +5,7 @@ from django.core.exceptions import ViewDoesNotExist
from django.utils.module_loading import module_has_submodule
@functools.lru_cache(maxsize=None)
@functools.cache
def get_callable(lookup_view):
"""
Return a callable corresponding to lookup_view.