Repository navigation
datetime, date and time: strftime method takes different keyword argument: fmt (pure) or format (C) #85432
Description
Activity
C:
cpython/Modules/_datetimemodule.c
Line 3183 in 8b33961
static char *keywords[] = {"format", NULL}; pure python:
Line 927 in 8b33961
def strftime(self, fmt): this makes it difficult to properly type in mypy:
and calling with
.strftime(fmt=...)or.strftime(format=...)is inconsistent(that said, it should _probably_ be a positional-only argument)
- added3.10 (EOL)end of lifeend of lifeextension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 9, 2020 I think a positional-only argument would be the best option if we could do it, but it would be a backwards-incompatible change and it's probably not worth the hassle.
If anyone is using the keyword argument, they're probably using
format=rather thanfmt=, since it's pretty rare for anyone to use the pure python version of datetime. PyPy uses it, but I think they tend to aim for consistency with the C API of CPython, and it seems like they already patch s/fmt/format/ themselves: https://foss.heptapod.net/pypy/pypy/-/blob/branch/default/lib_pypy/datetime.py#L781If anyone wants to make a PR I think we can fix this for 3.10, though unfortunately because it is an API change it can't be backported. I think in typeshed they can safely change from
fmttoformateven today (which would almost certainly be more accurate to end user use cases).awesome, I'm going to work through this with someone in my discord as a demo / mentorship opportunity -- hope that's ok!
Updating this issue to cover the problem in date, time and datetime.
- changed the title
[-]datetime: strftime method takes different keyword argument: fmt (pure) or format (C)[/-][+]datetime, date and time: strftime method takes different keyword argument: fmt (pure) or format (C)[/+]on Nov 16, 2021 - changed the title
[-]datetime: strftime method takes different keyword argument: fmt (pure) or format (C)[/-][+]datetime, date and time: strftime method takes different keyword argument: fmt (pure) or format (C)[/+]on Nov 16, 2021 Ref python/typeshed#6317 for a discussion about this in typeshed.
In addition to
date.strftimeandtime.strftime, there is also a discrepancy indatetime.fromtimestamp. In the C implementation, the first parameter is called "timestamp"; in the pure-Python implementation, the first parameter is called "t".- added a commit that references this issue
on Dec 22, 2022 - added a commit that references this issue
on Dec 28, 2022
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
datetime.time.strftime,datetime.datetime.fromtimestamp#99993