LBeaudoux / iso639

A fast, simple ISO 639 library.
MIT License
32 stars 4 forks source link

Use importlib.resources for accessing files #17

Closed tijmenbaarda closed 10 months ago

tijmenbaarda commented 10 months ago

Use importlib.resources.files instead of resource_filename from setuptools for accessing resource files, to assure compatibility with Python 3.12. In case of Python 3.8, the backport package importlib_resources is used. Closes #16

nass600 commented 10 months ago

Thanks for the PR and the library 👏 . Any chance to get it merged 🙏 ?

LBeaudoux commented 10 months ago

Any chance to get it merged 🙏 ?

@tijmenbaarda, thanks for your PR. Tests are successful for Python 3.8+.

I plan to take some time this weekend to determine if I can also cover Python 3.6 and Python 3.7. As iso639-lang is downloaded 20,000 times a month from PyPI, I also have to make sure I don't break anything during the packaging process. I'll try to release the new version this weekend. Sorry to keep you waiting.

nass600 commented 10 months ago

Thank yo @LBeaudoux 👏

LBeaudoux commented 10 months ago

Thank you @tijmenbaarda for reporting the compatibility issue with Python 3.12 and providing a solution. Your help was much appreciated.

tijmenbaarda commented 10 months ago

Thanks a lot @LBeaudoux for your quick response! I will happily continue using iso639 in my projects :)

tijmenbaarda commented 10 months ago

@LBeaudoux: now that I have imported the upgraded package in my project I see that Python 3.12 raises the following DeprecationWarning, which I didn't see before unfortunately:

../.env/lib/python3.12/site-packages/iso639/datafile.py:6
  /home/tijmen/p/lidia/lidia-browser/.env/lib/python3.12/site-packages/iso639/datafile.py:6: DeprecationWarning: 'importlib.abc.Traversable' is deprecated and slated for removal in Python 3.14
    from importlib.abc import Traversable

This is because Traversable has moved from importlib.abc to importlib.resources.abc in Python 3.11. This won't cause trouble until Python 3.14, but you may want to remove the return type hint of the get_file function (and the Traversable import) so that this warning won't show anymore and so that the code will continue to work from Python 3.14 onwards... Unfortunately changing the import to the new location would break compatibility with Python <3.11.

LBeaudoux commented 10 months ago

@tijmenbaarda thanks for reporting this DeprecationWarning, Maybe we could keep the type hint and be Python 3.14+ ready by using the following imports:

try:
    from importlib.resources import files
except ImportError:
    # Compatibility for Python <3.9
    from importlib_resources import files
try:
    from importlib.resources.abc import Traversable
except ImportError:
    try:
        # Compatibility for Python 3.9 and 3.10
        from importlib.abc import Traversable
    except ImportError:
        # Compatibility for Python <3.9
        from importlib_resources.abc import Traversable
tijmenbaarda commented 10 months ago

Sure! Although maybe that is overkill for a type hint for a private function, but that is up to you :)

LBeaudoux commented 10 months ago

maybe that is overkill for a type hint for a private function

You're right. I just removed the type hint and released v2.2.1.

tijmenbaarda commented 10 months ago

Great! The warning does not appear anymore.