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

unsupported operand type(s) for -: 'datetime.datetime' and 'str' #180

Open
bjosun opened this issue Jan 23, 2024 · 5 comments
Open

unsupported operand type(s) for -: 'datetime.datetime' and 'str' #180

bjosun opened this issue Jan 23, 2024 · 5 comments

Comments

@bjosun
Copy link
Contributor

bjosun commented Jan 23, 2024

Problem Description:
Users may encounter the following error when running the get_key_statistics_data() function:

unsupported operand type(s) for -: 'datetime.datetime' and 'str'

Possible Cause:
This error occurs when trying to subtract a string from a datetime.datetime object. The issue is often related to date handling in the code.

I've created a PR that you can try out.

@bjosun
Copy link
Contributor Author

bjosun commented Jan 25, 2024

Got rid of the unsupported operand type(s) for -: 'datetime.datetime' and 'str' but still som crumb error.
Changed the lookup method in cache.py

    def lookup(self, strategy):
        if self.dummy:
            return None
        if self.initialised == -1:
            self.initialise()
        if self.initialised == 0:  # failure
            return None
        try:
            data = _CookieSchema.get(_CookieSchema.strategy == strategy)
            cookie = _pkl.loads(data.cookie_bytes)
            if isinstance(data.fetch_date, _datetime.datetime):
                fetch_date = data.fetch_date
            else:
                # Try parsing the date string using the two supported formats
                try:
                    fetch_date = _datetime.datetime.strptime(data.fetch_date, "%Y-%m-%d %H:%M:%S.%f")
                except ValueError:
                    fetch_date = _datetime.datetime.strptime(data.fetch_date, "%Y-%m-%dT%H:%M:%S.%f")
            return {'cookie': cookie, 'age': _datetime.datetime.now() - fetch_date}
        except _CookieSchema.DoesNotExist:
            return None

@Meborl
Copy link

Meborl commented Jan 25, 2024

I run into the same problem today with yahoofinancials 1.20 installed from pypi.

I patched cache.py as follows, and then it was working as expected.

`
yahoofinancials/cache.py

373c373
return {'cookie': cookie, 'age': _datetime.datetime.now() - data.fetch_date}
return {'cookie': cookie, 'age': (_datetime.datetime.now() - _datetime.datetime.fromisoformat(data.fetch_date)).isoformat()}

`

@bjosun
Copy link
Contributor Author

bjosun commented Jan 26, 2024

Yes your update works as expected and a bit less code :). Thanks for the update.

@JECSand
Copy link
Owner

JECSand commented Feb 2, 2024

@bjosun @Meborl Would one of you be willing to make a PR with this change?

@pavel-nesterov
Copy link

I am also facing this recent issue and thankful for @Meborl and @bjosun for spending time to resolve it. Thank you guys.
I temporarily solved it reverting from 1.20 to version 1.18 (the issue exists in 1.19 also).

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

4 participants