[Django] #34809: Pycharm typechecker complains

5 views
Skip to first unread message

Django

unread,
Sep 1, 2023, 6:31:16 AM9/1/23
to django-...@googlegroups.com
#34809: Pycharm typechecker complains
-----------------------------------------+------------------------
Reporter: nilslindemann | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
#30345 was closed, probably because it was not clear that the code will
run, but the PyCharm type checker will complain. The solution posted
there, namely, to write ...

{{{
return self.pub_date >= (timezone.now() -
datetime.timedelta(days=1)).timestamp()
}}}

... instead of ...

{{{
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
# PyCharm complains: "Expected type 'timedelta', got 'DateTimeField'
instead"
}}}

... will result in the correct type returned and PyCharm will not complain
anymore.

[https://django.readthedocs.io/en/stable/intro/tutorial02.html#playing-
with-the-api The related code example in the docs].

See also [https://stackoverflow.com/questions/61468142/pycharm-ide-
warning-on-django-doc-expected-type-timedelta-got-datetimefield this Stack
Overflow question].

--
Ticket URL: <https://code.djangoproject.com/ticket/34809>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages