Skip to content
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

[ENH] performance optimization for _check_soft_dependencies, speed up test collection time #6355

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

fkiraly
Copy link
Collaborator

@fkiraly fkiraly commented Apr 28, 2024

This PR optimizes the runtime of _check_soft_dependencies, speeding up test collection time, towards #6344.

The speed-up comes from using importlib find_spec instead of actually attempting an import and catching the exception.

The "suppress std out" argument becomes obsolete, as no output is generated when not importing the module.

This PR also fixes an unreported bug in the sktime code base, where the package (PEP 440) scikit-learn was referred to as sklearn.

@fkiraly fkiraly added module:tests test framework functionality - only framework, excl specific tests enhancement Adding new functionality labels Apr 28, 2024
@fkiraly
Copy link
Collaborator Author

fkiraly commented May 21, 2024

@benHeid, @yarnabrina, I have no reasonable explanation on the discrepancies between package versions found.

The discrepancy seems to be between version("packagename") (with version from packaging) and from packagename import __version__ - why and how would that differ?

@yarnabrina
Copy link
Collaborator

yarnabrina commented May 21, 2024

@benHeid, @yarnabrina, I have no reasonable explanation on the discrepancies between package versions found.

The discrepancy seems to be between version("packagename") (with version from packaging) and from packagename import __version__ - why and how would that differ?

I think I can explain a situation where this will happen. Whether this is happening here or not that we need to check.

Suppose in pyproject.toml someone mentioned version="1.2.3". And in the top level __init__.py one mentions __version__="4.5.6". That will lead to discrepancy. There is no verification of the same during packaging. It is perfectly acceptable if a package do not even define __version__, it's just a convention.

image

@yarnabrina
Copy link
Collaborator

yarnabrina commented May 21, 2024

Another question, as I probably misunderstood you:

>>> from packaging import version
>>> version("numpy")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

Are you doing something like this? Why is it failing for me but working for you?

@fkiraly
Copy link
Collaborator Author

fkiraly commented May 22, 2024

Ah, sorry, I meant from importlib.metadata import version

for key in DEFAULT_DEPS_TO_SHOW:
key_is_available = _check_soft_dependencies(key, severity="none")
key_is_available = _check_soft_dependencies(
key, severity="none", package_import_alias=PKG_IMPORT_ALIAS
Copy link
Collaborator

Choose a reason for hiding this comment

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

@fkiraly here key is sklearn, as that's what mentioned here:

However, _check_soft_dependencies expect scikit-learn as the input, not sklearn.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I think that should be ok, because _get_deps_info uses the import path, not the environment specs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But this is causing key_is_available to be False in case of scikit-learn, do we want that? Is it not expected to be True for installed packages?

What is your expected value for this?

_check_soft_dependencies("scikit-learn", severity="none", package_import_alias={'scikit-learn':"sklearn"})

On main, it is True, on this branch it is False.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh, I see! That's the mistake.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

attempted fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Adding new functionality module:tests test framework functionality - only framework, excl specific tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants