Support validation of subscripted collections.abc.Callable types#27
Support validation of subscripted collections.abc.Callable types#27maread99 wants to merge 1 commit into
Conversation
Previously valimp only verified that an input annotated with `collections.abc.Callable` was callable; any subscripted argument types and return type were ignored. Validation now additionally checks a callable input's signature against the subscripted types: - the number of positional arguments accepted by the input is validated as accommodating the number of subscripted argument types (unless the arguments are subscripted as `...`); - where the input annotates a parameter or its return, that annotation is validated as matching the corresponding subscripted type (unannotated parameters/return are treated as `typing.Any`). Validation is skipped (and passes) where the input's signature cannot be introspected, as can be the case for some built-in callables. Tests, the README example and the tutorial are updated to exhibit the new functionality.
|
Closing this without merging. On reflection, validating the subscripted types of a
Validating that an input simply is callable (bare Thanks for the work here; the analysis was useful in clarifying where the package's boundary sits. Generated by Claude Code |
Summary
Previously valimp only verified that an input annotated with
collections.abc.Callablewas callable; any subscripted argument types and return type were ignored (this was noted as a limitation in the README and module docstring).This PR adds validation of a callable input's signature against the subscripted types.
What's validated
For an annotation such as
collections.abc.Callable[[str, int], bool]:*args, parameters with defaults and required keyword-only parameters are all accounted for. Skipped when the arguments are subscripted as...(e.g.Callable[..., bool]).typing.Any(so unannotated callables, e.g. lambdas, conform on the basis of arity alone). Per-position annotations of arguments absorbed by*argsare not checked.Validation is skipped (and passes) where the input's signature cannot be introspected, as can be the case for some built-in callables.
Implementation
validates_against_callable_hint(and helperget_callable_positional_params) insrc/valimp/valimp.py, dispatched to fromvalidates_against_hintwhen aCallablehint is subscripted.Documentation
Callableparameter, and the corresponding return/error examples are updated. The supported-types list and limitations section are updated.collections.abc.Callablesection is rewritten to demonstrate conforming inputs (including lambdas), subscription-validation failures (arity, parameter-annotation and return-annotation mismatches) and the not-callable error.Tests
New focused tests cover arity (too few/too many/required keyword-only), parameter- and return-annotation mismatches,
...behaviour, unannotated callables,*args, defaults, callable instances and non-introspectable callables. The shared fixtures are updated so the callables they pass conform under the new behaviour.All 28 tests pass;
ruff check,ruff format --checkandmypyare clean (mypy reports only pre-existing errors unrelated to this change).https://claude.ai/code/session_01XP8SomvVNUYuuuv5qEGmUc
Generated by Claude Code