bpo-15873: Implement [date][time].fromisoformat by pganssle · Pull Request #4699 · python/cpython (original) (raw)
@abalkin @vadmium Any thoughts on the question of whether to add a sep
parameter to fromisoformat
(from this now-hidden comment)?
The only reason to do so would be if in a future release we want to support ISO-8601 style strings that are just dates with offsets attached (e.g. 2017-01-01+12:00
). I'm not sure, but I think ISO-8601 makes time zone offsets a property of time, so such dates-with-offsets are probably not actually valid ISO-8601 strings. Because 2017-01-01+12:00
could have been generated from datetime.isoformat(sep='+')
, in its current form it would be impossible to get fromisoformat
to interpret that +12:00
as anything except the time component. If we make the default behavior "strict", then in the future we would be able to support these "out of the box", specifying the separator as +
or -
would cause these style strings to be interpreted as times, and otherwise they would be offsets.
Having given this a bit of thought, I think it's probably a good idea to leave out sep
, and if we want to support this use case in future releases, specifying a separator other than +
or -
would allow these things to be parsed as intended. Having the default value ignore the separator will make it easier to handle what is probably the most frequent use cases, which will be sep=' '
and sep='T'
out of the box without any sort of branching to detect which one you're in, etc.
If we assume that some fraction of people will want to be strict and some people will want to be lenient, no matter which one we choose, the other behavior can easily be emulated. If we choose lenient by default, strict users can do the equivalent with:
def strict_fromisoformat(dtstr, sep='T'): if dtstr[10:11] != sep: raise ValueError('Invalid isoformat string: %s' % dstr) return datetime.fromisoformat(dtstr)
If we choose strict by default, the "lenient by default" option is:
def lenient_fromisoformat(dtstr): # Assuming sep requires a single character be passed, for dates it can be anything return datetime.fromisoformat(dtstr, sep=dtstr[10:11] or 'T')
Anyway, this is a kind of long comment just to say, "I think we should keep the status quo", but I thought I'd at least outline my thinking.