Skip to content

Commit d5f1096

Browse files
committed
Make preprocess_entity properly subclassable again
1 parent 0c8b9f5 commit d5f1096

1 file changed

Lines changed: 23 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

@@ -930,6 +933,15 @@ def _preprocess_value(cls, key: str, value: t.Any) -> t.Any:
930933
return None
931934
return _preprocess_value(value)
932935

936+
def _preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> EntityDefinition:
937+
if isinstance(body, PreprocessedEntityDefinition):
938+
assert body._partial == partial
939+
return body
940+
else:
941+
return PreprocessedEntityDefinition(
942+
self.preprocess_entity(body, partial), _partial=partial
943+
)
944+
933945
def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> EntityDefinition:
934946
"""
935947
Filter to prepare the body for a create or update call.
@@ -944,16 +956,11 @@ def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> En
944956
Returns:
945957
The body ready to be passed to `call`.
946958
"""
947-
if isinstance(body, PreprocessedEntityDefinition):
948-
return body
949-
950-
return PreprocessedEntityDefinition(
951-
{
952-
key: self._preprocess_value(key, value)
953-
for key, value in body.items()
954-
if value is not None
955-
}
956-
)
959+
return {
960+
key: self._preprocess_value(key, value)
961+
for key, value in body.items()
962+
if value is not None
963+
}
957964

958965
def list_iterator(
959966
self,
@@ -1096,7 +1103,7 @@ def create(
10961103
if parameters:
10971104
_parameters.update(parameters)
10981105
if body is not None:
1099-
body = self.preprocess_entity(body, partial=False)
1106+
body = self._preprocess_entity(body, partial=False)
11001107
if self.pulp_ctx.fake_mode:
11011108
body["pulp_href"] = "<FAKE ENTITY>"
11021109
self._entity = body
@@ -1155,7 +1162,7 @@ def update(
11551162
if parameters:
11561163
_parameters.update(parameters)
11571164
if body is not None:
1158-
body = self.preprocess_entity(body, partial=True)
1165+
body = self._preprocess_entity(body, partial=True)
11591166
if self.pulp_ctx.fake_mode:
11601167
assert self._entity is not None
11611168
if body is not None:
@@ -1342,14 +1349,14 @@ def converge(
13421349
return True, None, self.create(desired_entity)
13431350
else:
13441351
update_attributes = {}
1345-
for k, v in self.preprocess_entity(desired_attributes, partial=True).items():
1352+
for k, v in self._preprocess_entity(desired_attributes, partial=True).items():
13461353
if entity.get(k) != v:
13471354
update_attributes[k] = v
13481355
if update_attributes:
13491356
return (
13501357
True,
13511358
entity,
1352-
self.update(PreprocessedEntityDefinition(update_attributes)),
1359+
self.update(PreprocessedEntityDefinition(update_attributes, _partial=True)),
13531360
)
13541361
return False, entity, entity
13551362

0 commit comments

Comments
 (0)