sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
186 stars 62 forks source link

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

Open anentropic opened 5 months ago

anentropic commented 5 months ago

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"