MAINT handle deprecations from importlib.resources#25157
MAINT handle deprecations from importlib.resources#25157ogrisel merged 3 commits intoscikit-learn:mainfrom
importlib.resources#25157Conversation
`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.
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.
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 |
ogrisel
left a comment
There was a problem hiding this comment.
LGTM as well. Thanks for the fix!
|
Thank you for finishing this, @glemaitre. |
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
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