-
-
Notifications
You must be signed in to change notification settings - Fork 26.5k
MAINT handle deprecations from importlib.resources
#25157
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
MAINT handle deprecations from importlib.resources
#25157
Conversation
`importlib.resources.open_text` has been deprecated in Python 3.11. See: https://docs.python.org/3/library/importlib.resources.html#importlib.resources.open_text
a510b9a to
e655221
Compare
|
I think the API was introduced in py3.9, so we need an entry in fixes.py for this 🙈 |
|
We should put that in 1.2.1 to spare some warnings to the users |
|
It seems that this is not compatible with Python 3.8: AttributeError: module 'importlib.resources' has no attribute 'files'Ups, did not see the previous comment. Using |
|
We should probably have an |
|
Note that we will have the same issue with |
importlib.resources.open_textimportlib.resources
|
I made a push with the handling of those specific |
| def _open_text(data_module, data_file_name): | ||
| if sys.version_info >= (3, 9): | ||
| return resources.files(data_module).joinpath(data_file_name).open("r") | ||
| else: | ||
| return resources.open_text(data_module, data_file_name) | ||
|
|
||
|
|
||
| def _open_binary(data_module, data_file_name): | ||
| if sys.version_info >= (3, 9): | ||
| return resources.files(data_module).joinpath(data_file_name).open("rb") | ||
| else: | ||
| return resources.open_binary(data_module, data_file_name) | ||
|
|
||
|
|
||
| def _read_text(descr_module, descr_file_name): | ||
| if sys.version_info >= (3, 9): | ||
| return resources.files(descr_module).joinpath(descr_file_name).read_text() | ||
| else: | ||
| return resources.read_text(descr_module, descr_file_name) | ||
|
|
||
|
|
||
| def _path(data_module, data_file_name): | ||
| if sys.version_info >= (3, 9): | ||
| return resources.as_file(resources.files(data_module).joinpath(data_file_name)) | ||
| else: | ||
| return resources.path(data_module, data_file_name) |
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.
Would it make sense to commute functions' definitions with the test based for sys.version_info >= (3, 9)? This would be:
if sys.version_info >= (3, 9):
# Use the newest preferred interfaces chaining of `importlib`.
_open_text = # ...
_open_binary = # ...
_read_text = # ...
_path = # ...
else:
_open_text = resources.read_text
_open_binary = resources.open_binary
_read_text = resources.read_text
_path = resources.pathBut that's probably harder to define.
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.
Since the arguments are not dispatched in the same way, I don't think that we will end-up with more readable knowing that we are going to drop it soonish.
|
It remains |
glemaitre
left a comment
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.
LGTM
ogrisel
left a comment
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.
LGTM as well. Thanks for the fix!
|
Thank you for finishing this, @glemaitre. |
) Co-authored-by: Guillaume Lemaitre <[email protected]>
) Co-authored-by: Guillaume Lemaitre <[email protected]>
) Co-authored-by: Guillaume Lemaitre <[email protected]>
) Co-authored-by: Guillaume Lemaitre <[email protected]>
) Co-authored-by: Guillaume Lemaitre <[email protected]>
Co-authored-by: Guillaume Lemaitre <[email protected]>
Reference Issues/PRs
Fix a problem first observed in skops-dev/skops#246
What does this implement/fix? Explain your changes.
Some utils from
importlib.resourceshas been deprecated in Python 3.11:See: https://docs.python.org/3/library/importlib.resources.html#importlib.resources.open_text
Any other comments?
cc @adrinjalali