-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
gh-121604: Raise DeprecationWarnings in importlib #121765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03bbac0
Add deprecation warnings
rashansmith 820ac21
📜🤖 Added by blurb_it.
blurb-it[bot] d6c0645
Update Lib/importlib/_abc.py
rashansmith cf8264d
Apply suggestions from code review
rashansmith cfbe079
Update machinery deprecation warning
rashansmith 6294458
Apply suggestions from code review
rashansmith f2458cb
Merge branch 'main' into main
brettcannon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
"""The machinery of importlib: finders, loaders, hooks, etc.""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd keep the newline |
||
import warnings | ||
from ._bootstrap import ModuleSpec | ||
from ._bootstrap import BuiltinImporter | ||
from ._bootstrap import FrozenImporter | ||
|
@@ -27,3 +28,15 @@ def all_suffixes(): | |
'NamespaceLoader', 'OPTIMIZED_BYTECODE_SUFFIXES', 'PathFinder', | ||
'SOURCE_SUFFIXES', 'SourceFileLoader', 'SourcelessFileLoader', | ||
'WindowsRegistryFinder', 'all_suffixes'] | ||
|
||
|
||
def __getattr__(name): | ||
if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES', 'WindowsRegistryFinder'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES'):
...
elif name == 'WindowsRegistryFinder':
... |
||
if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES'): | ||
warnings.warn(f"The '{name}' module is deprecated.", | ||
DeprecationWarning, stacklevel=2) | ||
return name | ||
else: | ||
warnings.warn(f"The '{name}' class is deprecated.", | ||
DeprecationWarning, stacklevel=2) | ||
return name |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2024-07-14-12-46-02.gh-issue-121754.2Tt2fE.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This PR adds some deprecation warnings to importlib |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure but I think we should raise the deprecation warning when the class is imported, not when you create a new instance with it. Going by the example in the original PEP 562 it'd look (I think) something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, no problem. Thanks for the reference link! Another approach I had was to just put the warning message by itself in the first line under the class name. Would this have a similar affect? I removed it because I then saw the deprecation error when running the
make
command.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem with the other approach is that you'll get the warning whenever you import the module, not just the deprecated class specifically. For example,
import importlib.abc
will produce a warning forInspectLoader
even if you're not actually using it