[FEATURE] Rework the hook system #1673
Labels
Component: Hooks
Extensibility and customization
Difficulty: Hard
Complex, needs deep understanding
Priority: Medium
Planned for regular releases
Type: Feature
New functionalities or enhancements
UX: Usability
Enhances user experience
Milestone
Problem
Applying hooks for different operations is clunky:
All the dispatching is in the same place and all the logic for different endpoints is also there.
Solution
Filters
Extend the hook system to allow users to specify the scope of hook functions through a decorator instead of implementing conditions inside the hook function body. This would simplify the filtering process and make it easier to use.
Naming & semantic change
The hook names are currently confusing and too verbose, making them difficult to use comfortably. The suggestion is to simplify the hook names to make them clearer and easier to use. This could include options such as
{map,filter,flatmap}_{query,path_parameters,cookie,header,body,case}
to match the Hypothesis' semantics:Filtration options are
apply_to
/skip_for
However, we need to be clear about the order in which these hooks are applied to avoid confusion.
The current hooks that accept strategies could be renamed to
generate_{query,path_parameters,cookie,header,body,case}
because thebefore
prefix is a bit misleading.Other considerations
Testing
It might be not really convenient to test hooks now, but we can expose some helpers:
Schemathesis basically does something similar to test built-in hooks. Exposing a cleaned-up version would benefit other people who develop hooks.
Validation
Validate hooks input and let the user know about some configuration issues immediately:
InvalidConfiguration: The "filter_query" hook defined at hooks.py:42 does not exclude anything and therefore has no effect
If a hook was not applied at all (some wrong filters), then we can report it as a warning at the end of the test run:
Debugging
It would be handy to see the filter result:
Such a feature would actually be really helpful for other filter cases (like the ones planned in #1256) as sometimes users report that filters they input do not match what they wanted and having a way to debug it would be really helpful.
Additionally, we may have some
debug
flag that enables extra output from a hook:But I am not sure where to output this info.
We might want to customize texts for exceptions that happen inside hooks to provide more concise info to the user:
Exception happened in the "filter_query" hook defined in hooks.py:42
+ show only relevant stack frames to avoid visual clutter.Documentation
This change surely needs a full rewrite with way more examples + a migration guide from old hooks.
More thoughts
pluggy
does and apply some extra arguments (likefirst
/last
) to control it. Now sure whether it will simplify the API though.or
/and
- it might help for some cases, but probably will complicate the API.The text was updated successfully, but these errors were encountered: