Skip to content

Commit e04a5de

Browse files
committed
Make preprocess_entity properly subclassable again
(cherry picked from commit 930e766)
1 parent 5792880 commit e04a5de

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

pulp-glue/src/pulp_glue/common/context.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def _inner(f: T) -> T:
7070

7171

7272
class PreprocessedEntityDefinition(dict[str, t.Any]):
73-
pass
73+
def __init__(self, /, *args: t.Any, _partial: bool, **kwargs: t.Any):
74+
super().__init__(*args, **kwargs)
75+
self._partial: bool = _partial
7476

7577

7678
EntityDefinition = dict[str, t.Any] | PreprocessedEntityDefinition
@@ -132,7 +134,8 @@ def preprocess_payload(payload: EntityDefinition) -> EntityDefinition:
132134
return payload
133135

134136
return PreprocessedEntityDefinition(
135-
{key: _preprocess_value(value) for key, value in payload.items() if value is not None}
137+
{key: _preprocess_value(value) for key, value in payload.items() if value is not None},
138+
_partial=False,
136139
)
137140

138141

@@ -957,6 +960,15 @@ def _preprocess_value(cls, key: str, value: t.Any) -> t.Any:
957960
return None
958961
return _preprocess_value(value)
959962

963+
def _preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> EntityDefinition:
964+
if isinstance(body, PreprocessedEntityDefinition):
965+
assert body._partial == partial
966+
return body
967+
else:
968+
return PreprocessedEntityDefinition(
969+
self.preprocess_entity(body, partial), _partial=partial
970+
)
971+
960972
def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> EntityDefinition:
961973
"""
962974
Filter to prepare the body for a create or update call.
@@ -971,16 +983,11 @@ def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> En
971983
Returns:
972984
The body ready to be passed to `call`.
973985
"""
974-
if isinstance(body, PreprocessedEntityDefinition):
975-
return body
976-
977-
return PreprocessedEntityDefinition(
978-
{
979-
key: self._preprocess_value(key, value)
980-
for key, value in body.items()
981-
if value is not None
982-
}
983-
)
986+
return {
987+
key: self._preprocess_value(key, value)
988+
for key, value in body.items()
989+
if value is not None
990+
}
984991

985992
def list_iterator(
986993
self,
@@ -1123,7 +1130,7 @@ def create(
11231130
if parameters:
11241131
_parameters.update(parameters)
11251132
if body is not None:
1126-
body = self.preprocess_entity(body, partial=False)
1133+
body = self._preprocess_entity(body, partial=False)
11271134
if self.pulp_ctx.fake_mode:
11281135
body["pulp_href"] = "<FAKE ENTITY>"
11291136
self._entity = body
@@ -1184,7 +1191,7 @@ def update(
11841191
if parameters:
11851192
_parameters.update(parameters)
11861193
if body is not None:
1187-
body = self.preprocess_entity(body, partial=True)
1194+
body = self._preprocess_entity(body, partial=True)
11881195
if self.pulp_ctx.fake_mode:
11891196
assert self._entity is not None
11901197
if body is not None:
@@ -1372,14 +1379,14 @@ def converge(
13721379
return True, None, self.create(desired_entity)
13731380
else:
13741381
update_attributes = {}
1375-
for k, v in self.preprocess_entity(desired_attributes, partial=True).items():
1382+
for k, v in self._preprocess_entity(desired_attributes, partial=True).items():
13761383
if entity.get(k) != v:
13771384
update_attributes[k] = v
13781385
if update_attributes:
13791386
return (
13801387
True,
13811388
entity,
1382-
self.update(PreprocessedEntityDefinition(update_attributes)),
1389+
self.update(PreprocessedEntityDefinition(update_attributes, _partial=True)),
13831390
)
13841391
return False, entity, entity
13851392

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pulp_glue.common.context import PreprocessedEntityDefinition, PulpContext, PulpEntityContext
2+
3+
4+
def test_preprocess_entity_is_only_called_once(mock_pulp_ctx: PulpContext) -> None:
5+
entity_ctx = PulpEntityContext(mock_pulp_ctx)
6+
7+
preprocessed = entity_ctx._preprocess_entity({})
8+
assert isinstance(preprocessed, PreprocessedEntityDefinition)
9+
10+
# Now call it again and see if the returned object is the same, not just equal.
11+
assert preprocessed is entity_ctx._preprocess_entity(preprocessed)

0 commit comments

Comments
 (0)