Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

d42-refine

Schema intersection for d42. refine(base, narrow) accepts only values accepted by both schemas. Data is delegated to d42.substitution.substitute.

Install

pip install d42-refine

Usage

from d42 import optional, schema
from d42_refine import refine

base = schema.dict({
    "id": schema.int,
    "name": schema.str,
    optional("deleted_at"): schema.str,
})
narrow = schema.dict({
    "id": schema.int.min(1),
    "name": schema.str.len(1, 10),
})

refined = refine(base, narrow)
# schema.dict({
#     "id": schema.int.min(1),
#     "name": schema.str.len(1, 10),
#     optional("deleted_at"): schema.str,
# })

The result is a normal d42 schema: validate, fake, represent, etc. work.

Data substitution

refine(schema.dict({"id": schema.int}), {"id": 1})
# same as d42.substitution.substitute(...)

% operator (opt-in)

schema % value always uses d42 substitution and never changes.

schema % schema refines only after enable_schema_substitution(), which enables it globally for all schemas until disable_schema_substitution():

schema.int % 42                                 # schema.int(42) — d42 substitution
schema.int.min(1) % schema.int.min(10).max(20)
# SubstitutionError: IntSchema given where int is expected

from d42_refine import disable_schema_substitution, enable_schema_substitution

enable_schema_substitution()

schema.int.min(1) % schema.int.min(10).max(20)  # schema.int.min(10).max(20)
schema.int % 42                                 # schema.int(42) — unchanged

disable_schema_substitution()

any branches

refine(schema.str, schema.any(schema.str("3"), schema.str("4")))
# schema.any(schema.str("3"), schema.str("4"))

refine(schema.str, schema.any(schema.str("3")))
# schema.str("3")  # single branch is collapsed

An incompatible branch raises RefinementError.

Check without an exception

from d42_refine import is_refinement

is_refinement(schema.int.min(1), schema.int.min(10))  # True
is_refinement(schema.int.min(1), schema.int)         # False

Rules

  • int / float: stricter min / max.
  • str / bytes: stricter lengths; contains only narrower; alphabet only a subset; regex only equal.
  • list: elements, type, ..., lengths, and unique are merged.
  • dict: keys are refined recursively; required / optional / absent and ... are preserved only if allowed by the base.
  • any: every branch is refined; incompatible branch = error.
  • Aliases are preserved; datetime refines date; bool refines int.
  • Equality is allowed. Otherwise RefinementError contains the path:
    refine(schema.str.regex(r"^[a-z]+$"), schema.str.regex(r"^[0-9]+$"))
    # RefinementError: $: ...
    

Custom type

  1. Add the hook to the schema class:
    def __d42_refine__(self, refiner, *, narrow, path):
        ...
    
  2. Refine nested schemas through refiner:
    from d42.declaration import Props, Schema
    from d42_refine import make_refinement_error
    
    class BoxSchema(Schema[Props]):
        def __init__(self, item):
            super().__init__(Props({"item": item}))
    
        @property
        def item(self):
            return self.props.get("item")
    
        def __d42_refine__(self, refiner, *, narrow, path):
            if not isinstance(narrow, BoxSchema):
                raise make_refinement_error(
                    path, f"BoxSchema can not be refined with {narrow.__class__.__name__}")
    
            item = refiner.refine(  # recurse into the nested schema
                self.item, narrow.item, refiner.child_path(path, "item"))
            return BoxSchema(item)  # new schema, do not mutate self
    
  3. Works inside dict, list, and any. Without the hook, equal schemas are a no-op and different classes raise RefinementError.

Limitations

Containment of different regexes is undecidable, so d42-refine raises RefinementError instead of guessing. Equal patterns and concrete values work.

Development

make install
make test
make lint
make coverage

An LLM-oriented API summary is packaged as d42_refine/llms.txt.

Release

Base version lives in d42_refine/version. CI appends EXT_VERSION (a1, b1, rc1) to it.

make tag          # v0.1.0
make tag-alpha    # v0.1.0a1
make tag-beta
make tag-rc
make push-tags

Publishing uses GitHub OIDC trusted publishing, no PyPI token. Alpha can be published manually: Actions → Check and publish → Run workflow → Publish alpha. Configure the PyPI trusted publisher environment as manual-gate for it.

License

Apache-2.0

Metadata

Release files for d42-refine 0.1.0a2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for d42-refine 0.1.0a2
File Size Uploaded Provenance Actions
d42_refine-0.1.0a2.tar.gz 20.9 kB With provenance Details for d42_refine-0.1.0a2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for d42-refine 0.1.0a2
File Interpreter ABI Platform Actions
d42_refine-0.1.0a2-py3-none-any.whl Python 3 none any Details for d42_refine-0.1.0a2-py3-none-any.whl

Total release size: 37.1 kB

Release files / d42_refine-0.1.0a2.tar.gz

Download URL d42_refine-0.1.0a2.tar.gz
Size 20.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8fb66f29ef39b84fb3859707d2095fa23546d38a1e9a745e6e28008287392c41
BLAKE2b-256 checksum
How to use checksums
b5983b8bd8ce47aebade321cc8d9f6d33f58422b72c1e28c6d91882c9b2488ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / d42_refine-0.1.0a2-py3-none-any.whl

Download URL d42_refine-0.1.0a2-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b220e38b43a03212f4d0db8ddec16bb1b4bea9cc45303c4253297ca3da2f80c6
BLAKE2b-256 checksum
How to use checksums
ad7f2aeaf20084954f19a8085b2a7957a0f6f2fb8fdcd608e82779a8d3156ebd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for d42-refine 0.1.0a2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for d42_refine-0.1.0a2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page