Skip to content

Conversation

@TTsangSC
Copy link
Collaborator

@TTsangSC TTsangSC commented Oct 25, 2025

Closes #405.

Summary

This fixes edge/corner cases which occur when importlib.resources is superseded during runtime by importlib_resources:

  • If importlib_resources >= 6, < 6.4, line_profiler fails to import because line_profiler.toml_config.ConfigSource.from_default() can no longer use the now-nonexistent importlib.resources.path().
  • If importlib_resources is even older, line_profiler generates a DeprecationWarning on import because importlib_resources.path() was deprecated in favor of importlib_resources.files().

(Such cases may happen e.g. when another package in the user's environment tries to be "smart" and replaces importlib.resources with importlib_resources in both the importlib namespace and sys.modules, via either explicit overwrites or tempering with the import machinery. Granted, this is probably a bad idea and most definitely not our problem, but the very fact that the issue exists shows that there may be a non-negligible amount of code out there which does this.)

A test (tests/test_toml_config.py::test_backported_importlib_resources()) has also been added to check that line_profiler.toml_config operates smoothly for:

  • Vanilla, standard-lib importlib.resources
  • When it's replaced by the following importlib_resources versions:
    • Default (newest on PyPI)
    • >= 6, < 6.4
    • < 6

Caveats

  • This is some additional 150+ LoC for an edge case which is outside of our control and strictly speaking purview, since we don't even explicitly use importlib_resources in the repo.
  • The new test is SLOW (some 10+ seconds) because it has to install line_profiler in a fresh venv where all the shenanigans with importlib_resources can happen.

Feel free to close if it's deemed that this is too much and we can just WONTFIX the original issue.

@TTsangSC TTsangSC changed the title FiX: importlib_resources-related edge cases FIX: importlib_resources-related edge cases Oct 25, 2025
@TTsangSC TTsangSC changed the title FIX: importlib_resources-related edge cases DRAFT: FIX: importlib_resources-related edge cases Oct 25, 2025
@codecov
Copy link

codecov bot commented Oct 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.28%. Comparing base (60e928f) to head (09d4307).
⚠️ Report is 21 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #406      +/-   ##
==========================================
+ Coverage   87.56%   90.28%   +2.71%     
==========================================
  Files          18       20       +2     
  Lines        1641     2080     +439     
  Branches      348      447      +99     
==========================================
+ Hits         1437     1878     +441     
- Misses        149      151       +2     
+ Partials       55       51       -4     
Files with missing lines Coverage Δ
line_profiler/toml_config.py 93.79% <100.00%> (ø)

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1219bc0...09d4307. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TTsangSC TTsangSC changed the title DRAFT: FIX: importlib_resources-related edge cases FIX: importlib_resources-related edge cases Oct 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeprecationWarning: path is deprecated. Use files() instead.

1 participant