Closed HealperDK closed 9 months ago
Hi,
Can you explain how unvalidated user input gets to be formatted as an str
? The client can only send an str
, they won’t be able to send None
. None
is a Python thing.
I do agree that __str__
should return an str
, however "None"
is already associated to None
and may cause confusion. Before to change __str__
, I’m considering to disallow None
as the PhoneNumber
value. It makes more sense (the model field would still allow null=True, but would not cast that into a PhoneNumber instance). Disallowing None
is what phonenumbers
does:
from phonenumbers import parse
>>> x = parse(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/freitafr/dev/django-phonenumber-field/venv/lib/python3.11/site-packages/phonenumbers/phonenumberutil.py", line 2927, in parse
raise NumberParseException(NumberParseException.NOT_A_NUMBER,
phonenumbers.phonenumberutil.NumberParseException: (1) The phone number supplied was None.
Not sure what the OP did wrong. The library behavior seems reasonable enough. Closing for inactivity.
In the PhoneNumber class, the string method is defined as
Since self raw_input is a user given input it could assume other values than a string. In my case, it was "None", but I got a
__str__ returned non-string (type NoneType)
exception, which caused a complete crash in a place that should have only returned a form-invalid. I would argue that replacing the last line withreturn str(self.raw_input)
would be a good fix.