-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
I want to be able to declare "naive datetime" or "aware datetime" as types #10067
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
Comments
(I have to assume someone has asked for this already, but I promise I searched for it before filing!) |
I happened to run into this due to the link with issue 2087. Assuming you know the data type somewhere in the process, wouldn't this be easily resolvable by using
This would yield incompatible type errors for |
That's the trick though, isn't it :-). I'll note that even in your example, you might as well have used (Also, no need for
but… you just defined two mypy-specific subtypes to accomplish this? |
What if |
I did a proof of concept here by just copying over the pyi file and starting from there, rather than trying to convince mypy to somehow transform the existing stubs (which is maybe impossible). |
PEP 696 has a way to solve this using default and bound which should be timezone | None. datetime[timezone] would mean timezone-aware datetimes are expected to be passed, datetime[None] means naive datetimes and datetime[timezone | None] means you don’t care about awareness. I actually implemented this change to my local copy of typeshed and found it surprisingly ergonomic. |
@Gobot1234 brilliant! Could you elaborate on how you added this to your configuration? |
I think this is also related to the way that |
Feature
I would like to be able to have a way to say
foo: datetime
but know thatfoo
either:This could be resolved by an
Intersection[]
type as described in #2087 and a protocol that defines thetzinfo
attribute appropriately.Pitch
My database layer can only persist datetimes with a timezone; my timezone parsing code has portions where I want to make sure I haven't associated a timezone yet because to do so and then convert while replacing it would be an error. Really, naive datetimes and aware datetimes are subtly, but profoundly different types of objects, and their arithmetic represents different types of deltas.
Also, pytz requires different sorts of logic on arithmetic and comparison (i.e. possibly
normalize
is required) than other zoneinfo objects.The text was updated successfully, but these errors were encountered: