-
Notifications
You must be signed in to change notification settings - Fork 410
feat: Export SKIP sentinel for excluding keys from DictFactory #1142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: Export SKIP sentinel for excluding keys from DictFactory #1142
Conversation
This adds a MISSING sentinel value that can be used with DictFactory
to exclude specific keys from the generated dictionary.
Example usage:
class MyFactory(factory.DictFactory):
name = factory.Faker('name')
email = factory.MISSING # This key will be excluded
The MISSING sentinel:
- Is a singleton with a falsy boolean value
- Can be used with Transformer declarations
- Can be conditionally returned from LazyFunction
- Can be overridden at call time to include the key
|
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. |
- Export SKIP from factory/__init__.py to public API - Remove all MISSING sentinel code (utils.py, base.py) - Add SKIP check in Transformer.evaluate_pre to bypass transformation - Rename test_missing.py to test_skip.py and update all tests - All 447 tests passing
|
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!
|
Summary
Exports
factory.SKIPto the public API, allowing users to exclude keys fromDictFactoryoutput.Closes #1140
Motivation
When using
DictFactory, there was no documented way to conditionally exclude keys from the generated dictionary. TheSKIPsentinel was already available internally but not exposed to users.Changes
SKIPfromfactory.declarationsto the top-level factory packageTransformer.evaluate_preto bypass transformation when value isSKIPUsage
Testing
tests/test_skip.pyLazyFunctioncompatibility, singleton behavior, falsy nature, andTransformerinteractionChecklist