From 023e0eebac0a850ece61363055d98964e6b9983e Mon Sep 17 00:00:00 2001 From: "James M. Allen" Date: Tue, 8 Aug 2017 10:12:38 -0400 Subject: [PATCH] Adding tests and transforms for DurationField Original PR: https://github.com/landscapeio/pylint-django/pull/99 Fixes https://github.com/landscapeio/pylint-django/issues/95 --- pylint_django/transforms/fields.py | 2 ++ .../transforms/transforms/django_db_models_fields.py | 6 ++++++ .../transforms/transforms/django_forms_fields.py | 8 +++++++- test/input/func_noerror_form_fields.py | 6 ++++++ test/input/func_noerror_model_fields.py | 6 ++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pylint_django/transforms/fields.py b/pylint_django/transforms/fields.py index ec311155..5a22dfda 100644 --- a/pylint_django/transforms/fields.py +++ b/pylint_django/transforms/fields.py @@ -45,6 +45,8 @@ def apply_type_shim(cls, context=None): # noqa base_nodes = MANAGER.ast_from_module_name('datetime').lookup('time') elif cls.name == 'DateField': base_nodes = MANAGER.ast_from_module_name('datetime').lookup('date') + elif cls.name == 'DurationField': + base_nodes = MANAGER.ast_from_module_name('datetime').lookup('timedelta') elif cls.name == 'ManyToManyField': base_nodes = MANAGER.ast_from_module_name('django.db.models.query').lookup('QuerySet') elif cls.name in ('ImageField', 'FileField'): diff --git a/pylint_django/transforms/transforms/django_db_models_fields.py b/pylint_django/transforms/transforms/django_db_models_fields.py index 0f1b6106..a0208fc0 100644 --- a/pylint_django/transforms/transforms/django_db_models_fields.py +++ b/pylint_django/transforms/transforms/django_db_models_fields.py @@ -110,6 +110,12 @@ def __new__(cls, verbose_name=None, name=None, auto_now=False, pass +class DurationField(datetime.timedelta, django_fields.DurationField): + if PY3: + def __new__(cls, verbose_name=None, name=None, **kwargs): + pass + + # ------- # misc diff --git a/pylint_django/transforms/transforms/django_forms_fields.py b/pylint_django/transforms/transforms/django_forms_fields.py index 18105429..4ace966d 100644 --- a/pylint_django/transforms/transforms/django_forms_fields.py +++ b/pylint_django/transforms/transforms/django_forms_fields.py @@ -101,7 +101,13 @@ def __new__(cls, input_formats=None, *args, **kwargs): pass - # -------- +class DurationField(datetime.timedelta, django_fields.DurationField): + if PY3: + def __new__(cls, *args, **kwargs): + pass + + +# -------- # choice class ChoiceField(object, django_fields.ChoiceField): diff --git a/test/input/func_noerror_form_fields.py b/test/input/func_noerror_form_fields.py index 2a910727..7ddb4ff5 100644 --- a/test/input/func_noerror_form_fields.py +++ b/test/input/func_noerror_form_fields.py @@ -15,6 +15,7 @@ class ManyFieldsForm(forms.Form): datetimefield = forms.DateTimeField(auto_now_add=True) datefield = forms.DateField(auto_now_add=True) decimalfield = forms.DecimalField(max_digits=5, decimal_places=2) + durationfield = forms.DurationField() emailfield = forms.EmailField() filefield = forms.FileField(name='test_file', upload_to='test') filepathfield = forms.FilePathField(path='/some/path') @@ -57,6 +58,11 @@ def datefield_tests(self): def decimalfield_tests(self): print(self.decimalfield.adjusted()) + def durationfield_tests(self): + now = datetime.now() + print(now - self.durationfield) + print(self.durationfield.total_seconds()) + def filefield_tests(self): print(self.filefield) print(self.imagefield) diff --git a/test/input/func_noerror_model_fields.py b/test/input/func_noerror_model_fields.py index d30bb1cd..08145fe5 100644 --- a/test/input/func_noerror_model_fields.py +++ b/test/input/func_noerror_model_fields.py @@ -18,6 +18,7 @@ class LotsOfFieldsModel(models.Model): datetimefield = models.DateTimeField(auto_now_add=True) datefield = models.DateField(auto_now_add=True) decimalfield = models.DecimalField(max_digits=5, decimal_places=2) + durationfield = models.DurationField() emailfield = models.EmailField() filefield = models.FileField(name='test_file', upload_to='test') filepathfield = models.FilePathField() @@ -69,6 +70,11 @@ def datefield_tests(self): def decimalfield_tests(self): print(self.decimalfield.compare(Decimal('1.4'))) + def durationfield_tests(self): + now = datetime.now() + print(now - self.durationfield) + print(self.durationfield.total_seconds()) + def filefield_tests(self): print(self.filefield.file) print(self.imagefield.file)