Skip to content

Commit c94d4d5

Browse files
committed
docs: changelog + clarify AlreadyRegistered contract; ruff cleanup
- Add CHANGELOG entries under v0.150.1 for the four fixes landed in this branch. - Expand `AlreadyRegistered` docstring to spell out the new contract: same class -> no-op, anything else -> raise (and unregister first to replace). - Ruff autofix removed two unused `# noqa: PLC0415` directives. The import in the autodiscover regression test is now explicit via `importlib.import_module` so the test's precondition is self- documenting and lint-clean.
1 parent 1d933cb commit c94d4d5

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@
88

99
Multi-node template expressions (e.g. `value="prefix {{ x }}"` passed to a component) are wrapped in an internal `StringifiedNode` node that previously carried no template origin. As a result, Django's debug-mode traceback annotator could not locate the failing source line. The host template's origin is now threaded through to these nodes, so errors raised inside such expressions are annotated against the correct template file.
1010

11-
This builds on [#1597](https://github.com/django-components/django-components/issues/1597) and [#1635](https://github.com/django-components/django-components/pull/1635).
11+
See [#1597](https://github.com/django-components/django-components/issues/1597) and [#1635](https://github.com/django-components/django-components/pull/1635).
12+
13+
- **`@djc_test` no longer re-executes already-imported component modules between tests**
14+
15+
The fixture used to pop every autodiscovered module from `sys.modules` at teardown. Now, the teardown snapshots `sys.modules` at setup and only clears modules the test brought in itself.
16+
17+
See [#1598](https://github.com/django-components/django-components/issues/1598) / [#1630](https://github.com/django-components/django-components/pull/1630).
18+
19+
#### Refactor
20+
21+
- **`ComponentRegistry.register()` raises `AlreadyRegistered` on any replacement**
22+
23+
Previously `register()` silently overwrote an existing entry whenever the old and new classes had the same `class_id`. Now, different classes under the same name now raise `AlreadyRegistered`, and must call `registry.unregister(name)` first. Re-registering the **exact same class object** is still a no-op.
1224

1325
## v0.150.0
1426

‎src/django_components/component_registry.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@
3131

3232
class AlreadyRegistered(Exception):
3333
"""
34-
Raised when you try to register a [Component](./api.md#django_components.Component),
35-
but it's already registered with given
34+
Raised when you try to register a [Component](./api.md#django_components.Component)
35+
under a name that is already registered with the given
3636
[ComponentRegistry](./api.md#django_components.ComponentRegistry).
37+
38+
Re-registering the exact same class object under the same name is a no-op
39+
and does NOT raise - so calling `autodiscover()` or `import_libraries()`
40+
more than once is safe. Any *other* class under that name raises and you
41+
must call [`unregister()`](./api.md#django_components.ComponentRegistry.unregister)
42+
first to replace the existing registration.
3743
"""
3844

3945

‎tests/test_autodiscover.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,32 @@ def test_import_libraries_map_modules(self):
107107

108108
@djc_test
109109
class TestSysModulesIsolation:
110-
"""Regression coverage for #1598: modules present before a @djc_test should not be
111-
re-executed by the test teardown."""
110+
"""
111+
Regression coverage for #1598: modules present before a @djc_test should not be
112+
re-executed by the test teardown.
113+
"""
112114

113115
def test_modules_present_before_test_are_not_reimported(self):
114-
# The module must be imported before any @djc_test cycle touches it.
115-
import tests.components.single_file # noqa: PLC0415
116+
# Ensure the target module is in `sys.modules` before any @djc_test cycle
117+
# touches it. This is the precondition the fix must preserve.
118+
import importlib
119+
120+
importlib.import_module("tests.components.single_file")
116121

117122
module_before = sys.modules.get("tests.components.single_file")
118123
assert module_before is not None
119124
module_id_before = id(module_before)
120125

121126
@djc_test
122127
def inner_test() -> None:
123-
from django_components.autodiscovery import autodiscover # noqa: PLC0415
128+
from django_components.autodiscovery import autodiscover
124129

125130
autodiscover(map_module=lambda p: "tests." + p if p.startswith("components") else p)
126131

127132
# Run two cycles - prior to the fix, the first teardown would pop the module from
128133
# `sys.modules`, and the second setup's `autodiscover` would re-execute it.
129-
inner_test()
130-
inner_test()
134+
inner_test() # type: ignore[call-arg]
135+
inner_test() # type: ignore[call-arg]
131136

132137
module_after = sys.modules.get("tests.components.single_file")
133138
assert module_after is not None

‎tests/test_registry.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def test_module_global_node_subcls_cache_no_longer_exists(self):
208208
# `component_node_subclasses_by_name` that pinned subclasses + registries
209209
# forever. The cache now lives per-registry. Catch any accidental
210210
# reintroduction of the global.
211-
import django_components.component as component_module # noqa: PLC0415
211+
import django_components.component as component_module
212212

213213
assert not hasattr(component_module, "component_node_subclasses_by_name")
214214

0 commit comments

Comments
 (0)