Skip to content
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

Field.validators: list[BaseValidator] is too specific #231

Open
anentropic opened this issue Mar 26, 2024 · 0 comments
Open

Field.validators: list[BaseValidator] is too specific #231

anentropic opened this issue Mar 26, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@anentropic
Copy link

anentropic commented Mar 26, 2024

Here: https://github.com/sbdchd/django-types/blob/main/django-stubs/forms/fields.pyi#L30

The confusion is that BaseValidator is not actually the base class of all validators, nor describing the Protocol which custom validators have to satisfy. Instead it seems to be offered as a helpful starting point for defining your own custom validators.

But this means that typing Field.validators: list[BaseValidator] is too specific, because there are validators provided with Django such as RegexValidator which are neither subclass of BaseValidator nor would satisfy its interface if it was treated as a Protocol.

We can see the other django-stubs lib has the more valid type here by defining a simpler _ValidatorCallable alias:

https://github.com/typeddjango/django-stubs/blob/3f9b3c82c3038663beb49fb41c05734eedd95e8b/django-stubs/core/validators.pyi#L15C33-L15C54

https://github.com/typeddjango/django-stubs/blob/3f9b3c82c3038663beb49fb41c05734eedd95e8b/django-stubs/core/validators.pyi#L15C33-L15C54

I encountered this issue when trying to use RegexValidator in a custom form field and append it to the field's validators list... pyright in VS Code complains that: "RegexValidator" is incompatible with "BaseValidator"

@sbdchd sbdchd added the bug Something isn't working label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants