Skip to content

Commit 20a574a

Browse files
feat!: remove deprecated "descendant_count" completely
1 parent 4c4b876 commit 20a574a

5 files changed

Lines changed: 25 additions & 33 deletions

File tree

src/openedx_tagging/data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class TagData(TypedDict):
2121
value: str
2222
external_id: str | None
2323
child_count: int
24-
descendant_count: int # Deprecated; do not use.
2524
depth: int
2625
parent_value: str | None
2726
# Note: usage_count may or may not be present, depending on the request.

src/openedx_tagging/models/base.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def descendant_count(self) -> int:
173173
).count()
174174
return 0
175175

176-
def save(self, *args, **kwargs):
176+
def save(self, *args, **kwargs) -> None:
177177
"""
178178
Compute and persist depth and lineage before saving, then cascade any changes to descendants.
179179
"""
@@ -187,8 +187,8 @@ def save(self, *args, **kwargs):
187187
self.lineage = self.value + "\t"
188188
else:
189189
if Tag.parent.is_cached(self): # pylint: disable=no-member
190-
parent_depth = self.parent.depth
191-
parent_lineage = self.parent.lineage
190+
parent_depth = self.parent.depth # type: ignore[union-attr]
191+
parent_lineage = self.parent.lineage # type: ignore[union-attr]
192192
else:
193193
parent_vals = Tag.objects.values("depth", "lineage").get(pk=self.parent_id)
194194
parent_depth = parent_vals["depth"]
@@ -216,7 +216,9 @@ def save(self, *args, **kwargs):
216216
}
217217
if depth_delta != 0:
218218
update_kwargs["depth"] = F("depth") + depth_delta
219-
self.taxonomy.tag_set.filter(lineage__startswith=old_values["lineage"]).exclude(pk=self.pk).update(
219+
self.taxonomy.tag_set.filter( # type: ignore[union-attr]
220+
lineage__startswith=old_values["lineage"]
221+
).exclude(pk=self.pk).update(
220222
**update_kwargs
221223
)
222224

@@ -492,7 +494,6 @@ def _get_filtered_tags_free_text(
492494
qs = qs.annotate(
493495
depth=Value(0),
494496
child_count=Value(0),
495-
descendant_count=Value(0),
496497
external_id=Value(None, output_field=models.CharField()),
497498
parent_value=Value(None, output_field=models.CharField()),
498499
_id=Value(None, output_field=models.CharField()),
@@ -524,13 +525,11 @@ def _get_filtered_tags_one_level(
524525
qs = self.tag_set.filter(parent=None)
525526
qs = qs.annotate(parent_value=Value(None, output_field=models.CharField()))
526527
qs = qs.annotate(child_count=models.Count("children", distinct=True)) # type: ignore[no-redef]
527-
# Add the deprecated "descendant_count field". For now it's just the same as child_count.
528-
qs = qs.annotate(descendant_count=F("child_count"))
529528
# Filter by search term:
530529
if search_term:
531530
qs = qs.filter(value__icontains=search_term)
532531
qs = qs.annotate(_id=F("id")) # ID has an underscore to encourage use of 'value' rather than this internal ID
533-
qs = qs.values("value", "child_count", "descendant_count", "depth", "parent_value", "external_id", "_id")
532+
qs = qs.values("value", "child_count", "depth", "parent_value", "external_id", "_id")
534533
qs = qs.order_by("value")
535534
if include_counts:
536535
# We need to include the count of how many times this tag is used to tag objects.
@@ -605,15 +604,13 @@ def _get_filtered_tags_deep(
605604
.order_by()
606605
.annotate(count=models.Func(F("id"), function="Count"))
607606
)
608-
qs = initial_qs.annotate(child_count=models.Subquery(child_count_sq.values("count")))
609-
# Add the deprecated "descendant_count" field. For now it just is the same as child_count.
610-
qs = qs.annotate(descendant_count=F("child_count"))
607+
qs = initial_qs.annotate(child_count=models.Subquery(child_count_sq.values("count"))) # type: ignore[no-redef]
611608

612609
# Add the parent value
613610
qs = qs.annotate(parent_value=F("parent__value"))
614611
qs = qs.annotate(_id=F("id")) # ID has an underscore to encourage use of 'value' rather than this internal ID
615612
qs = qs.values( # type: ignore[assignment]
616-
"value", "child_count", "descendant_count", "depth", "parent_value", "external_id", "_id"
613+
"value", "child_count", "depth", "parent_value", "external_id", "_id"
617614
)
618615
# lineage is a case-insensitive column storing "Root\tParent\t...\tThisValue\t", so
619616
# ordering by it gives the tree sort order that we want.

src/openedx_tagging/rest_api/v1/serializers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ class TagDataSerializer(UserPermissionsSerializerMixin, serializers.Serializer):
226226
value = serializers.CharField()
227227
external_id = serializers.CharField(allow_null=True)
228228
child_count = serializers.IntegerField()
229-
descendant_count = serializers.IntegerField() # deprecated
230229
depth = serializers.IntegerField()
231230
parent_value = serializers.CharField(allow_null=True)
232231
usage_count = serializers.IntegerField(required=False)

tests/openedx_tagging/test_models.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ def test_get_root(self) -> None:
329329
del r["_id"] # Remove the internal database IDs; they aren't interesting here and a other tests check them
330330
assert result == [
331331
# These are the root tags, in alphabetical order:
332-
{"value": "Archaea", "child_count": 3, "descendant_count": 3, **common_fields},
333-
{"value": "Bacteria", "child_count": 2, "descendant_count": 2, **common_fields},
334-
{"value": "Eukaryota", "child_count": 5, "descendant_count": 5, **common_fields},
332+
{"value": "Archaea", "child_count": 3, **common_fields},
333+
{"value": "Bacteria", "child_count": 2, **common_fields},
334+
{"value": "Eukaryota", "child_count": 5, **common_fields},
335335
]
336336

337337
def test_get_child_tags_one_level(self) -> None:
@@ -345,11 +345,11 @@ def test_get_child_tags_one_level(self) -> None:
345345
del r["_id"] # Remove the internal database IDs; they aren't interesting here and a other tests check them
346346
assert result == [
347347
# These are the Eukaryota tags, in alphabetical order:
348-
{"value": "Animalia", "child_count": 7, "descendant_count": 7, **common_fields},
349-
{"value": "Fungi", "child_count": 0, "descendant_count": 0, **common_fields},
350-
{"value": "Monera", "child_count": 0, "descendant_count": 0, **common_fields},
351-
{"value": "Plantae", "child_count": 0, "descendant_count": 0, **common_fields},
352-
{"value": "Protista", "child_count": 0, "descendant_count": 0, **common_fields},
348+
{"value": "Animalia", "child_count": 7, **common_fields},
349+
{"value": "Fungi", "child_count": 0, **common_fields},
350+
{"value": "Monera", "child_count": 0, **common_fields},
351+
{"value": "Plantae", "child_count": 0, **common_fields},
352+
{"value": "Protista", "child_count": 0, **common_fields},
353353
]
354354

355355
def test_get_grandchild_tags_one_level(self) -> None:
@@ -363,13 +363,13 @@ def test_get_grandchild_tags_one_level(self) -> None:
363363
del r["_id"] # Remove the internal database IDs; they aren't interesting here and a other tests check them
364364
assert result == [
365365
# These are the Eukaryota tags, in alphabetical order:
366-
{"value": "Arthropoda", "child_count": 0, "descendant_count": 0, **common_fields},
367-
{"value": "Chordata", "child_count": 1, "descendant_count": 1, **common_fields},
368-
{"value": "Cnidaria", "child_count": 0, "descendant_count": 0, **common_fields},
369-
{"value": "Ctenophora", "child_count": 0, "descendant_count": 0, **common_fields},
370-
{"value": "Gastrotrich", "child_count": 0, "descendant_count": 0, **common_fields},
371-
{"value": "Placozoa", "child_count": 0, "descendant_count": 0, **common_fields},
372-
{"value": "Porifera", "child_count": 0, "descendant_count": 0, **common_fields},
366+
{"value": "Arthropoda", "child_count": 0, **common_fields},
367+
{"value": "Chordata", "child_count": 1, **common_fields},
368+
{"value": "Cnidaria", "child_count": 0, **common_fields},
369+
{"value": "Ctenophora", "child_count": 0, **common_fields},
370+
{"value": "Gastrotrich", "child_count": 0, **common_fields},
371+
{"value": "Placozoa", "child_count": 0, **common_fields},
372+
{"value": "Porifera", "child_count": 0, **common_fields},
373373
]
374374

375375
def test_get_depth_1_search_term(self) -> None:
@@ -381,7 +381,6 @@ def test_get_depth_1_search_term(self) -> None:
381381
{
382382
"value": "Archaea",
383383
"child_count": 3,
384-
"descendant_count": 3,
385384
"depth": 0,
386385
"usage_count": 0,
387386
"parent_value": None,
@@ -400,7 +399,6 @@ def test_get_depth_1_child_search_term(self) -> None:
400399
{
401400
"value": "Archaebacteria",
402401
"child_count": 0,
403-
"descendant_count": 0,
404402
"depth": 1,
405403
"parent_value": "Bacteria",
406404
"external_id": None,
@@ -511,7 +509,6 @@ def test_tags_deep(self) -> None:
511509
"depth": 3,
512510
"usage_count": 0,
513511
"child_count": 0,
514-
"descendant_count": 0,
515512
"external_id": None,
516513
"_id": 21, # These IDs are hard-coded in the test fixture file
517514
}

tests/openedx_tagging/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ def test_large_taxonomy(self):
16141614
self.client.force_authenticate(user=self.staff)
16151615

16161616
url = self.large_taxonomy_url + "?include_counts"
1617-
with self.assertNumQueries(1):
1617+
with self.assertNumQueries(3):
16181618
response = self.client.get(url)
16191619

16201620
assert response.status_code == status.HTTP_200_OK

0 commit comments

Comments
 (0)