Hello,
Please correct me if I am making a mistake. But this is my observation. I subclassed CharField and added a custom parameter like this:
class ISBN10Field(CharField):
default_validators = [ISBN10Validator]
length = 10
normalize: bool
def __init__(self, normalize: bool = True, *args, **kwargs):
self.normalize = normalize
if self.normalize:
kwargs["max_length"] = self.length
super().__init__(*args, **kwargs)
field = ISBN10Field(normalize=False)
print(field.length)
Two issues:
Because of __new__ method, the type of field is always CharField[str] and not ISBN10Field. That causes attribute validation to fail. Also, because of overload, adding new parameters gives errors. The last two lines generate the following errors:
tests.py:38:12 - error: No overloads for "__new__" match the provided arguments
Argument types: (Literal[False]) (reportCallIssue)
tests.py:68:32 - error: Cannot access attribute "length" for class "CharField[str]"
Attribute "length" is unknown (reportAttributeAccessIssue)
Any ideas on how to fix this?
Hello,
Please correct me if I am making a mistake. But this is my observation. I subclassed
CharFieldand added a custom parameter like this:Two issues:
Because of
__new__method, the type offieldis alwaysCharField[str]and notISBN10Field. That causes attribute validation to fail. Also, because of overload, adding new parameters gives errors. The last two lines generate the following errors:Any ideas on how to fix this?