Expose factory.SKIP to omit a field from the generated object - #1142
rodrigobnogueira wants to merge 1 commit into
Conversation
|
I tested this in one of my projects and it seems to work fine |
|
I've just realised that the |
|
The use of SKIP looks promising indeed. SKIP already exists internally in The semantics of both SKIP and MISSING seem consistent: 'omit this attribute' we could: Export |
|
Yes, that sounds good. And I guess in cases where you are using, say, a dataclass as a model, SKIP makes more sense than MISSING, as you will be getting a default value instead. |
|
The MISSING constant was removed and only a small number of adjustments were necessary to use SKIP instead. The previous tests for MISSING have been updated for SKIP. It was really a great insight @chriswyatt . thanks!
|
SKIP already exists as factory.declarations.SKIP: Maybe uses it as its default branch, and prepare_arguments drops any field that resolves to it, for every factory. Export it as factory.SKIP and document it, so a DictFactory can leave a key out and a model factory can fall back to the model's default for an argument. A Transformer passed SKIP to its transform function, which raised TypeError for functions such as str.upper; it now returns SKIP as is. SKIP also gets a repr, and keeps its identity through copy, deepcopy and pickle, which the identity checks depend on. Closes FactoryBoy#1140.
eaafc37 to
a5be1b9
Compare
Summary
Exports
factory.SKIP, the sentinelMaybealready uses as its default branch. A field that resolves to it is left out of the argumentsprepare_argumentsbuilds, for every factory: aDictFactoryomits the key, and a model factory falls back to the model's own default for that argument.Closes #1140.
Changes
SKIPfromfactoryand document it with its own.. data:: SKIPentry next toMaybe.Transformerno longer passesSKIPto itstransformfunction, which raisedTypeErrorfor functions such asstr.upper. This also covers a wrapped declaration that resolves toSKIP, such asTransformer(Maybe("flag", "x"), transform=...)with the flag off.SKIPgets a repr and keeps its identity through copy, deepcopy and pickle, which the identity checks depend on.