Skip to content

idea: allow passing ISO string to all constructors #229

@ariebovenberg

Description

@ariebovenberg

Something that has been bouncing around my mind: what if all constructors accepted an ISO string als an 'alternative' way of constructing an object?

So you could do:

PlainDateTime(2020, 3, 2, 8, 15)
# but also equivalently
PlainDateTime("2020-03-02 08:15")

pros:

  • More readable than comma separated integers
  • Great for specifying literals in tests (speaking from experience)
  • Makes it possible to have a round-trippable repr() (see Allow recreating from repr #222)
  • It's a somewhat popular pattern. Other libraries do it (Temporal, Arrow.get()), and Pydantic has proven the "constructor is parser" pattern is successful (aside from whether this is a good thing)
  • It's faster—really. Python's object overhead is so large that it's actually faster in most cases to parse the values from one string object instead of handling individual integers.
  • Instant now has a constructor, instead of awkwardly not
  • shorter to type (no method name needed)

cons. There aren't many, but they're big ones:

  • There are now two ways of parsing an ISO string. The constructor and parse_common_iso()
  • It makes the constructor more complex from a typing perspective which may confuse users not used to seeing "overloads"
  • "Parsing" and "constructing an object" are conceptually different. Combining the two could lead to muddied semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionDiscussion is needed before proceedingsomething newNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions