Skip to content

Conversation

@jjerphan
Copy link
Member

@jjerphan jjerphan commented Dec 9, 2022

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.resources has 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

@jjerphan jjerphan force-pushed the fix/replace-importlib.resources.open_text branch from a510b9a to e655221 Compare December 9, 2022 16:01
@adrinjalali
Copy link
Member

I think the API was introduced in py3.9, so we need an entry in fixes.py for this 🙈

@jeremiedbb
Copy link
Member

We should put that in 1.2.1 to spare some warnings to the users

@jeremiedbb jeremiedbb added this to the 1.2.1 milestone Dec 9, 2022
@glemaitre
Copy link
Member

glemaitre commented Dec 12, 2022

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 fixes seems good

@glemaitre
Copy link
Member

We should probably have an if ... else ... depending on the python version since your changes are the recommended ones.

@glemaitre
Copy link
Member

Note that we will have the same issue with open_binary, read_text, etc. I am going to try to solve these issues together with the other deprecation breakage in scipy since they are blocking the CI.

@glemaitre glemaitre self-assigned this Dec 12, 2022
@glemaitre glemaitre changed the title FIX Replace deprecated importlib.resources.open_text MAINT handle deprecations from importlib.resources Dec 12, 2022
@glemaitre
Copy link
Member

I made a push with the handling of those specific importlib.resources since we might want only to cherry-pick those changes (and not other SciPy (not sure yet)).

@glemaitre glemaitre removed their assignment Dec 12, 2022
Comment on lines +182 to +207
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)
Copy link
Member Author

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.path

But that's probably harder to define.

Copy link
Member

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.

@glemaitre
Copy link
Member

It remains is_resource. I will make the change after launch.

@glemaitre glemaitre self-requested a review December 12, 2022 13:17
Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@glemaitre glemaitre added Quick Review For PRs that are quick to review Waiting for Second Reviewer First reviewer is done, need a second one! labels Dec 13, 2022
Copy link
Member

@ogrisel ogrisel left a 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!

@ogrisel ogrisel merged commit f2c78fe into scikit-learn:main Dec 13, 2022
@ogrisel ogrisel added To backport PR merged in master that need a backport to a release branch defined based on the milestone. and removed Waiting for Second Reviewer First reviewer is done, need a second one! labels Dec 13, 2022
@jjerphan
Copy link
Member Author

Thank you for finishing this, @glemaitre.

@jjerphan jjerphan deleted the fix/replace-importlib.resources.open_text branch December 13, 2022 11:00
Vincent-Maladiere pushed a commit to Vincent-Maladiere/scikit-learn that referenced this pull request Dec 14, 2022
jjerphan added a commit to jjerphan/scikit-learn that referenced this pull request Jan 3, 2023
jjerphan added a commit to jjerphan/scikit-learn that referenced this pull request Jan 20, 2023
jjerphan added a commit to jjerphan/scikit-learn that referenced this pull request Jan 20, 2023
jjerphan added a commit to jjerphan/scikit-learn that referenced this pull request Jan 23, 2023
adrinjalali pushed a commit that referenced this pull request Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:datasets No Changelog Needed Quick Review For PRs that are quick to review To backport PR merged in master that need a backport to a release branch defined based on the milestone.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants