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.9 PytzUsageWarning #1013

Closed
kkoehncke opened this issue Oct 28, 2021 · 11 comments
Closed

Python 3.9 PytzUsageWarning #1013

kkoehncke opened this issue Oct 28, 2021 · 11 comments
Labels

Comments

@kkoehncke
Copy link

When upgrading to Python 3.9, getting the following warning:

Python 3.9.5 (default, Jun  4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateparser
>>> dateparser.parse("2021-08-02")
/opt/conda/lib/python3.9/site-packages/dateparser/date_parser.py:35: PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
  date_obj = stz.localize(date_obj)
datetime.datetime(2021, 8, 2, 0, 0)

Is there an anticipated fix for this or a way to suppress this?

Thanks!

@viseshrp
Copy link

viseshrp commented Nov 1, 2021

is there a way to silence these warnings temporarily?

@FinnWoelm
Copy link

Hi @viseshrp,

I was able to temporarily silence these warnings with:

import warnings
import dateparser

# Ignore dateparser warnings regarding pytz
warnings.filterwarnings(
    "ignore",
    message="The localize method is no longer necessary, as this time zone supports the fold attribute",
)

# Your code ...

SO thread on filtering Python warnings: https://stackoverflow.com/a/56142903/6451879


For pytest, you can modify pytest.ini like this:

[pytest]
filterwarnings = 
    ignore:The localize method is no longer necessary, as this time zone supports the fold attribute

@viseshrp
Copy link

viseshrp commented Nov 1, 2021

Hi @viseshrp,

I was able to temporarily silence these warnings with:

import warnings
import dateparser

# Ignore dateparser warnings regarding pytz
warnings.filterwarnings(
    "ignore",
    message="The localize method is no longer necessary, as this time zone supports the fold attribute",
)

# Your code ...

SO thread on filtering Python warnings: https://stackoverflow.com/a/56142903/6451879

For pytest, you can modify pytest.ini like this:

[pytest]
filterwarnings = 
    ignore:The localize method is no longer necessary, as this time zone supports the fold attribute

Thanks!

@ElijahSink
Copy link

For some reason the filterwarnings configuration mentioned above didn't work for me.
Here's what I used:

# pyproject.toml
[tool.pytest.ini_options]
filterwarnings = ["ignore::pytz_deprecation_shim.PytzUsageWarning"]

Tom-Brouwer added a commit to OpenDaL/ingestion-and-transformation that referenced this issue Dec 17, 2021
The asShape function is being deprecated in favor of shape.

Please note that still warnings remain with respect to the
dateparser package. For this we'll have to wait for an update of
that package. (scrapinghub/dateparser#1013)
@KevinMGranger
Copy link

This occurs in 3.10 as well.

@bsekiewicz
Copy link

bsekiewicz commented Jan 12, 2022

Why we can't replace this

stz = get_localzone()
if hasattr(stz, 'localize'):
date_obj = stz.localize(date_obj)
else:
date_obj = date_obj.replace(tzinfo=stz)

with this

stz = get_localzone()
date_obj = date_obj.replace(tzinfo=stz)

???

Definition of localize is equivalent (???) - https://github.com/pganssle/pytz-deprecation-shim/blob/47bd4bdd9346cafa6c6d66817082ccce099890ad/src/pytz_deprecation_shim/_impl.py#L205

@DavidMStraub
Copy link

Hi,

I suspect the cause of the issue is a change in tzlocal a few months ago (source):

With version 3.0 of tzlocal, tzlocal no longer returned pytz objects, but zoneinfo objects, which has a different API. Since 4.0, it now restored partial compatibility for pytz users through Paul Ganssle’s pytz_deprecation_shim.

A second problem could be that dateparser, according to its readme, aims to be compatible with Python 3.5+, but 3.5 was before PEP 495 was introduced.

Since even Python 3.6 has reached EOL in the meantime, I suggest dropping at least Python 3.5 support and implementing @bsekiewicz's suggestion.

@DavidMStraub
Copy link

I suggest dropping at least Python 3.5 support

I realized tzinfo itself requires 3.6+, so 3.5 support is anyway gone. So just an update of the Readme is needed for this.

@akash-cis
Copy link

I faced the same issue with dateparser. passing a timezone to the parse method solved that error for me.
dateparser.parse(value, settings={'TIMEZONE': 'UTC'})

you can read all about the by this documentation https://pypi.org/project/dateparser/

@dreki
Copy link

dreki commented May 12, 2022

You can also add a timezone in the string you're parsing as well.

E.g. instead of dateparser.parse('2022-04-13’), do dateparser.parse('2022-04-13 Z’)

The-Compiler added a commit to The-Compiler/cloclify that referenced this issue May 13, 2022
@Gallaecio
Copy link
Member

Fixed by #1062

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

No branches or pull requests

10 participants