# d42-refine

Schema intersection for d42. `refine(base, narrow)` returns a schema that
accepts only values accepted by both `base` and `narrow`.

## API

```python
from d42 import schema
from d42_refine import (
    RefinementError,
    Refiner,
    disable_schema_substitution,
    enable_schema_substitution,
    is_enabled,
    is_refinement,
    make_refinement_error,
    refine,
)
```

- `refine(schema, schema) -> Schema` — intersection of two schemas.
- `refine(schema, value) -> Schema` — same as
  `d42.substitution.substitute(schema, value)`.
- `is_refinement(base, narrow) -> bool` — `True` if `refine` would succeed.
- `RefinementError` — wider/incompatible/undecidable input. Message starts with
  a path such as `$['items'][0]: ...`.
- `enable_schema_substitution()` — enables `schema % schema`. Off by default;
  stays enabled globally until `disable_schema_substitution()`.
- `schema % value` is always the original d42 substitution.
- `Refiner.refine(base, narrow, path=Nil)` and `Refiner.child_path(path, key)`
  are for custom `__d42_refine__` implementations.

## Examples

```python
from d42 import optional, schema
from d42_refine import refine

refine(
    schema.dict({"id": schema.int, "name": schema.str}),
    schema.dict({"id": schema.int.min(1), "name": schema.str.len(1, 10)}),
)
# schema.dict({"id": schema.int.min(1), "name": schema.str.len(1, 10)})

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

refine(schema.int, 5)  # schema.int(5)
```

## Semantics

- `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 refined recursively; required/optional/absent and `...` only
  when allowed by the base.
- `any`: each branch is refined; an incompatible branch is an error. One branch
  collapses to that branch.
- Aliases are preserved; `datetime` refines `date`; `bool` refines `int`.
- Equality is allowed. Otherwise `RefinementError`.
- Different regexes raise `RefinementError` because containment is undecidable.

## Custom type

```python
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(
            self.item, narrow.item, refiner.child_path(path, "item"))
        return BoxSchema(item)
```

A custom hook receives `refiner`, `narrow`, and `path`; it must return a new
schema. Without a hook, equal schemas are a no-op and different classes fail.
