Closed sam-ghosh closed 1 week ago
Sorry, these two lines are confusing:
reveal_type(a.friendly_title) # int
a.friendly_title = 123 # does not show error
if mypy infers a.friendly_title
as int
(as you say), so, why it should raise a warning on a.friendly_title = 123
?
You are correct @sobolevn , it is my bad - i put wrong comment, i have fixed it now
should be
reveal_type(a.friendly_title) # str
There's nothing displaying what field friendly_title
is, but I would guess that it's declared as a CharField
.
The default behaviour from django-stubs
regarding CharField
declares that it accepts int
, str
or Combinable
as set types. Ref:
It's possible to change that if you set explicit set/get type arguments to CharField
. e.g. the following would declare that friendly_title
only accepts str
as set and get type:
class RUSupportTicket(models.Model):
friendly_title = CharField[str, str]()
Thanks @flaeppe , i understand a bit better now. What I now understand is that there is much more more to django-stubs than what is shown on the readme
For example syntax like models.CharField[str, str] is even possible is not shown with examples in the readme or anywhere else, do you have a recommendation of where I can see a "recipe book" or a "cookbook" kind of standard and best practices to use django-stubs?
While I agree that documentation could definitely improve. I unfortunately don't have any good place to point to for reading.
Closest thing I could come up with is to read Mypy's documentation
Bug report
Assignment of django model field to wrong type does not show error
What's wrong
How is that should be
System information
python
version: 3.11django
version: 4.2.9mypy
version: 1.7.0django-stubs
version: 4.2.7django-stubs-ext
version: 4.2.7