Repository: django/django
File: django/db/backends/sqlite3/operations.py (Python)

Code before:
import datetime
import decimal
import sqlite3
import uuid
from functools import lru_cache
from itertools import chain

from django.conf import settings
from django.core.exceptions import FieldError
from django.db import DatabaseError, NotSupportedError, models
from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.models.constants import OnConflict
from django.db.models.expressions import Col
from django.utils import timezone
from django.utils.dateparse import parse_date, parse_datetime, parse_time
from django.utils.functional import cached_property

from .base import Database

UNSUPPORTED_DATETIME_AGGREGATES = (
    models.Sum,
    models.Avg,
    models.Variance,
    models.StdDev,
)
DATETIME_FIELDS = (models.DateField, models.DateTimeField, models.TimeField)


class DatabaseOperations(BaseDatabaseOperations):
    cast_char_field_without_max_length = "text"
    cast_data_types = {
        "DateField": "TEXT",
        "DateTimeField": "TEXT",
    }
    explain_prefix = "EXPLAIN QUERY PLAN"
    # List of datatypes to that cannot be extracted with JSON_EXTRACT() on
    # SQLite. Use JSON_TYPE() instead.
    jsonfield_datatype_values = frozenset(["null", "false", "true"])

    def check_expression_support(self, expression):
        if isinstance(expression, UNSUPPORTED_DATETIME_AGGREGATES):
            for expr in expression.get_source_expressions():
                try:
                    output_field = expr.output_field
                except (AttributeError, FieldError):
                    # Not every subexpression has an output_field which is fine
                    # to ignore.
                    pass
                else:
                    if isinstance(output_field, DATETIME_FIELDS):
                        raise NotSupportedError(

Review comment (suggestion):
If we want to do this only once why not use `frozenset` 🤔 

Code after:
import datetime
import decimal
import sqlite3
import uuid
from functools import lru_cache
from itertools import chain

from django.conf import settings
from django.core.exceptions import FieldError
from django.db import DatabaseError, NotSupportedError, models
from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.models.constants import OnConflict
from django.db.models.expressions import Col
from django.utils import timezone
from django.utils.dateparse import parse_date, parse_datetime, parse_time
from django.utils.functional import cached_property

from .base import Database

UNSUPPORTED_DATETIME_AGGREGATES = (
    models.Sum,
    models.Avg,
    models.Variance,
    models.StdDev,
)
DATETIME_FIELDS = (models.DateField, models.DateTimeField, models.TimeField)


class DatabaseOperations(BaseDatabaseOperations):
    cast_char_field_without_max_length = "text"
    cast_data_types = {
        "DateField": "TEXT",
        "DateTimeField": "TEXT",
    }
    explain_prefix = "EXPLAIN QUERY PLAN"
    # List of datatypes to that cannot be extracted with JSON_EXTRACT() on
    # SQLite. Use JSON_TYPE() instead.
    jsonfield_datatype_values = frozenset(["null", "false", "true"])

    def check_expression_support(self, expression):
        if isinstance(expression, UNSUPPORTED_DATETIME_AGGREGATES):
            for expr in expression.get_source_expressions():
                try:
                    output_field = expr.output_field
                except (AttributeError, FieldError):
                    # Not every subexpression has an output_field which is fine
                    # to ignore.
                    pass
                else:
                    if isinstance(output_field, DATETIME_FIELDS):
                        klass = expression.__class__.__name__