Repository navigation
autoreload: Use importlib.util.find_spec instead of pkgutil.get_loader - #3849
Merged
Merged
Conversation
pkgutil.get_loader was removed in Python 3.14, so on 3.14+ the `python -m tornado.autoreload -m module` wrapper raised AttributeError once the module exited, instead of watching the module's file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
chiragjangiir
left a comment
There was a problem hiding this comment.
Code Review: autoreload: Use importlib.util.find_spec instead of pkgutil.get_loader (#3849)
1. Summary & Architecture
Modernizes module reloading in Tornado's autoreload module by replacing the deprecated pkgutil.get_loader() with Python 3.12+ standard importlib.util.find_spec().
2. Implementation & Diff Analysis
- API Migration: In
tornado/autoreload.py, queriesimportlib.util.find_spec(module_name)to locate source files and determine module origins. - Spec Origin Inspection: Extracts
spec.originto check whether the file is a valid filesystem path suitable for stat/mtime modification tracking.
3. Edge Cases & Robustness
- Gracefully handles namespace packages and built-in/frozen modules where
spec.originisNoneor'frozen'. - Eliminates Python 3.12+
DeprecationWarningnoise during development server reloads.
4. Test Coverage Assessment
- Unit tests in
tornado/test/autoreload_test.pyverifying module discovery and reloading behavior under modern CPython runtimes.
5. Recommendation
Timely maintenance eliminating deprecated library calls before upcoming Python removals.
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pkgutil.get_loader()was removed in Python 3.14.python -m tornado.autoreload -m <module>calls it after the module exits, to find the module's file and watch it, so on 3.14+ the wrapper stops withAttributeError: module 'pkgutil' has no attribute 'get_loader'instead of waiting for changes (on 3.12/3.13 it prints a DeprecationWarning). The existing autoreload tests don't reach this line because their apps reload themselves before they exit.This uses
importlib.util.find_spec(), which is whatget_loader()called internally, so the watched file is the same. The new test runs a package with-mand--until-success; it fails on Python 3.15.1 without the change and passes with it.python -m tornado.test.runtestspasses on 3.15.1, andautoreload_testpasses on 3.11 and 3.13.I used Claude Code to write this change.