Skip to content

Commit

Permalink
Adding tests and transforms for DurationField
Browse files Browse the repository at this point in the history
Original PR: pylint-dev#99
Fixes pylint-dev#95
  • Loading branch information
jmaroeder authored and atodorov committed Jan 17, 2018
1 parent e523508 commit 023e0ee
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pylint_django/transforms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion pylint_django/transforms/transforms/django_forms_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions test/input/func_noerror_form_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/input/func_noerror_model_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 023e0ee

Please sign in to comment.