Skip to content

v0.13.0: Substitution ⚖️

Latest

Choose a tag to compare

@github-actions github-actions released this 02 Oct 09:56
· 40 commits to main since this release
Immutable release. Only release title and notes can be modified.
f0f58f4

⚖️ Substitution

A substitution of parties puts someone new in the case without disturbing it. The claim is the same, the record is the same, only the name on it changes. @probatio can now do that with the error it raises.

A library has its own exception hierarchy, and its callers expect one except to cover it. Put the decorator on a public method and an invalid argument raises MultipleInvalid instead, so the library ends up wrapping every decorated method to translate it. That is the same few lines in every library using the decorator.

class ClientValueError(ClientError, ValueError):
    """The caller passed an invalid argument."""


@probatio(error=ClientValueError)
async def stations(self, *, limit: Annotated[int, Range(min=0)] = 100) -> list[Station]: ...

error takes an exception class, or any callable that receives the Invalid. What it returns is raised from the original, so the path and the sub errors stay reachable as __cause__. Substituted, not discharged.

Only argument validation is translated. A result failing returns still raises MultipleInvalid, because that is the function's mistake and not the caller's. A malformed call still raises the TypeError Python gives it. Whatever the body raises is left alone, so a rule spanning several arguments stays the library's own error and nothing of someone else's is quietly rewritten.

Leave error out and the wrapper is never added, so the default path costs nothing. An error that cannot be called is refused with a SchemaError while decorating, rather than at the first bad argument.

pip install probatio

📚 Docs: https://probatio.frenck.dev

Put your data to the proof. ⚖️

../Frenck

                       

Blogging my personal ramblings at frenck.dev

What's changed

✨ New features

  • Let the probatio decorator raise an error of your own @frenck (#437)

⬆️ Dependency updates