Skip to content
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

fromtimestamp does not call __new__ in datetime subclasses #76585

Closed
pganssle opened this issue Dec 21, 2017 · 2 comments
Closed

fromtimestamp does not call __new__ in datetime subclasses #76585

pganssle opened this issue Dec 21, 2017 · 2 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes

Comments

@pganssle
Copy link
Member

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']

    @pganssle
    Copy link
    Member Author

    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).

    @pganssle pganssle added 3.7 (EOL) end of life 3.8 only security fixes labels Dec 21, 2017
    @abalkin
    Copy link
    Member

    abalkin commented Jan 16, 2018

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

    @abalkin abalkin closed this as completed Jan 16, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants