Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive: unexpected-keyword-arg, timespec in isoformat calls of DateTimeField values #392

Open
koniiiik opened this issue Feb 21, 2023 · 3 comments

Comments

@koniiiik
Copy link

Here's a weird corner case that I've run into in our code base, where if a model instance is created through direct instantiation, subsequent calls to isoformat with a timespec are flagged, even though this works fine when instantiating through ModelClass.objects.create(). Here's an example (settings.py is an empty file):

$ cat datetimefield.py
# pylint: disable=C
from django.db import models


class A(models.Model):
    field = models.DateTimeField(auto_now=True)


x = A()
x.save()
x.field.isoformat(timespec='seconds')

y = A.objects.create()
y.field.isoformat(timespec='seconds')

$ pylint --load-plugins=pylint_django --django-settings-module=settings datetimefield.py 
************* Module datetimefield
datetimefield.py:11:0: E1123: Unexpected keyword argument 'timespec' in method call (unexpected-keyword-arg)

-----------------------------------
Your code has been rated at 3.75/10

Here's the environment (I simply installed prospector and django):

asgiref==3.6.0
astroid==2.14.2
dill==0.3.6
Django==4.1.7
dodgy==0.2.1
flake8==5.0.4
flake8-polyfill==1.0.2
gitdb==4.0.10
GitPython==3.1.31
isort==5.12.0
lazy-object-proxy==1.9.0
mccabe==0.7.0
packaging==23.0
pep8-naming==0.10.0
platformdirs==3.0.0
poetry-semver==0.1.0
prospector==1.9.0
pycodestyle==2.9.1
pydocstyle==6.3.0
pyflakes==2.5.0
pylint==2.16.2
pylint-celery==0.3
pylint-django==2.5.3
pylint-flask==0.6
pylint-plugin-utils==0.7
PyYAML==6.0
requirements-detector==1.1.0
setoptconf-tmp==0.3.1
smmap==5.0.0
snowballstemmer==2.2.0
sqlparse==0.4.3
toml==0.10.2
tomli==2.0.1
tomlkit==0.11.6
typing_extensions==4.5.0
wrapt==1.14.1
@koniiiik
Copy link
Author

And I can also add another case that triggers the same false positive – methods on the model class itself:

class A(models.Model):
    field = models.DateTimeField(auto_now=True)

    def b(self):
        return self.field.isoformat(timespec='seconds')

@tofahelai
Copy link

I have encountered the same issue. Some more observations:

from django.db import models
from datetime import UTC

class A(models.Model):
    field = models.DateTimeField(auto_now=True)

    def b(self):
        self.field.replace(year=2001) # ok
        self.field.replace(month=1) # ok
        self.field.replace(day=1) # ok
        self.field.replace(hour=1) # unexpected-keyword-arg
        self.field.replace(second=1) # unexpected-keyword-arg
        self.field.replace(microsecond=1)  # unexpected-keyword-arg
        self.field.replace(tzinfo=UTC)  # unexpected-keyword-arg
        self.field.replace(fold=0)  # unexpected-keyword-arg
        self.field.today() # ok

@tofahelai
Copy link

The issue was encountered with python 3.11.7
I can not reproduce it with python 3.12.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants