Skip to content

Pytz warnings when using khal #1092

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

Closed
rodriguegaspard opened this issue Oct 21, 2021 · 25 comments · Fixed by #1126
Closed

Pytz warnings when using khal #1092

rodriguegaspard opened this issue Oct 21, 2021 · 25 comments · Fixed by #1126

Comments

@rodriguegaspard
Copy link

rodriguegaspard commented Oct 21, 2021

Hello, I did a full system upgrade yesterday, and I now have those warnings when I'm trying to display events or calendars (khal at, khal list, khal calendar)

/usr/lib/python3.9/site-packages/khal/khalendar/khalendar.py:153: 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
  localized_events = self.get_localized(localize(start), localize(end))
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:541: 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
  start_local_datetime = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:543: 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
  end_local_datetime = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:546: 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
  day_start = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:549: 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
  day_end = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/khalendar.py:162: 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
  self._backend.get_localized_calendars(localize(start), localize(end)),

Here's the link mentioned in the warning log for convenience : https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html

@kflak
Copy link

kflak commented Oct 21, 2021

Same thing here! System: Arch Linux latest

@pjkaufman
Copy link

I am hitting the same issue as well. I can see about making a PR for this, but it would need to be validated by a maintainer as I do not have a whole bunch of python experience. This issue happens for me on Pop!Os 21.04.

@pjkaufman
Copy link

pjkaufman commented Oct 22, 2021

@WhyNotHugo , @d7415 I think I have a potential fix for this issue and the tests pass locally, but I don't think I have access to create a PR on this repo. So I will just explain the solution I used.

You need to do the following in both event.py and khalander.py:

  1. from zoneinfo import ZoneInfo with the other imports
  2. create a local variable to store the updated locale and then use that to replace the old locale method use as mentioned in the warnings above (note doing so for all instances breaks lost of unit tests, but doing it for just the ones listed above does not):
 def get_events_on(self, day: dt.date) -> Iterable[Event]:
        """return all events on `day`"""
        ...
        locale = ZoneInfo(str(self._locale['local_timezone'])) # create local variable for new locale
        localized_events = self.get_localized(start.replace(tzinfo=locale), end.replace(tzinfo=locale)) # equivalent to using the new locale
        return itertools.chain(localized_events, floating_events)

@mschilli87
Copy link

@pjkaufman:

I don't think I have access to create a PR on this repo.

If you fork the repo via the button on the top right, you can commit/push to your fork, and open a PR to merge your changes here.

@pjkaufman
Copy link

@mschilli87 , thank you for letting me know. I did not realize the only way to make PRs was through forking the repo. I will try that and see what happens.

@jtorrex
Copy link

jtorrex commented Nov 2, 2021

Hello,

I'm facing some issues quite similar to the issue related here when I try to use khal from Arch:

  localized_events = self.get_localized(localize(start), localize(end))
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:546: 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
  day_start = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:549: 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
  day_end = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/khalendar.py:162: 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
  self._backend.get_localized_calendars(localize(start), localize(end)),
  • ikhal, version 0.10.4
  • Linux archlinux 5.10.76-1-lts 1 SMP Sat, 30 Oct 2021 19:13:46 +0000 x86_64 GNU/Linux

Any suggestion on how to correct it or workaround this? I'm unable to create events from it.

Thanks in advance.

@Terrance
Copy link
Contributor

Terrance commented Nov 3, 2021

The issue for me was tzlocal -- as the comments on that PR request a smaller change to keep the current code working in the meantime, I've opened #1098 with a requirements tweak to keep tzlocal 2.x for now.

@jinnko
Copy link

jinnko commented Nov 10, 2021

Still seeing these issues on ArchLinux when using the khal-git package at version 0.10.5.dev17+g44d5959-1, which seems to be the commit where #1098 was merged.

Additionally when I edit an event via ikhal and enter the date fields there are errors emitted in the form, then when saving the changes ikhal exits with the following exception.

/usr/lib/python3.9/site-packages/khal/khalendar/khalendar.py:153: 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
  localized_events = self.get_localized(localize(start), localize(end))
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:546: 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
  day_start = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:549: 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
  day_end = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/khalendar.py:162: 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
  self._backend.get_localized_calendars(localize(start), localize(end)),
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:541: 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
  start_local_datetime = self._locale['local_timezone'].localize(
/usr/lib/python3.9/site-packages/khal/khalendar/event.py:543: 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
  end_local_datetime = self._locale['local_timezone'].localize(
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/khal/ui/__init__.py", line 1358, in start_pane
    loop.run()
  File "/usr/lib/python3.9/site-packages/urwid/main_loop.py", line 287, in run
    self._run()
  File "/usr/lib/python3.9/site-packages/urwid/main_loop.py", line 385, in _run
    self.event_loop.run()
  File "/usr/lib/python3.9/site-packages/urwid/main_loop.py", line 790, in run
    self._loop()
  File "/usr/lib/python3.9/site-packages/urwid/main_loop.py", line 827, in _loop
    self._watch_files[fd]()
  File "/usr/lib/python3.9/site-packages/urwid/raw_display.py", line 416, in <lambda>
    wrapper = lambda: self.parse_input(
  File "/usr/lib/python3.9/site-packages/urwid/raw_display.py", line 515, in parse_input
    callback(processed, processed_codes)
  File "/usr/lib/python3.9/site-packages/urwid/main_loop.py", line 412, in _update
    self.process_input(keys)
  File "/usr/lib/python3.9/site-packages/urwid/main_loop.py", line 513, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)
  File "/usr/lib/python3.9/site-packages/urwid/wimp.py", line 651, in keypress
    return self._current_widget.keypress(size, key)
  File "/usr/lib/python3.9/site-packages/urwid/container.py", line 1135, in keypress
    return self.body.keypress( (maxcol, remaining), key )
  File "/usr/lib/python3.9/site-packages/urwid/container.py", line 2316, in keypress
    key = w.keypress((mc,) + size[1:], key)
  File "/usr/lib/python3.9/site-packages/khal/ui/editor.py", line 530, in keypress
    return super().keypress(size, key)
  File "/usr/lib/python3.9/site-packages/khal/ui/widgets.py", line 376, in keypress
    key = super().keypress(size, key)
  File "/usr/lib/python3.9/site-packages/urwid/listbox.py", line 968, in keypress
    key = focus_widget.keypress((maxcol,),key)
  File "/usr/lib/python3.9/site-packages/urwid/container.py", line 2316, in keypress
    key = w.keypress((mc,) + size[1:], key)
  File "/usr/lib/python3.9/site-packages/urwid/wimp.py", line 543, in keypress
    self._emit('click')
  File "/usr/lib/python3.9/site-packages/urwid/widget.py", line 461, in _emit
    signals.emit_signal(self, name, self, *args)
  File "/usr/lib/python3.9/site-packages/urwid/signals.py", line 265, in emit
    result |= self._call_callback(callback, user_arg, user_args, args)
  File "/usr/lib/python3.9/site-packages/urwid/signals.py", line 295, in _call_callback
    return bool(callback(*args_to_pass))
  File "/usr/lib/python3.9/site-packages/khal/ui/editor.py", line 510, in save
    self.collection.update(self.event)
  File "/usr/lib/python3.9/site-packages/khal/khalendar/khalendar.py", line 172, in update
    event.etag = self._storages[event.calendar].update(event.href, event, event.etag)
  File "/usr/lib/python3.9/site-packages/khal/khalendar/vdir.py", line 251, in update
    if not isinstance(item.raw, str):
  File "/usr/lib/python3.9/site-packages/khal/khalendar/event.py", line 382, in raw
    timezone = create_timezone(tzinfo, self.start)
  File "/usr/lib/python3.9/site-packages/khal/khalendar/event.py", line 858, in create_timezone
    for one, two in iter(tz._tzinfos.items())
AttributeError: '_PytzShimTimezone' object has no attribute '_tzinfos'

@Terrance
Copy link
Contributor

The requirement tweak in my PR doesn't extend to the AUR package, which just depends on the latest python-tzlocal from the main repository. You'd need to do your own build of tzlocal 2.x using e.g. the previous PKGBUILD for that package.

@nicksellen
Copy link

I experienced the same issue as @jinnko too, with this error on save:

AttributeError: '_PytzShimTimezone' object has no attribute '_tzinfos'

Also on arch. Using the community repo package. I guess this project needs updating to support tzlocal v3+.

Until then though, I downgraded my python-tzlocal package to v2, by fetching it from the archive (https://archive.archlinux.org/packages/p/python-tzlocal/), and installing with pacman -U, or in one go:

sudo pacman -U https://archive.archlinux.org/packages/p/python-tzlocal/python-tzlocal-2.1-3-any.pkg.tar.zst

And then prevent it getting upgraded again in /etc/pacman.conf:

IgnorePkg   = python-tzlocal

(In my case khal was the only thing using python-tzlocal, check with pacman -Qi python-tzlocal, so nothing else might break as a result of the downgrade)

@eoli3n
Copy link

eoli3n commented Nov 16, 2021

Same with downgrade tool which does those two things interactively.
https://aur.archlinux.org/packages/downgrade/

@mstmob
Copy link
Contributor

mstmob commented Dec 14, 2021

@nicksellen Thanks for your (temporary) workaround!
Yesterday there were a bunch of python updates and now khal seems to be broken again:

khal calendar
Traceback (most recent call last):
  File "/usr/bin/khal", line 2, in <module>
    from khal.cli import main_khal
  File "/usr/lib/python3.10/site-packages/khal/cli.py", line 35, in <module>
    from .settings import InvalidSettingsError, get_config
  File "/usr/lib/python3.10/site-packages/khal/settings/__init__.py", line 1, in <module>
    from .settings import get_config  # noqa
  File "/usr/lib/python3.10/site-packages/khal/settings/settings.py", line 34, in <module>
    from .utils import (config_checks, expand_db_path, expand_path,
  File "/usr/lib/python3.10/site-packages/khal/settings/utils.py", line 30, in <module>
    from tzlocal import get_localzone
ModuleNotFoundError: No module named 'tzlocal'

@WhyNotHugo
Copy link
Member

If you're using ArchLinux, you need to re-build any AUR package that's still using python3.9. You may get a list of those using pacman -Qo /usr/lib/python3.9.

@mstmob
Copy link
Contributor

mstmob commented Dec 14, 2021

Yes I'm using ArchLinux. The only package which still uses python3.9 is python-tzlocal because of manual downgrade workaround.

Upgrading python-tzlocal results in the deprecation warning and an unusable ikhal...

@WhyNotHugo
Copy link
Member

A python3.9 package won't work with python 3.10.

If you need an older version of python-tzlocal, you'll need to re-build it using python 3.10. It might make sense to have an aur package for that TBH.

@mstmob
Copy link
Contributor

mstmob commented Dec 14, 2021

Thanks, this makes sense! Actually I don't need python-tzlocal at all, except for khal.
Has anybody found another workaround or do we need to wait for #1093 ?

@nicksellen
Copy link

I just did:

sudo pip install tzlocal==2

It's not a good solution, as it replaces the package installed version. But enough for me for now!

@Terrance
Copy link
Contributor

As I hinted at before, it's easy enough to just build your own copy of the package using the repo's PKGBUILD as a base:

cd $(mktemp -d)
wget 'https://raw.githubusercontent.com/archlinux/svntogit-community/9bf3950/trunk/PKGBUILD' # python-tzlocal 2.1r3
makepkg -s
sudo pacman -U python-tzlocal-2.1-3-any.pkg.tar.zst

This will target whichever version of Python you currently have installed.

@pinpox
Copy link

pinpox commented Jan 26, 2022

Also happening on the NixOS package. Is there any hope this get fixed?

@pjkaufman
Copy link

pjkaufman commented Jan 26, 2022 via email

@lucc
Copy link
Contributor

lucc commented Jan 29, 2022

@pjkaufman can you tell us which dependencies so? Maybe somebody is motivated to jump in and help there.

@pjkaufman
Copy link

pjkaufman commented Jan 29, 2022 via email

@polyzen
Copy link
Contributor

polyzen commented Mar 4, 2022

Manual intervention is no longer needed on Arch Linux, the python-tzlocal package is back to version 2.1. You can go ahead and remove it from your IgnorePkg list.

@WhyNotHugo WhyNotHugo pinned this issue Mar 14, 2022
@arjan-s
Copy link

arjan-s commented Mar 18, 2022

For those like me on NixOS without flakes (which is linked above), here's an overlay to temporarily fix this issue by downgrading the tzlocal used by khal. Just put it in /etc/nixos/configuration.nix or an imported file.

  nixpkgs.overlays = [ (self: super: {
    khal = super.khal.overridePythonAttrs (attrs: rec {
      # fixes https://github.com/pimutils/khal/issues/1092
      tzlocal21 = super.python39Packages.tzlocal.overridePythonAttrs (old: rec {
        pname = "tzlocal";
        version = "2.1";
        propagatedBuildInputs = [ super.python39Packages.pytz ];
        src = super.python39Packages.fetchPypi {
          inherit pname version;
          sha256 = "sha256-ZDyXxSlK7cc3eApJ2d8wiJMhy+EgTqwsLsYTQDWpLkQ=";
        };
        doCheck = false;
        pythonImportsCheck = [ "tzlocal" ];
      });
      propagatedBuildInputs = (builtins.filter (i: i.pname != "tzlocal")
        attrs.propagatedBuildInputs) ++ [ tzlocal21 ];
    });
  })];

geier added a commit that referenced this issue May 4, 2022
Newer versions of tzlocal return tzinfo timezones, not pytz ones. Khal
can't deal with tzinfo timezones for the moment, so we force pytz
timezones when we use tzlocal.

This should be removed when we completely migrate to tzinfo.

Fix #1092
@geier
Copy link
Member

geier commented May 4, 2022

If you can, please check if the workaround in #1126 is working for you. Thanks!

geier added a commit that referenced this issue May 4, 2022
Newer versions of tzlocal return tzinfo timezones, not pytz ones. Khal
can't deal with tzinfo timezones for the moment, so we force pytz
timezones when we use tzlocal.

This should be removed when we completely migrate to tzinfo.

Fix #1092
geier added a commit that referenced this issue May 4, 2022
Newer versions of tzlocal return tzinfo timezones, not pytz ones. Khal
can't deal with tzinfo timezones for the moment, so we force pytz
timezones when we use tzlocal.

This should be removed when we completely migrate to tzinfo.

Fix #1092
geier added a commit that referenced this issue May 5, 2022
Newer versions of tzlocal return tzinfo timezones, not pytz ones. Khal
can't deal with tzinfo timezones for the moment, so we force pytz
timezones when we use tzlocal.

This should be removed when we completely migrate to tzinfo.

Fix #1092
geier added a commit that referenced this issue May 5, 2022
Newer versions of tzlocal return tzinfo timezones, not pytz ones. Khal
can't deal with tzinfo timezones for the moment, so we force pytz
timezones when we use tzlocal.

This should be removed when we completely migrate to tzinfo.

Fix #1092
geier added a commit that referenced this issue May 7, 2022
Newer versions of tzlocal return tzinfo timezones, not pytz ones. Khal
can't deal with tzinfo timezones for the moment, so we force pytz
timezones when we use tzlocal.

This should be removed when we completely migrate to tzinfo.

Fix #1092
@geier geier unpinned this issue Jun 24, 2022
BioBox pushed a commit to BioBox/khal that referenced this issue Sep 29, 2022
Newer versions of tzlocal return tzinfo timezones, not pytz ones. Khal
can't deal with tzinfo timezones for the moment, so we force pytz
timezones when we use tzlocal.

This should be removed when we completely migrate to tzinfo.

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

Successfully merging a pull request may close this issue.