Skip to content

Expose factory.SKIP to omit a field from the generated object - #1142

Open
rodrigobnogueira wants to merge 1 commit into
FactoryBoy:masterfrom
rodrigobnogueira:feature/missing-sentinel
Open

rodrigobnogueira wants to merge 1 commit into
FactoryBoy:masterfrom
rodrigobnogueira:feature/missing-sentinel

Conversation

@rodrigobnogueira

@rodrigobnogueira rodrigobnogueira commented Dec 27, 2025 •

Copy link
Copy Markdown

Summary

Exports factory.SKIP, the sentinel Maybe already uses as its default branch. A field that resolves to it is left out of the arguments prepare_arguments builds, for every factory: a DictFactory omits the key, and a model factory falls back to the model's own default for that argument.

class ConfigFactory(factory.DictFactory):
    host = "localhost"
    debug = factory.SKIP

ConfigFactory()            # {'host': 'localhost'}
ConfigFactory(debug=True)  # {'host': 'localhost', 'debug': True}

Closes #1140.

Changes

  • Export SKIP from factory and document it with its own .. data:: SKIP entry next to Maybe.
  • Transformer no longer passes SKIP to its transform function, which raised TypeError for functions such as str.upper. This also covers a wrapped declaration that resolves to SKIP, such as Transformer(Maybe("flag", "x"), transform=...) with the flag off.
  • SKIP gets a repr and keeps its identity through copy, deepcopy and pickle, which the identity checks depend on.

@chriswyatt

Copy link
Copy Markdown

I tested this in one of my projects and it seems to work fine

@chriswyatt

chriswyatt commented Jan 13, 2026 •

Copy link
Copy Markdown

I've just realised that the SKIP declaration might already do what I want, though it's not obvious, as the documentation only shows it being used with factory.Maybe. I'm also not yet sure if SKIP would work with all sorts of edge cases (e.g. transformers, class inheritance with overrides, traits, etc.).

@rodrigobnogueira

Copy link
Copy Markdown
Author

The use of SKIP looks promising indeed. SKIP already exists internally in factory.declarations.SKIP and is already handled in prepare_arguments. However, SKIP isn't currently exported at the package level—you can't do factory.SKIP directly. It's only used internally for Maybe and Trait defaults.

The semantics of both SKIP and MISSING seem consistent: 'omit this attribute'

we could:

Export SKIP from factory/init.py
Add documentation showing it can be used standalone (not just with Maybe)

@chriswyatt

chriswyatt commented Jan 13, 2026 •

Copy link
Copy Markdown

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.

@rodrigobnogueira

Copy link
Copy Markdown
Author

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!

  • Exported SKIP from factory/init.py (now part of public API)
  • Removed all MISSING sentinel code (26 lines removed)
  • Added SKIP check to Transformer.evaluate_pre to prevent transformation errors
  • Renamed test_missing.py → test_skip.py and updated all 8 test methods

@rodrigobnogueira rodrigobnogueira changed the title feat: Add MISSING sentinel to exclude keys from DictFactory feat: Export SKIP sentinel for excluding keys from DictFactory Jan 13, 2026
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.
@rodrigobnogueira rodrigobnogueira changed the title feat: Export SKIP sentinel for excluding keys from DictFactory Expose factory.SKIP to omit a field from the generated object Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sentinel object for excluding items in DictFactory

2 participants