-
Notifications
You must be signed in to change notification settings - Fork 85
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
Deprecate allowing datetime.datetime instances as values for Date traits #1441
Deprecate allowing datetime.datetime instances as values for Date traits #1441
Conversation
@@ -73,33 +73,45 @@ def test_assign_non_date(self): | |||
message = str(exception_context.exception) | |||
self.assertIn("must be a date or None, but", message) | |||
|
|||
def test_assign_datetime(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is redundant with the new test_assign_datetime_with_allow_datetime_not_given
test
@kitchoi Would this PR meet your project's needs? |
@mdickinson Thank you for this. I haven't reviewed the code change in details but the intended behaviour looks good to me!
Recalling the other project that motivated #398, I believe the answer is yes. With the caveat that my memory of the context is rather dated, here is the details: For the project that revived the discussion around disallowing datetime for Date, Minor: Did you mean to change |
Yes, we still don't have a good solution to this problem. Part of the solution might be simply to recommend that deprecation warnings always be turned on during manual testing of an application, and providing patterns for doing that. But I think this discussion is out of scope for Traits itself. |
Thanks! I didn't, but it's a good idea. Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I was going through the documentation to see if anything needed to be updated - but from the looks of it, We might want to include |
Agreed; opened #1445 for this. |
This PR paves the way for eventually making it illegal to pass a
datetime.datetime
instance to aDate
trait:allow_datetime
toNone
rather thanTrue
allow_datetime
is not supplied and the user tries to assign adatetime.datetime
value to aDate
trait, aDeprecationWarning
is issued. In the future (ideally in Traits 7.0), aTraitError
will be raised.Rationale: right now,
Date
allows any value that's an instance of the Python std. lib.datetime.date
class. Somewhat surprisingly, Python'sdatetime.datetime
is a subclass ofdatetime.date
. But in typical uses for theDate
trait, we really do want to specify a date and not a datetime.Users who do want to allow datetimes in addition to date can modify their code in a number of ways to avoid the warning: for example, using
Union(Datetime(), Date())
,Instance(datetime.date)
, orDate(allow_datetime=True)
.Users can also opt in to the future behaviour right now by using
Date(allow_datetime=False)
.Checklist
docs/source/traits_api_reference
) (docstring updated)Update User manual (N/Adocs/source/traits_user_manual
)Update type annotation hints inN/A (the default fortraits-stubs
allow_datetime
has changed, but that default isn't recorded in the stub)