﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34385	BaseTemporalField child fields causing AttributeError to be raised by calling form.is_valid().	Fábio David Freitas	nobody	"When validating a form with a temporal field that has been passed a non `str` native value, `AttributeError` is raised.

The problem happens in `DateField`, `DateTimeField`, and `TimeField` fields. The respective temporal field's `to_python` method treats the input assuming it is of type `str`, `datetime.datetime`, `datetime.date`, or `datetime.timedelta`. So when something like an integer is fed to a field and `clean` is called in the validation process, `AttributeError` is raised instead of a `ValidationError`, causing the form not to catch the error since it only catches `ValidationError` instances, and, therefore, raising an unexpected exception from a, in my opinion, validation error.

=== Reproducing the problem ===
You can test this through the form validation:
{{{
from django import forms

class MyForm(forms.Form):
    datefield = forms.DateField()

form = MyForm({'datefield': 1})
form.is_valid()
------------------------------------------------------------------------------------
>    def to_python(self, value):
>       value = value.strip()
E       AttributeError: 'int' object has no attribute 'strip'
}}}

Or directly with a field:
{{{
from django.forms import DateField

field = DateField()
field.to_python(1)
------------------------------------------------------------------------------------
>    def to_python(self, value):
>       value = value.strip()
E       AttributeError: 'int' object has no attribute 'strip'
}}}

Note that through the form validation process the problem is more apparent, since when calling `is_valid`, we do not expect an exception raised from said validation, we expect the form to store its validation errors so we can handle them directly. Passing an integer to a `DateField`, however, causes an exception to be raised when validating a form.

=== Test case ===
{{{
from django import forms
from unittest import TestCase


class TemporalFieldValidationTests(TestCase):
    def test_date_field_raises_validationerror_when_value_is_not_str_or_temporal_object(self):
        date_field = forms.DateField()

        with self.assertRaises(forms.ValidationError):
            date_field.clean(1)

    def test_time_field_raises_validationerror_when_value_is_not_str_or_temporal_object(self):
        time_field = forms.TimeField

        with self.assertRaises(forms.ValidationError):
            time_field.clean(1)

    def test_datetime_field_raises_validationerror_when_value_is_not_str_or_temporal_object(self):
        datetime_field = forms.DateTimeField()

        with self.assertRaises(forms.ValidationError):
            datetime_field.clean(1)
}}}

=== Considerations ===
- Tested on versions `3.2.16`, and `4.1.7`
- I will open a PR to solve this"	Bug	closed	Forms	4.1	Normal	wontfix	DateField DateTimeField validation AttributeError		Unreviewed	0	0	0	0	0	0
