ENH Adding TypeError support to param validation #24327
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Follow-on from #24224, and relevant for #23462 and #22722.
What does this implement/fix? Explain your changes.
_validate_params
is currently being rolled out to all estimators as part of #23462, however when a parameter doesn't match the constraints, a blankedValueError
is thrown, even when aTypeError
would be more relevant. This PR adds a newis_correct_type
to each constraint, and raises aTypeError
when the value being checked doesn't match any type of the constraints. If the type of at least one constraint is matched, aValueError
is still thrown.For example, for the following constraint,
if a value
-1
is given, aValueError
is thrown, but if a value"1"
is given, aTypeError
is thrown.is_correct_type
is added as an abstract method to_Constraint
, and the default behaviour ofis_satisfied_by
is to only checkis_correct_type
(since many constraints only check type). For constraints that also have a value check, this is handled byis_satisfied_by
, which also checksis_correct_type
(this ensures that the entire constraint is satisfied, and not just the value).Any other comments?
This should give more granular feedback to a user for incorrect parameter specification.
generate_invalid_params
: The invalid check for_MissingValues
raises a TypeError at present (generated_invalid_params
is intended for ValueErrors only), and the constraint allows for all possible values for each type, so I can't think of a way to generate a ValueError (unless I write new methods for generating params of invalid type). I have also commented out the invalid param check forHasMethods
for now, for similar reasons.Tests have been updated accordingly.