Skip to content

fromtimestamp does not call __new__ in datetime subclasses #76585

Closed

Description

@pganssle
BPO 32404
Nosy @abalkin, @pganssle
PRs
  • bpo-32403: Faster date and datetime constructors #4993
  • 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:

    assignee = None
    closed_at = <Date 2018-01-16.18:06:34.145>
    created_at = <Date 2017-12-21.21:54:13.191>
    labels = ['3.7', '3.8']
    title = 'fromtimestamp does not call __new__ in datetime subclasses'
    updated_at = <Date 2018-01-16.18:06:34.144>
    user = 'https://github.com/pganssle'

    bugs.python.org fields:

    activity = <Date 2018-01-16.18:06:34.144>
    actor = 'belopolsky'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-01-16.18:06:34.145>
    closer = 'belopolsky'
    components = []
    creation = <Date 2017-12-21.21:54:13.191>
    creator = 'p-ganssle'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32404
    keywords = ['patch']
    message_count = 2.0
    messages = ['308908', '310100']
    nosy_count = 2.0
    nosy_names = ['belopolsky', 'p-ganssle']
    pr_nums = ['4993']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue32404'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    Activity

    1. pganssle commented on Dec 21, 2017

      @pganssle
      MemberAuthor

      In writing some tests for the alternate date constructors as part of my PR for bpo-32403 (https://bugs.python.org/issue32403), I noticed that for datetime, the fromtimestamp bypasses the __new__ call on the subclass:

          from datetime import datetime
      
          args = (2003, 4, 14)
          ts = 1050292800.0           # Equivalent timestamp
          d_ord = 731319              # Equivalent ordinal date
      
      
          class DatetimeSubclass(datetime):
              def __new__(cls, *args, **kwargs):
                  result = datetime.__new__(cls, *args, **kwargs)
                  result.extra = 7
                  return result
      
      
          base_d = DatetimeSubclass(*args)
          assert isinstance(base_d, DatetimeSubclass)         # Passes
          assert base_d.extra == 7                            # Passes
      
          ord_d = DatetimeSubclass.fromordinal(d_ord)
          assert isinstance(ord_d, DatetimeSubclass)          # Passes
          assert ord_d.extra == 7                             # Passes
      
          ts_d = DatetimeSubclass.fromtimestamp(ts)
          assert isinstance(ts_d, DatetimeSubclass)           # Passes
          assert ts_d.extra == 7                              # Fails

      Replacing datetime with date in the above code we don't get a failure, but with datetime, it fails with:

      AttributeError: 'DatetimeSubclass' object has no attribute 'extra'
      

      Regardless of the status of 32403, I think this should be fixed (though I can try to fix them both at the same time).

    2. added
      3.7 (EOL)
      3.8 (EOL)
      on Dec 21, 2017
    3. abalkin commented on Jan 16, 2018

      @abalkin
      Member

      New changeset 9f1b7b9 by Alexander Belopolsky (Paul Ganssle) in branch 'master':
      bpo-32403: Faster date and datetime constructors (bpo-4993)
      9f1b7b9

    4. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)3.8 (EOL)

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions