Skip to content

Creating pytz timezone always returns UnknownTimeZoneError #925

Closed

Description

@lsfischer

Describe the bug
When using TestCase from from pyfakefs.fake_filesystem_unittest import TestCase calling self.setUpPyfakefs on setUp, trying to create a pytz timezone object will always result in ÙnknownTimeZoneError` no matter the timezone string.

In contrast, omitting the call to self.setUpPyfakefs or using TestCase from unittest does not result in this error.

How To Reproduce

from pyfakefs.fake_filesystem_unittest import TestCase
from pytz import timezone


class ExampleTestCase(TestCase):
    def setUp(self):
        self.setUpPyfakefs()

    def test_timezone(self):
        timezone("Europe/Copenhagen")
        # raises pytz.exceptions.UnknownTimeZoneError: 'Europe/Copenhagen'

However not setting up the fake fs (or equivalently, using TestCase from unittest) does not yield this error

from pyfakefs.fake_filesystem_unittest import TestCase
from pytz import timezone


class ExampleTestCase(TestCase):
    def test_timezone(self):
        timezone("Europe/Copenhagen")
        # test passes

Your environment

python -c "import platform; print(platform.platform())"
>>> Linux-6.5.0-14-generic-x86_64-with-glibc2.38

python -c "import sys; print('Python', sys.version)"
>>> Python 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0]

python -c "from pyfakefs import __version__; print('pyfakefs', __version__)"
>>> pyfakefs 5.2.4

python -c "import pytest; print('pytest', pytest.__version__)"
>>> pytest 7.4.3

pip freeze | grep "^pytz"
>>> pytz==2023.3.post1

I'd be interested in helping out on a solution to this if required!
Thanks a lot!

Activity

  1. mrbean-bremen commented on Dec 18, 2023

    @mrbean-bremen
    Member

    The problem here is that pytz uses time zone information data files inside its package to initialize the time zone, and won't find them in the fake fs.
    To fix this, you have to map the timezone files into the fake filesystem, something like this (out of my head):

    from pathlib import Path
    import pytz
    
    class ExampleTestCase(TestCase):
        def setUp(self):
            self.setUpPyfakefs()
            info_dir = Path(pytz.__file__).parent / "zoneinfo"
            self.fs.add_real_directory(info_dir)

    See also this entry in the troubleshooting section of the documentation. It probably would make sense to add this concrete example there.

  2. mrbean-bremen commented on Dec 18, 2023

    @mrbean-bremen
    Member

    I'm converting this into a discussion issue - it is probably helpful for others.

  3. locked and limited conversation to collaborators on Dec 18, 2023
  4. converted this issue into a discussion #926 on Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions