Skip to content

ENH Adding TypeError support to param validation #24327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

naoise-h
Copy link
Contributor

@naoise-h naoise-h commented Sep 2, 2022

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 blanked ValueError is thrown, even when a TypeError would be more relevant. This PR adds a new is_correct_type to each constraint, and raises a TypeError when the value being checked doesn't match any type of the constraints. If the type of at least one constraint is matched, a ValueError is still thrown.

For example, for the following constraint,

"max_samples": [
            None,
            Interval(Real, 0.0, 1.0, closed="right"),
            Interval(Integral, 1, None, closed="left"),
        ],

if a value -1 is given, a ValueError is thrown, but if a value "1" is given, a TypeError is thrown.

is_correct_type is added as an abstract method to _Constraint, and the default behaviour of is_satisfied_by is to only check is_correct_type (since many constraints only check type). For constraints that also have a value check, this is handled by is_satisfied_by, which also checks is_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 for HasMethods for now, for similar reasons.

Tests have been updated accordingly.

@naoise-h naoise-h changed the title Adding TypeError support to param validation [ENH] Adding TypeError support to param validation Sep 2, 2022
@naoise-h naoise-h marked this pull request as ready for review September 2, 2022 11:22
@naoise-h
Copy link
Contributor Author

naoise-h commented Sep 7, 2022

I wonder would this be of interest to @jeremiedbb as they seem to have been spearheading the param validation implementation?

@glemaitre glemaitre changed the title [ENH] Adding TypeError support to param validation ENH Adding TypeError support to param validation Nov 4, 2022
@jeremiedbb
Copy link
Member

Thanks for the PR @naoise-h and sorry for the long delay. In the end we created a custom exception instead of using Value/TypeError: InvalidParameterError. This PR is thus obsolete now and we can close it. Thanks anyway for your contribution.

@jeremiedbb jeremiedbb closed this Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants