Open peterschutt opened 9 months ago
Other approaches I can think of:
class MyDTO(PydanticDTO[...]):
default_configs = [
DefaultConfig(name="exclude", value={"id"}, strategy="merge"),
]
Or, have the config object itself receive a merge callback that is called whenever we detect a config object is about to be overwritten by another config object in the MRO.
def merge_callback(self: DTOConfig, new: DTOConfig) -> DTOConfig:
return new.replace(exclude=self.exclude | new.exclude)
class BaseDTO(SQLAlchemyDTO[T]):
config = DTOConfig(exclude={"id"}, merge_callback=merge_callback)
Other approaches I can think of:
class MyDTO(PydanticDTO[...]): default_configs = [ DefaultConfig(name="exclude", value={"id"}, strategy="merge"), ]
What other strategies than merge
would be useful to have as a default?
Or, have the config object itself receive a merge callback that is called whenever we detect a config object is about to be overwritten by another config object in the MRO.
def merge_callback(self: DTOConfig, new: DTOConfig) -> DTOConfig: return new.replace(exclude=self.exclude | new.exclude) class BaseDTO(SQLAlchemyDTO[T]): config = DTOConfig(exclude={"id"}, merge_callback=merge_callback)
I like the idea of being able to supply a callable somehow, but I'm not sure if a method on the DTO or a callback is better :shrug:
The callback would allow a mixed approach though:
DTOConfig(..., merge_configs="merge")
DTOConfig(..., merge_configs="update")
def merge_configs(self: DTOConfig, other: DTOConfig) -> DTOConfig:
return other.replace(exclude=self.exclude | other.exclude)
DTOConfig(..., merge_configs=merge_configs)
When inheriting a DTO factory class, there is no way to merge the config of the inherited type with a config defined on the new class.
One idea is to have a callback method on AbstractDTO that handles the case where a 2nd config object is found in the mro of the DTO type.. something like:
Default would just return the latest one, which would preserve current overwrite behavior, but then you could do something like: