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

python 3.8: test_date*_behaves_like_date* tests fail with AttributeError #52

Open
decathorpe opened this issue May 4, 2019 · 3 comments

Comments

@decathorpe
Copy link

=================================== FAILURES ===================================
_____________________ test_datetimes_behave_like_datetimes _____________________

    def test_datetimes_behave_like_datetimes():
        i = item(datetime(2018, 7, 22, 12, 34, 56))
    
        assert i == datetime(2018, 7, 22, 12, 34, 56)
        assert i.as_string() == "2018-07-22T12:34:56"
    
>       i += timedelta(days=1)

tests/test_items.py:275: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tomlkit/items.py:525: in __add__
    result = super(DateTime, self).__add__(other)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'tomlkit.items.DateTime'>, value = 2018
_ = (7, 23, 12, 34, 56, 0, ...)

    def __new__(cls, value, *_):  # type: (..., datetime, ...) -> datetime
        return datetime.__new__(
            cls,
>           value.year,
            value.month,
            value.day,
            value.hour,
            value.minute,
            value.second,
            value.microsecond,
            tzinfo=value.tzinfo,
        )
E       AttributeError: 'int' object has no attribute 'year'

tomlkit/items.py:498: AttributeError
_________________________ test_dates_behave_like_dates _________________________

    def test_dates_behave_like_dates():
        i = item(date(2018, 7, 22))
    
        assert i == date(2018, 7, 22)
        assert i.as_string() == "2018-07-22"
    
>       i += timedelta(days=1)

tests/test_items.py:295: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tomlkit/items.py:584: in __add__
    result = super(Date, self).__add__(other)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'tomlkit.items.Date'>, value = 2018, _ = (7, 23)

    def __new__(cls, value, *_):  # type: (..., date, ...) -> date
>       return date.__new__(cls, value.year, value.month, value.day)
E       AttributeError: 'int' object has no attribute 'year'

tomlkit/items.py:565: AttributeError
@tirkarthi
Copy link

This failure seems to be due to python/cpython#10902 . Relevant What's new entry text.

Arithmetic operations between subclasses of :class:`datetime.date` or
:class:`datetime.datetime` and :class:`datetime.timedelta` objects now return
an instance of the subclass, rather than the base class. This also affects
the return type of operations whose implementation (directly or indirectly)
uses :class:`datetime.timedelta` arithmetic, such as
:meth:`datetime.datetime.astimezone`.

@decathorpe
Copy link
Author

@tirkarthi yeah that looks like the culprit. thanks for finding this!

So I think it's not only the test that's failing, but some internal logic needs to be adapted to this change?

@tirkarthi
Copy link

Yes, I think so but unfortunately I am not sure how it needs to be changed since I have less experience with the project. A simplified reproducer is as below that will pass in Python 3.7 and will fail like the test in Python 3.8 due to CPython change.

from datetime import datetime, date, timedelta

class Date(date):

    def __new__(cls, value, *_):
        return date.__new__(cls, value.year, value.month, value.day)

    def __add__(self, other):
        result = super(Date, self).__add__(other)
        return Date(result)

    def __sub__(self, other):
        result = super(Date, self).__sub__(other)
        return Date(result)


d = date(2018, 7, 22)
d1 = Date(d)
d1 += timedelta(days=1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants