From 1d0a06a9ab9bdfbcfa1f22bbc86137f5596dde46 Mon Sep 17 00:00:00 2001 From: Andrew-Chen-Wang Date: Tue, 4 Apr 2023 15:28:15 -0400 Subject: [PATCH] Remove Python 2 and Django 2.2 code --- CHANGELOG.rst | 2 +- cachalot/api.py | 36 ++++++++--------------- cachalot/settings.py | 4 --- cachalot/tests/migrations/0001_initial.py | 16 ++-------- cachalot/utils.py | 15 +--------- 5 files changed, 16 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d34b3d8..682cc55 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,7 @@ What’s new in django-cachalot? 2.6.0 ----- -- Dropped Django 2.2 and 4.0 support. Added Django 4.2 and Python 3.11 support (#229) +- Dropped Django 2.2 and 4.0 support. Added Django 4.2 and Python 3.11 support. Added psycopg support (#229) 2.5.3 ----- diff --git a/cachalot/api.py b/cachalot/api.py index b451891..d50b277 100644 --- a/cachalot/api.py +++ b/cachalot/api.py @@ -1,4 +1,5 @@ from contextlib import contextmanager +from typing import Any, Optional, Tuple, Union from django.apps import apps from django.conf import settings @@ -45,7 +46,11 @@ def _get_tables(tables_or_models): else table_or_model._meta.db_table) -def invalidate(*tables_or_models, **kwargs): +def invalidate( + *tables_or_models: Tuple[Union[str, Any], ...], + cache_alias: Optional[str] = None, + db_alias: Optional[str] = None, +) -> None: """ Clears what was cached by django-cachalot implying one or more SQL tables or models from ``tables_or_models``. @@ -62,19 +67,9 @@ def invalidate(*tables_or_models, **kwargs): (or a combination) :type tables_or_models: tuple of strings or models :arg cache_alias: Alias from the Django ``CACHES`` setting - :type cache_alias: string or NoneType :arg db_alias: Alias from the Django ``DATABASES`` setting - :type db_alias: string or NoneType :returns: Nothing - :rtype: NoneType """ - # TODO: Replace with positional arguments when we drop Python 2 support. - cache_alias = kwargs.pop('cache_alias', None) - db_alias = kwargs.pop('db_alias', None) - for k in kwargs: - raise TypeError( - "invalidate() got an unexpected keyword argument '%s'" % k) - send_signal = False invalidated = set() for cache_alias, db_alias, tables in _cache_db_tables_iterator( @@ -90,7 +85,11 @@ def invalidate(*tables_or_models, **kwargs): post_invalidation.send(table, db_alias=db_alias) -def get_last_invalidation(*tables_or_models, **kwargs): +def get_last_invalidation( + *tables_or_models: Tuple[Union[str, Any], ...], + cache_alias: Optional[str] = None, + db_alias: Optional[str] = None, +) -> float: """ Returns the timestamp of the most recent invalidation of the given ``tables_or_models``. If ``tables_or_models`` is not specified, @@ -106,19 +105,9 @@ def get_last_invalidation(*tables_or_models, **kwargs): (or a combination) :type tables_or_models: tuple of strings or models :arg cache_alias: Alias from the Django ``CACHES`` setting - :type cache_alias: string or NoneType :arg db_alias: Alias from the Django ``DATABASES`` setting - :type db_alias: string or NoneType :returns: The timestamp of the most recent invalidation - :rtype: float """ - # TODO: Replace with positional arguments when we drop Python 2 support. - cache_alias = kwargs.pop('cache_alias', None) - db_alias = kwargs.pop('db_alias', None) - for k in kwargs: - raise TypeError("get_last_invalidation() got an unexpected " - "keyword argument '%s'" % k) - last_invalidation = 0.0 for cache_alias, db_alias, tables in _cache_db_tables_iterator( list(_get_tables(tables_or_models)), cache_alias, db_alias): @@ -134,7 +123,7 @@ def get_last_invalidation(*tables_or_models, **kwargs): @contextmanager -def cachalot_disabled(all_queries=False): +def cachalot_disabled(all_queries: bool = False): """ Context manager for temporarily disabling cachalot. If you evaluate the same queryset a second time, @@ -158,7 +147,6 @@ def cachalot_disabled(all_queries=False): the original and duplicate query. :arg all_queries: Any query, including already evaluated queries, are re-evaluated. - :type all_queries: bool """ was_enabled = getattr(LOCAL_STORAGE, "cachalot_enabled", cachalot_settings.CACHALOT_ENABLED) LOCAL_STORAGE.cachalot_enabled = False diff --git a/cachalot/settings.py b/cachalot/settings.py index a736465..2bf5c81 100644 --- a/cachalot/settings.py +++ b/cachalot/settings.py @@ -9,8 +9,6 @@ SUPPORTED_DATABASE_ENGINES = { 'django.db.backends.sqlite3', 'django.db.backends.postgresql', 'django.db.backends.mysql', - # TODO: Remove when we drop Django 2.x support. - 'django.db.backends.postgresql_psycopg2', # GeoDjango 'django.contrib.gis.db.backends.spatialite', @@ -20,8 +18,6 @@ SUPPORTED_DATABASE_ENGINES = { # django-transaction-hooks 'transaction_hooks.backends.sqlite3', 'transaction_hooks.backends.postgis', - # TODO: Remove when we drop Django 2.x support. - 'transaction_hooks.backends.postgresql_psycopg2', 'transaction_hooks.backends.mysql', # django-prometheus wrapped engines diff --git a/cachalot/tests/migrations/0001_initial.py b/cachalot/tests/migrations/0001_initial.py index 45b4f87..f2f97df 100644 --- a/cachalot/tests/migrations/0001_initial.py +++ b/cachalot/tests/migrations/0001_initial.py @@ -2,7 +2,7 @@ from django import VERSION as DJANGO_VERSION from django.conf import settings from django.contrib.postgres.fields import ( ArrayField, HStoreField, IntegerRangeField, - DateRangeField, DateTimeRangeField) + DateRangeField, DateTimeRangeField, DecimalRangeField) from django.contrib.postgres.operations import ( HStoreExtension, UnaccentExtension) from django.db import models, migrations @@ -21,19 +21,6 @@ def extra_regular_available_fields(): def extra_postgres_available_fields(): fields = [] - try: - # TODO Remove when Dj31 support is dropped - from django.contrib.postgres.fields import FloatRangeField - fields.append(('float_range', FloatRangeField(null=True, blank=True))) - except ImportError: - pass - - try: - # TODO Add to module import when Dj31 is dropped - from django.contrib.postgres.fields import DecimalRangeField - fields.append(('decimal_range', DecimalRangeField(null=True, blank=True))) - except ImportError: - pass # Future proofing with Django 40 deprecation if DJANGO_VERSION[0] < 4: @@ -103,6 +90,7 @@ class Migration(migrations.Migration): ('int_range', IntegerRangeField(null=True, blank=True)), ('date_range', DateRangeField(null=True, blank=True)), ('datetime_range', DateTimeRangeField(null=True, blank=True)), + ('decimal_range', DecimalRangeField(null=True, blank=True)) ] + extra_postgres_available_fields(), ), migrations.RunSQL('CREATE TABLE cachalot_unmanagedmodel ' diff --git a/cachalot/utils.py b/cachalot/utils.py index 9df2a74..e136e9e 100644 --- a/cachalot/utils.py +++ b/cachalot/utils.py @@ -35,13 +35,6 @@ CACHABLE_PARAM_TYPES = { } UNCACHABLE_FUNCS = {Now, TransactionNow} -try: - # TODO Drop after Dj30 drop - from django.contrib.postgres.fields.jsonb import JsonAdapter - CACHABLE_PARAM_TYPES.update((JsonAdapter,)) -except ImportError: - pass - try: from psycopg2 import Binary from psycopg2.extras import ( @@ -131,13 +124,7 @@ def _find_rhs_lhs_subquery(side): elif h_class is QuerySet: return side.query elif h_class in (Subquery, Exists): # Subquery allows QuerySet & Query - try: - return side.query.query if side.query.__class__ is QuerySet else side.query - except AttributeError: # TODO Remove try/except closure after drop Django 2.2 - try: - return side.queryset.query - except AttributeError: - return None + return side.query.query if side.query.__class__ is QuerySet else side.query elif h_class in UNCACHABLE_FUNCS: raise UncachableQuery