ronantakizawa/codereview-bench huggingface dataset:
Repository: django/django
File: django/contrib/postgres/functions.py (Python)

Code before:
import warnings

from django.db.models import DateTimeField, Func
from django.db.models.functions import UUID4
from django.utils.deprecation import RemovedInDjango70Warning, django_file_prefixes


class RandomUUID(UUID4):
    def __init__(self, *args, **kwargs):
        warnings.warn(
            "RandomUUID is deprecated. Use django.db.models.functions.UUID4 instead.",
            RemovedInDjango70Warning,
            skip_file_prefixes=django_file_prefixes(),
        )
        super().__init__(*args, **kwargs)


class TransactionNow(Func):
    template = "CURRENT_TIMESTAMP"
    output_field = DateTimeField()


Review comment (suggestion):
Reminder for when you've finalized the deprecation strategy: Please add a `# RemovedInDjango70Warning` with instructions about how to alter the code.

Code after:
from django.db.models import DateTimeField, Func, UUIDField


class RandomUUID(Func):
    template = "GEN_RANDOM_UUID()"
    output_field = UUIDField()


class TransactionNow(Func):
    template = "CURRENT_TIMESTAMP"
    output_field = DateTimeField()
Repository: django/django
File: django/utils/html.py (Python)

Code before:
    Return the given text with ampersands, quotes and angle brackets encoded
    for use in HTML.

    Always escape input, even if it's already escaped and marked as such.
    This may result in double-escaping. If this is a concern, use
    conditional_escape() instead.
    """
    return SafeString(html.escape(str(text)))


_js_escapes = {
    ord("\\"): "\\u005C",
    ord("'"): "\\u0027",
    ord('"'): "\\u0022",
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
    ord("="): "\\u003D",
    ord("-"): "\\u002D",
    ord(";"): "\\u003B",
    ord("`"): "\\u0060",
    ord("\u2028"): "\\u2028",
    ord("\u2029"): "\\u2029",
}

# Escape every ASCII character with a value less than 32 (C0) && 127-159(C1).
_js_escapes.update((ord("%c" % z), "\\u%04X" % z) for z in range(32))
_js_escapes.update((ord("%c" % z), "\\u%04X" % z) for z in range(0x80, 0xA0))


@keep_lazy(SafeString)
def escapejs(value):
    """Hex encode characters for use in JavaScript strings."""
    return mark_safe(str(value).translate(_js_escapes))


_json_script_escapes = {
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
}


def json_script(value, element_id=None, encoder=None):
    """
    Escape all the HTML/XML special characters with their unicode escapes, so
    value is safe to be output anywhere except for inside a tag attribute. Wrap
    the escaped JSON in a script tag.
    """
    from django.core.serializers.json import DjangoJSONEncoder


Review comment (security):
Nit: Suggest keeping the comment as prose
```suggestion
# Escape every ASCII character with a value less than 32 (C0) and 127-159(C1).
```

Code after:
    """
    Return the given text with ampersands, quotes and angle brackets encoded
    for use in HTML.

    Always escape input, even if it's already escaped and marked as such.
    This may result in double-escaping. If this is a concern, use
    conditional_escape() instead.
    """
    return SafeString(html.escape(str(text)))


_js_escapes = {
    ord("\\"): "\\u005C",
    ord("'"): "\\u0027",
    ord('"'): "\\u0022",
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
    ord("="): "\\u003D",
    ord("-"): "\\u002D",
    ord(";"): "\\u003B",
    ord("`"): "\\u0060",
    ord("\u2028"): "\\u2028",
    ord("\u2029"): "\\u2029",
}

# Escape every ASCII character with a value less than 32 (C0), 127(C0),
# or 128-159(C1).
_js_escapes.update(
    (ord("%c" % z), "\\u%04X" % z) for z in chain(range(32), range(0x7F, 0xA0))
)


@keep_lazy(SafeString)
def escapejs(value):
    """Hex encode characters for use in JavaScript strings."""
    return mark_safe(str(value).translate(_js_escapes))


_json_script_escapes = {
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
}


def json_script(value, element_id=None, encoder=None):
    """
    Escape all the HTML/XML special characters with their unicode escapes, so
    value is safe to be output anywhere except for inside a tag attribute. Wrap
    the escaped JSON in a script tag.
Repository: django/django
File: django/utils/html.py (Python)

Code before:
    Return the given text with ampersands, quotes and angle brackets encoded
    for use in HTML.

    Always escape input, even if it's already escaped and marked as such.
    This may result in double-escaping. If this is a concern, use
    conditional_escape() instead.
    """
    return SafeString(html.escape(str(text)))


_js_escapes = {
    ord("\\"): "\\u005C",
    ord("'"): "\\u0027",
    ord('"'): "\\u0022",
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
    ord("="): "\\u003D",
    ord("-"): "\\u002D",
    ord(";"): "\\u003B",
    ord("`"): "\\u0060",
    ord("\u2028"): "\\u2028",
    ord("\u2029"): "\\u2029",
}

# Escape every ASCII character with a value less than 32 (C0) ,127(C0)
#  or 128-159(C1).
_js_escapes.update(
    (ord("%c" % z), "\\u%04X" % z) for z in chain(range(32), range(0x7F, 0xA0))
)


@keep_lazy(SafeString)
def escapejs(value):
    """Hex encode characters for use in JavaScript strings."""
    return mark_safe(str(value).translate(_js_escapes))


_json_script_escapes = {
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
}


def json_script(value, element_id=None, encoder=None):
    """
    Escape all the HTML/XML special characters with their unicode escapes, so
    value is safe to be output anywhere except for inside a tag attribute. Wrap
    the escaped JSON in a script tag.
    """

Review comment (security):
```suggestion
# Escape every ASCII character with a value less than 32 (C0), 127(C0)
```

Code after:
    Return the given text with ampersands, quotes and angle brackets encoded
    for use in HTML.

    Always escape input, even if it's already escaped and marked as such.
    This may result in double-escaping. If this is a concern, use
    conditional_escape() instead.
    """
    return SafeString(html.escape(str(text)))


_js_escapes = {
    ord("\\"): "\\u005C",
    ord("'"): "\\u0027",
    ord('"'): "\\u0022",
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
    ord("="): "\\u003D",
    ord("-"): "\\u002D",
    ord(";"): "\\u003B",
    ord("`"): "\\u0060",
    ord("\u2028"): "\\u2028",
    ord("\u2029"): "\\u2029",
}

# Escape every ASCII character with a value less than 32 (C0), 127(C0),
# or 128-159(C1).
_js_escapes.update(
    (ord("%c" % z), "\\u%04X" % z) for z in chain(range(32), range(0x7F, 0xA0))
)


@keep_lazy(SafeString)
def escapejs(value):
    """Hex encode characters for use in JavaScript strings."""
    return mark_safe(str(value).translate(_js_escapes))


_json_script_escapes = {
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
}


def json_script(value, element_id=None, encoder=None):
    """
    Escape all the HTML/XML special characters with their unicode escapes, so
    value is safe to be output anywhere except for inside a tag attribute. Wrap
    the escaped JSON in a script tag.
    """
Repository: django/django
File: django/utils/html.py (Python)

Code before:
    for use in HTML.

    Always escape input, even if it's already escaped and marked as such.
    This may result in double-escaping. If this is a concern, use
    conditional_escape() instead.
    """
    return SafeString(html.escape(str(text)))


_js_escapes = {
    ord("\\"): "\\u005C",
    ord("'"): "\\u0027",
    ord('"'): "\\u0022",
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
    ord("="): "\\u003D",
    ord("-"): "\\u002D",
    ord(";"): "\\u003B",
    ord("`"): "\\u0060",
    ord("\u2028"): "\\u2028",
    ord("\u2029"): "\\u2029",
}

# Escape every ASCII character with a value less than 32 (C0), 127(C0)
#  or 128-159(C1).
_js_escapes.update(
    (ord("%c" % z), "\\u%04X" % z) for z in chain(range(32), range(0x7F, 0xA0))
)


@keep_lazy(SafeString)
def escapejs(value):
    """Hex encode characters for use in JavaScript strings."""
    return mark_safe(str(value).translate(_js_escapes))


_json_script_escapes = {
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
}


def json_script(value, element_id=None, encoder=None):
    """
    Escape all the HTML/XML special characters with their unicode escapes, so
    value is safe to be output anywhere except for inside a tag attribute. Wrap
    the escaped JSON in a script tag.
    """
    from django.core.serializers.json import DjangoJSONEncoder

Review comment (security):
```suggestion
# Escape every ASCII character with a value less than 32 (C0), 127(C0),
# or 128-159(C1).
```

Code after:
    for use in HTML.

    Always escape input, even if it's already escaped and marked as such.
    This may result in double-escaping. If this is a concern, use
    conditional_escape() instead.
    """
    return SafeString(html.escape(str(text)))


_js_escapes = {
    ord("\\"): "\\u005C",
    ord("'"): "\\u0027",
    ord('"'): "\\u0022",
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
    ord("="): "\\u003D",
    ord("-"): "\\u002D",
    ord(";"): "\\u003B",
    ord("`"): "\\u0060",
    ord("\u2028"): "\\u2028",
    ord("\u2029"): "\\u2029",
}

# Escape every ASCII character with a value less than 32 (C0), 127(C0),
# or 128-159(C1).
_js_escapes.update(
    (ord("%c" % z), "\\u%04X" % z) for z in chain(range(32), range(0x7F, 0xA0))
)


@keep_lazy(SafeString)
def escapejs(value):
    """Hex encode characters for use in JavaScript strings."""
    return mark_safe(str(value).translate(_js_escapes))


_json_script_escapes = {
    ord(">"): "\\u003E",
    ord("<"): "\\u003C",
    ord("&"): "\\u0026",
}


def json_script(value, element_id=None, encoder=None):
    """
    Escape all the HTML/XML special characters with their unicode escapes, so
    value is safe to be output anywhere except for inside a tag attribute. Wrap
    the escaped JSON in a script tag.
    """
    from django.core.serializers.json import DjangoJSONEncoder
Repository: django/django
File: django/contrib/admindocs/utils.py (Python)

Code before:
    final_pattern, prev_end = "", None
    for start, end, _ in _find_groups(pattern, unnamed_group_matcher):
        if prev_end:
            final_pattern += pattern[prev_end:start]
        final_pattern += pattern[:start] + "<var>"
        prev_end = end
    return final_pattern + pattern[prev_end:]


def remove_non_capturing_groups(pattern):
    r"""
    Find non-capturing groups in the given `pattern` and remove them, e.g.
    1. (?P<a>\w+)/b/(?:\w+)c(?:\w+) => (?P<a>\\w+)/b/c
    2. ^(?:\w+(?:\w+))a => ^a
    3. ^a(?:\w+)/b(?:\w+) => ^a/b
    """
    group_start_end_indices = _find_groups(pattern, non_capturing_group_matcher)
    final_pattern, prev_end = "", None
    for start, end, _ in group_start_end_indices:
        final_pattern += pattern[prev_end:start]
        prev_end = end
    return final_pattern + pattern[prev_end:]


def strip_p_tags(value):
    return SafeString(value.replace("<p>", "").replace("</p>", ""))


Review comment (suggestion):
On second thought, can you revert this also? It's the only one that's not a a literal string (in other words, where we can't guarantee the type is exactly `str`. There could be a lazy object with a custom `replace` method that relies on this somehow.

Code after:
    final_pattern, prev_end = "", None
    for start, end, _ in _find_groups(pattern, unnamed_group_matcher):
        if prev_end:
            final_pattern += pattern[prev_end:start]
        final_pattern += pattern[:start] + "<var>"
        prev_end = end
    return final_pattern + pattern[prev_end:]


def remove_non_capturing_groups(pattern):
    r"""
    Find non-capturing groups in the given `pattern` and remove them, e.g.
    1. (?P<a>\w+)/b/(?:\w+)c(?:\w+) => (?P<a>\\w+)/b/c
    2. ^(?:\w+(?:\w+))a => ^a
    3. ^a(?:\w+)/b(?:\w+) => ^a/b
    """
    group_start_end_indices = _find_groups(pattern, non_capturing_group_matcher)
    final_pattern, prev_end = "", None
    for start, end, _ in group_start_end_indices:
        final_pattern += pattern[prev_end:start]
        prev_end = end
    return final_pattern + pattern[prev_end:]


def strip_p_tags(value):
    return mark_safe(value.replace("<p>", "").replace("</p>", ""))
Repository: django/django
File: django/dispatch/dispatcher.py (Python)

Code before:

from django.utils.inspect import func_accepts_kwargs

logger = logging.getLogger("django.dispatch")


def _make_id(target):
    if hasattr(target, "__func__"):
        return (id(target.__self__), id(target.__func__))
    return id(target)


NONE_ID = _make_id(None)

# A marker for caching
NO_RECEIVERS = object()


async def _run_parallel(*coros):
    """
    Execute multiple asynchronous coroutines in parallel,
    sharing the current context between them.
    """
    context = contextvars.copy_context()

    def _restore_context(context):
        # Check for changes in contextvars, and set them to the current
        # context for downstream consumers
        for cvar in context:
            cvalue = context.get(cvar)
            try:
                if cvar.get() != cvalue:
                    cvar.set(cvalue)
            except LookupError:
                cvar.set(cvalue)

    if len(coros) == 0:
        return []

    async def run(i, coro):
        results[i] = await coro

    try:
        async with asyncio.TaskGroup() as tg:
            results = [None] * len(coros)
            for i, coro in enumerate(coros):
                tg.create_task(run(i, coro), context=context)
        return results
    except BaseExceptionGroup as exception_group:
        if len(exception_group.exceptions) == 1:
            raise exception_group.exceptions[0]

Review comment (suggestion):
I see we're coping this from `asgiref`. Is that only because it's marked as private? We could ask Carlton if he minds us importing it and/or if he wants to make it public in the next release.

Code after:

from django.utils.inspect import func_accepts_kwargs

logger = logging.getLogger("django.dispatch")


def _make_id(target):
    if hasattr(target, "__func__"):
        return (id(target.__self__), id(target.__func__))
    return id(target)


NONE_ID = _make_id(None)

# A marker for caching
NO_RECEIVERS = object()


def _restore_context(context):
    """
    Check for changes in contextvars, and set them to the current
    context for downstream consumers.
    """
    for cvar in context:
        cvalue = context.get(cvar)
        try:
            if cvar.get() != cvalue:
                cvar.set(cvalue)
        except LookupError:
            cvar.set(cvalue)


async def _run_parallel(*coros):
    """
    Execute multiple asynchronous coroutines in parallel,
    sharing the current context between them.
    """
    context = contextvars.copy_context()

    if len(coros) == 0:
        return []

    async def run(i, coro):
        results[i] = await coro

    try:
        async with asyncio.TaskGroup() as tg:
            results = [None] * len(coros)
            for i, coro in enumerate(coros):
                tg.create_task(run(i, coro), context=context)
        return results
Repository: django/django
File: django/utils/inspect.py (Python)

Code before:

def is_module_level_function(func):
    if not inspect.isfunction(func) or inspect.isbuiltin(func):
        return False

    if "<locals>" in func.__qualname__:
        return False

    return True


@contextmanager
def leave_deferred_annotations_unevaluated():
    """
    inspect.getfullargspec eagerly evaluates type annotations. To add
    compatibility with Python 3.14+ deferred evaluation, patch the module-level
    helper to provide the annotation_format that we are using elsewhere.

    This private helper could be removed when there is an upstream solution for
    https://github.com/python/cpython/issues/141560.
    """
    if not PY314:
        yield
        return
    with lock:
        helper_was_already_replaced = False
        if original_helper := getattr(inspect, "_signature_from_callable", None):
            helper_was_already_replaced = isinstance(original_helper, functools.partial)
            if not helper_was_already_replaced:
                inspect._signature_from_callable = safe_signature_from_callable
        try:
            yield
        finally:
            if original_helper and not helper_was_already_replaced:
                inspect._signature_from_callable = original_helper


Review comment (refactor):
I would suggest using the slightly shorter:

```suggestion
        helper_was_replaced = False
```

Code after:
def is_module_level_function(func):
    if not inspect.isfunction(func) or inspect.isbuiltin(func):
        return False

    if "<locals>" in func.__qualname__:
        return False

    return True


@contextmanager
def lazy_annotations():
    """
    inspect.getfullargspec eagerly evaluates type annotations. To add
    compatibility with Python 3.14+ deferred evaluation, patch the module-level
    helper to provide the annotation_format that we are using elsewhere.

    This private helper could be removed when there is an upstream solution for
    https://github.com/python/cpython/issues/141560.

    This context manager is not reentrant.
    """
    if not PY314:
        yield
        return
    with lock:
        original_helper = inspect._signature_from_callable
        inspect._signature_from_callable = safe_signature_from_callable
        try:
            yield
        finally:
            inspect._signature_from_callable = original_helper
Repository: django/django
File: django/utils/inspect.py (Python)

Code before:
import functools
import inspect

from django.utils.version import PY314

if PY314:
    import annotationlib


@functools.lru_cache(maxsize=512)
def _get_func_parameters(func, remove_first):
    # As the annotations are not used in any case, inspect the signature with
    # FORWARDREF to leave any deferred annotations unevaluated.
    if PY314:
        signature = inspect.signature(
            func, annotation_format=annotationlib.Format.FORWARDREF
        )
    else:
        signature = inspect.signature(func)

    parameters = tuple(signature.parameters.values())
    if remove_first:
        parameters = parameters[1:]
    return parameters


def _get_callable_parameters(meth_or_func):
    is_method = inspect.ismethod(meth_or_func)
    func = meth_or_func.__func__ if is_method else meth_or_func
    return _get_func_parameters(func, remove_first=is_method)


ARG_KINDS = frozenset(
    {
        inspect.Parameter.POSITIONAL_ONLY,
        inspect.Parameter.KEYWORD_ONLY,
        inspect.Parameter.POSITIONAL_OR_KEYWORD,
    }

Review comment (refactor):
No reason to have to `getattr` for this below, since we're relying on it here. This helper hasn't changed in python for a long time, it's likely pretty stable.

Code after:
import functools
import inspect
import threading
from contextlib import contextmanager

from django.utils.version import PY314

if PY314:
    import annotationlib

    lock = threading.Lock()
    safe_signature_from_callable = functools.partial(
        inspect._signature_from_callable,
        annotation_format=annotationlib.Format.FORWARDREF,
    )


@functools.lru_cache(maxsize=512)
def _get_func_parameters(func, remove_first):
    # As the annotations are not used in any case, inspect the signature with
    # FORWARDREF to leave any deferred annotations unevaluated.
    if PY314:
        signature = inspect.signature(
            func, annotation_format=annotationlib.Format.FORWARDREF
        )
    else:
        signature = inspect.signature(func)

    parameters = tuple(signature.parameters.values())
    if remove_first:
        parameters = parameters[1:]
    return parameters


def _get_callable_parameters(meth_or_func):
    is_method = inspect.ismethod(meth_or_func)
    func = meth_or_func.__func__ if is_method else meth_or_func
    return _get_func_parameters(func, remove_first=is_method)
Repository: django/django
File: tests/model_fields/models.py (Python)

Code before:
class GeneratedModelUniqueConstraint(GeneratedModelBase):
    class Meta:
        required_db_features = {
            "supports_stored_generated_columns",
            "supports_expression_indexes",
        }
        constraints = [
            models.UniqueConstraint(F("a"), name="Generated model unique constraint a"),
        ]


class GeneratedModelUniqueConstraintVirtual(GeneratedModelVirtualBase):
    class Meta:
        required_db_features = {
            "supports_virtual_generated_columns",
            "supports_expression_indexes",
        }
        constraints = [
            models.UniqueConstraint(
                F("a"), name="Generated model unique constraint virtual a"
            ),
        ]


class HighPrecision(models.Model):
    d = models.DecimalField(max_digits=16, decimal_places=0)


Review comment (refactor):
Please remove this model and reuse `BigD`. We can't add 1x model per test due to the overhead of creating tables.

Code after:

class GeneratedModelCheckConstraintVirtual(GeneratedModelVirtualBase):
    class Meta:
        required_db_features = {
            "supports_virtual_generated_columns",
            "supports_table_check_constraints",
        }
        constraints = [
            models.CheckConstraint(
                condition=models.Q(a__gt=0),
                name="Generated model check constraint virtual a > 0",
            )
        ]


class GeneratedModelUniqueConstraint(GeneratedModelBase):
    class Meta:
        required_db_features = {
            "supports_stored_generated_columns",
            "supports_expression_indexes",
        }
        constraints = [
            models.UniqueConstraint(F("a"), name="Generated model unique constraint a"),
        ]


class GeneratedModelUniqueConstraintVirtual(GeneratedModelVirtualBase):
    class Meta:
        required_db_features = {
            "supports_virtual_generated_columns",
            "supports_expression_indexes",
        }
        constraints = [
            models.UniqueConstraint(
                F("a"), name="Generated model unique constraint virtual a"
            ),
        ]