Erotemic / ubelt

A Python utility library with a stdlib like feel and extra batteries. Paths, Progress, Dicts, Downloads, Caching, Hashing: ubelt makes it easy!
Apache License 2.0
719 stars 43 forks source link

import_module_from_name:0 test failure with Python 3.13.0b1 #157

Closed arkamar closed 1 month ago

arkamar commented 2 months ago

Describe the bug I tried to run tests with Python 3.13.0b1 and ubelt/util_import.py::import_module_from_name:0 test fails:

python3.13 -m pytest -vv -ra -l -Wdefault -Werror::pytest.PytestUnhandledCoroutineWarning --color=yes -o console_output_style=count -o tmp_path_retention_count=0 -o tmp_path_retention_policy=failed -p no:cov -p no:flake8 -p no:flakes -p no
:pylint -p no:markdown -p no:sugar -p no:xvfb -p no:pytest-describe -p no:plus -p no:tavern -p no:salt-factories --deselect tests/test_editable_modules.py::test_import_of_editable_install --deselect ubelt/util_path.py::userhome:0
================================================= test session starts =================================================
platform linux -- Python 3.13.0b1, pytest-8.2.2, pluggy-1.5.0 -- /var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6-python3_13/install/usr/bin/python3.13
cachedir: .pytest_cache
rootdir: /var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6
configfile: pyproject.toml
plugins: xdoctest-1.1.5
collecting ... collected 591 items / 2 deselected / 589 selected

[snip]

ubelt/util_import.py::import_module_from_name:0 FAILED                                                       [430/589]

[snip]

====================================================== FAILURES =======================================================
________________________________________ [xdoctest] import_module_from_name:0 _________________________________________
* REASON: ModuleNotFoundError
DOCTEST DEBUG INFO
  XDoc "/var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6/ubelt/util_import.py::import_module_from_name:0", line 10 <- wrt doctest
  File "/var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6/ubelt/util_import.py", line 335, <- wrt source file
DOCTEST PART BREAKDOWN
Failed Part:
     1 >>> # test with modules that won't be imported in normal circumstances
     2 >>> # todo write a test where we guarantee this
     3 >>> import ubelt as ub
     4 >>> import sys
     5 >>> modname_list = [
     6 >>>     'pickletools',
     7 >>>     'lib2to3.fixes.fix_apply',
     8 >>> ]
     9 >>> #assert not any(m in sys.modules for m in modname_list)
    10 >>> modules = [ub.import_module_from_name(modname) for modname in modname_list]
    11 >>> assert [m.__name__ for m in modules] == modname_list
    12 >>> assert all(m in sys.modules for m in modname_list)
DOCTEST TRACEBACK
Traceback (most recent call last):

  File "/usr/lib/python3.13/site-packages/xdoctest/doctest_example.py", line 849, in run
    exec(code, test_globals)
    ~~~~^^^^^^^^^^^^^^^^^^^^

  File "<doctest:/var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6/ubelt/util_import.py::import_module_from_name:0>", line rel: 10, abs: 335, in <module>
    >>> modules = [ub.import_module_from_name(modname) for modname in modname_list]

  File "/var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6/ubelt/util_import.py", line 345, in import_module_from_name
    module = importlib.import_module(modname)

  File "/usr/lib/python3.13/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import

  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load

  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed

  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import

  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load

  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed

  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import

  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load

  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'lib2to3'

DOCTEST REPRODUCTION
CommandLine:
    pytest /var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6/ubelt/util_import.py::import_module_from_name:0
/var/tmp/portage/dev-python/ubelt-1.3.6/work/ubelt-1.3.6/ubelt/util_import.py:335: ModuleNotFoundError
------------------------------------------------ Captured stdout call -------------------------------------------------
[snip]
=============================================== short test summary info ===============================================
SKIPPED [1] tests/test_download.py:256: This takes a long time to timeout and I dont understand why
SKIPPED [1] tests/test_futures.py:44: long test, demos that timeout does not work with SerialExecutor
SKIPPED [1] tests/test_hash.py:444: blake3 is not available
SKIPPED [1] tests/test_hash.py:472: FIXME THIS ISSUE IS NOT RESOLVE YET.
SKIPPED [20] ../../../../../../../usr/lib/python3.13/site-packages/xdoctest/doctest_example.py:964: Skipped
FAILED ubelt/util_import.py::import_module_from_name:0
======================== 1 failed, 564 passed, 24 skipped, 2 deselected, 17 warnings in 6.55s =========================

The reason is that the lib2to3 was removed in Python 3.13, see removed modules item in https://docs.python.org/3.13/whatsnew/3.13.html#summary-release-highlights.

Desktop (please complete the following information):

Erotemic commented 2 months ago

Thanks for catching this. I'll replace lib2to3.fixes.fix_apply with email.mime.text (unless you have a better suggestion?)

I'll also add 3.13.0-beta.2 to the test matrix.

arkamar commented 2 months ago

I don't have better suggestion, email.mime.text sounds good to me.

Erotemic commented 1 month ago

This should be fixed. Thank you for the report!

arkamar commented 1 month ago

Changes in dev/1.3.7 branch work for me in Python 3.13.0b4. Thanks!