-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix datetime.strftime #6317
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
Fix datetime.strftime #6317
Conversation
`datetime.strftime()` takes `format: str` as a positional argument.
|
This comment has been minimized.
This comment has been minimized.
Same goes for |
This comment has been minimized.
This comment has been minimized.
Unfortunately, it's not that easy. The pure-Python version of |
True. I dived into the source to see where, if any, a C module gets imported. It's towards the end. That implementation is the one that uses Maybe typeshed could check for |
To me, a positional only argument makes sense the most. I would want to get errors for both |
Personally, I think it's a bit of a bug that the parameter has a different name in the C implementation and the Python implementation; I'd be inclined to report it as a bug on bugs.python.org and try to get that resolved. |
Although |
I'd be with you on that, but generally, bugfixes don't happen very fast. (I like to pass positional arguments, makes the code more readable) |
Seems like there already is an open BPO issue about this here |
As mentioned in the other issue, I'm for changing it to |
While I would prefer we change this to a positional only for now, I'm fine with changing it to |
I can make the correction for It looks as though a couple lines disappeared from the master branch? |
Yes, I, er, just deleted |
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, but I'd like to hear @Akuli's opinion.
thereifixedit™ |
I agree with Akuli that this should be positional-only till the fix for https://bugs.python.org/issue41260 is merged, at which point we can branch on sys.version (and yes, I agree that |
This comment has been minimized.
This comment has been minimized.
I still think a positional-only parameter would be better for now, but I'm fine with it either way. |
stdlib/datetime.pyi
Outdated
@@ -55,7 +55,7 @@ class date: | |||
@property | |||
def day(self) -> int: ... | |||
def ctime(self) -> str: ... | |||
def strftime(self, fmt: str) -> str: ... | |||
def strftime(self, format: str) -> str: ... |
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.
def strftime(self, format: str) -> str: ... | |
def strftime(self, __format: str) -> str: ... |
Let's make it positional-only as discussed.
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.
Quoting https://bugs.python.org/msg373421, specifically:
I think in typeshed they can safely change from
fmt
toformat
even today (which would almost certainly be more accurate to end user use cases).
What do you have against this?
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.
Type checking is all about warning users about situations where their code could lead to unexpected errors. Passing a keyword argument to this parameter will currently cause code to raise an error sometimes (but not all the time, or even most of the time), as not all systems can be guaranteed to use the C implementation of datetime
(this is partly why the Python implementation exists alongside the C implementation). If a user passes a positional argument to this parameter, however, then there is no possibility of an error. So, it seems correct that, as the situation currently stands, a type-checker should raise an error if a keyword argument is passed to this parameter, as doing so could in some situations lead to unexpected errors.
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.
It's definitely better than the current situation to have the parameter named format
, but I agree that the best option for now is to have it as a positional-only parameter in the stub.
stdlib/datetime.pyi
Outdated
@@ -118,7 +118,7 @@ class time: | |||
if sys.version_info >= (3, 7): | |||
@classmethod | |||
def fromisoformat(cls: Type[_S], time_string: str) -> _S: ... | |||
def strftime(self, fmt: str) -> str: ... | |||
def strftime(self, format: str) -> str: ... |
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.
def strftime(self, format: str) -> str: ... | |
def strftime(self, __format: str) -> str: ... |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
datetime.strftime()
takesformat: str
as a positional argument.