Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 7, 2024
1 parent dadd71f commit 775bb84
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
17 changes: 8 additions & 9 deletions src/django_components/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
# And https://github.com/Xzya/django-web-components/blob/b43eb0c832837db939a6f8c1980334b0adfdd6e4/django_web_components/attributes.py

import re
from typing import Any, Dict, List, Tuple, Union, Mapping
from typing import Any, Dict, List, Mapping, Tuple, Union

from django.template import Node, Context, TemplateSyntaxError
from django.template import Context, Node, TemplateSyntaxError
from django.template.base import FilterExpression, Parser
from django.utils.html import format_html, conditional_escape
from django.utils.html import conditional_escape, format_html
from django.utils.regex_helper import _lazy_re_compile
from django.utils.safestring import mark_safe, SafeString
from django.utils.safestring import SafeString, mark_safe

from django_components.utils import FrozenDict


_AttrItem = Tuple[str, FilterExpression]


Expand Down Expand Up @@ -48,7 +47,7 @@ class MergeAttrsNode(Node):
def __init__(
self,
attributes: FilterExpression,
default_attrs: List[_AttrItem],
default_attrs: List[_AttrItem],
append_attrs: List[_AttrItem],
):
self.attributes = attributes
Expand Down Expand Up @@ -187,10 +186,10 @@ def extract_attrs_from_component_input(input: Dict) -> Tuple[Dict, HtmlAttribute
"""
Split a dict into two by extracting keys that start with `attrs:`
into a separate dict. The `attrs:` prefix is removed along the way.
We use the `attrs:` prefix to identify the "fallthrough" attributes,
AKA HTML attributes that are to be passed to the underlying HTML tag.
These attributes are collected into an `attrs` kwarg that's passed to
`get_context_data`, and also made available via `component_vars.attrs`
in the template.
Expand All @@ -209,7 +208,7 @@ def extract_attrs_from_component_input(input: Dict) -> Tuple[Dict, HtmlAttribute
continue

# NOTE: Trim off attrs prefix from keys
trimmed_key = key[len(attr_prefix):]
trimmed_key = key[len(attr_prefix) :]
fallthrough_attrs[trimmed_key] = val

attrs = HtmlAttributes(fallthrough_attrs)
Expand Down
3 changes: 2 additions & 1 deletion src/django_components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from django.utils.safestring import SafeString, mark_safe
from django.views import View

from django_components.attributes import extract_attrs_from_component_input

# Global registry var and register() function moved to separate module.
# Defining them here made little sense, since 1) component_tags.py and component.py
# rely on them equally, and 2) it made it difficult to avoid circularity in the
Expand All @@ -29,7 +31,6 @@
prepare_context,
)
from django_components.logger import logger, trace_msg
from django_components.attributes import extract_attrs_from_component_input
from django_components.middleware import is_dependency_middleware_active
from django_components.slots import DEFAULT_SLOT_KEY, FillContent, FillNode, SlotName, resolve_slots
from django_components.utils import gen_id, search
Expand Down
8 changes: 4 additions & 4 deletions src/django_components/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import glob
from collections import UserDict
from pathlib import Path
from typing import Any, Callable, Dict, List, NamedTuple, Optional, no_type_check
from collections import UserDict

from django.template.engine import Engine

Expand Down Expand Up @@ -63,7 +63,7 @@ def find_last_index(lst: List, predicate: Callable[[Any], bool]) -> Any:
class FrozenDict(UserDict):
# NOTE: This __init__ is taken from `UserDict`, except `UserDict` calls
# `self.update`, which we make immutable
def __init__(self, dict: Dict|None=None, /, **kwargs: Any):
def __init__(self, dict: Dict | None = None, /, **kwargs: Any):
self._hash = None
self.data = {}
if dict is not None:
Expand All @@ -78,13 +78,13 @@ def __hash__(self) -> int:
return self._hash

def _immutable(self, *args: Any, **kws: Any) -> None:
raise TypeError('cannot change object - object is immutable')
raise TypeError("cannot change object - object is immutable")

# makes (deep)copy more efficient
def __copy__(self) -> "FrozenDict":
return self

def __deepcopy__(self, memo: Dict | None =None) -> "FrozenDict":
def __deepcopy__(self, memo: Dict | None = None) -> "FrozenDict":
if memo is not None:
memo[id(self)] = self
return self
Expand Down
8 changes: 4 additions & 4 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.template import Template, Context
from django.utils.safestring import mark_safe, SafeString
from django.template import Context, Template
from django.utils.safestring import SafeString, mark_safe

from django_components import component, types
from django_components.attributes import (
append_attributes,
attributes_to_string,
merge_attributes,
normalize_html_class,
append_attributes,
)
from django_components import component, types

# isort: off
from .django_test_setup import * # NOQA
Expand Down
1 change: 1 addition & 0 deletions tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# isort: off
from .django_test_setup import * # NOQA
from .testutils import BaseTestCase, autodiscover_with_cleanup

# isort: on

from django_components import component, types
Expand Down

0 comments on commit 775bb84

Please sign in to comment.