Skip to content

Conversation

@rodrigobnogueira
Copy link

@rodrigobnogueira rodrigobnogueira commented Dec 27, 2025

Summary

Exports factory.SKIP to the public API, allowing users to exclude keys from DictFactory output.

Closes #1140

Motivation

When using DictFactory, there was no documented way to conditionally exclude keys from the generated dictionary. The SKIP sentinel was already available internally but not exposed to users.

Changes

  • Export SKIP from factory.declarations to the top-level factory package
  • Add check in Transformer.evaluate_pre to bypass transformation when value is SKIP
  • Add comprehensive tests for SKIP behavior with DictFactory
  • Add documentation example in reference.rst
  • Add ChangeLog entry

Usage

import factory

class ConfigFactory(factory.DictFactory):
    host = "localhost"
    port = 8080
    debug = factory.SKIP  # This key will be omitted

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

Testing

  • 8 new tests covering the feature in tests/test_skip.py
  • Full test suite passes (verified locally)
  • Covers: basic exclusion, per-instance override, skipping all fields, LazyFunction compatibility, singleton behavior, falsy nature, and Transformer interaction

Checklist

  • Tests pass locally
  • New tests added for new functionality
  • Documentation added reference.rst
  • ChangeLog updated
  • Follows existing code style

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
@chriswyatt
Copy link

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

@chriswyatt
Copy link

chriswyatt commented Jan 13, 2026

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
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
Copy link

chriswyatt commented Jan 13, 2026

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
@rodrigobnogueira
Copy link
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
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