Skip to content

Adding tests and transforms for DurationField #99

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -60,6 +61,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