-
Notifications
You must be signed in to change notification settings - Fork 15
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
Reconsider mandatory explicit disambiguation #173
Comments
I think the issue from the pendulum comment is that it's rather hard to go from a string containing a date to a corresponding date = whenever.Date.parse_common_iso('2021-01-01')
z = whenever.LocalDateTime(date.year, date.month, date.day).assume_tz("US/Eastern", disambiguate="compatible")
z = whenever.ZonedDateTime(date.year, date.month, date.day, tz="US/Eastern") Maybe a Note that you're not 100% consistent about the On a side note: |
There's >>> Date(2020, 3, 2).at(Time.MIDNIGHT)
LocalDateTime(2020-03-02 00:00:00) although you can also LocalDateTime.strptime("2020-03-03", "%Y-%m-%d")
Correct, although I cheated a bit by saying all methods require it 😁 . The reason for the constructor exemption is that it's usually called with literals, which are likely correct. More on topic again:
Happy to hear it! What do you think of making |
For improving the parsing API, see #174 |
Thanks for considering easier usability options! Another minor note around basic usage showing we can't see the parameters for completion or in-line documentation: In [7]: f = whenever.LocalDateTime.from_py_datetime(dateutil.parser.parse("2021-01-01 01:01:01"))
In [8]: f
Out[8]: LocalDateTime(2021-01-01 01:01:01)
# but, no documentation or tab completion available on extra methods
In [9]: f.assume_tz?
Docstring: Assume the datetime is in a timezone
Type: builtin_method
As you noted already, it's the "burden of correctness" problem. It also depends on how "serious" the system is. If I'm just scheduling timestamps to change my RGB light strip modes, an extra 20+ characters of "correctness" on every time manipulation feels excessive. but, for more serious systems, we don't want things exploding due to hidden assumptions. Do we want our systems to randomly crash every 5 years when we actually encounter a '60' leap second value? Or the popular "extra ISO week in the year" surprises too. I think it would be nice to have some "default behavior" options (the evil ThingFactoryFactoryConstructor pattern) to use a consistent instance of the API where all operations conform to the user's preferred rounding/fallback by default. This would be the same as everybody writing custom wrappers around all operations themselves, but just provided library-side instead of user-side every time. |
Maybe a stub file? It's really annoying that the literal values don't show up!
But why don't you schedule them with I think a good design makes correct things easy and wrong things hard. |
Good point! I think it depends on the input structure and UI being consumed. Lots of parsers like returning no-timezone datetime objects then we just need to populate a timezone. Some of our current usage is reading free-form time fields on behalf of users (command line arguments), then applying their time to a specific timezone configured elsewhere. So the user only provides "tomorrow at 4pm" and we schedule for "now + 1 day @ 4pm US/Eastern". I converted a platform from pendulum last weekend and a lot of the changes were Just usability decoration I guess, but the much better |
I suspect this is indeed the case 😢 . I investigated this before, and found that each editor/linter basically does something different. Although I did check the "major" editors worked: ✅ Neovim ✅ PyCharm ✅ VS code I suspect the iPython/jupyter shell you've got doesn't support it. A quick google I found: ipython/ipython#10044. Looks like
This would be great, although toggling defaults globally wouldn't work because you'd affect other libraries using One interesting option could be a linter or alternate type stub file that would more strictly enforce these parameters—only if you explicitly enabled/installed it. This would be tricky to implement, but could be a "have your cake and eat it too" solution. Casual users have their "it just works" API, and responsible teams can set an extra check on their code. That said, I really like the idea of educating users through the API, although I empatise that's a limit to how mach 'annoyance' users can take 😄. |
Another option could be to use the The downside of this is that this warning would only appear at runtime. |
In any case, #178 at least improves the documentation of all methods involving disambiguation, so the explanation is "a click away" in most IDEs |
In Jiff, I followed in Temporal's footsteps here and just made "compatible" disambiguation the default without any need for explicit opt-in. You can still change the policy to "later", "earlier" or "reject," just like in Temporal. I went this way generally for two reasons:
|
Ok, looks like there's enough reason to move towards a I'll also investigate how well it'd work to have optional "strict" stubs that do enforce this parameter—for those that do want it. Important though: I'll need to double-check some APIs like |
The change is almost ready, and there was indeed a potentially surprising edge case in The case occurs when dealing with repeated times. Imagine with have the "second" occurrence of 2:15 AM in the day the clock goes backwards. Replacing the "minute" component with
For now, I accept these slight weirdnesses—especially since it only affects very rare cases. (note: Temporal solves this by not allowing the equivalent |
Ok, the change is out with version 0.6.16. I'll close this issue, and create a follow-up if needed given the edge case above |
Should forced explicit disambiguation remain the default? Currently, the
disambiguate=
kwarg is required in all relevant methods:A common complaint is this makes for a clunky/noisy API (e.g. python-pendulum/pendulum#590 (comment))
I see the following options:
Keep the behavior.
pro:
con:
make
disambiguate=
optional, with"compatible"
as the default.pro:
"compatible"
option anywaycon:
make
disambiguate=
optional, with"raise"
as the default.pro:
con:
use an "optional" stub file that increases strictness. Users that want strictness can install it.
pro:
con:
use
warnings
(NOTE: this option was introduced in a comment later, but included here for a good overview). Basically the same as option (2), but trigger aImplicitDisambiguationWarning
that links to the docs. This warning can be disabled globally, or—of course—convince the user to specifydisambiguation
.pro:
con:
The text was updated successfully, but these errors were encountered: