-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
importorskip variant that uses ModuleNotFoundError #11523
Comments
Hey @graingert thanks for the proposal.
I personally like |
i think it would be very nice if a requirement could be passed to allow checking for versions |
The alternative to |
We could also check the exception message matches the module being imported |
Thought the same, I for one would always add Perhaps it is worthwhile to go through a deprecation period, where we issue a warning in case a |
I don't understand the rationale of doing string matching against the exception message here. Why would we need to check that if we can just check the exception type? Why would it be useful to catch an |
Just to issue a warning that the behavior will change in the future, where we will only catch except ModuleNotFoundError:
skip()
except ImportError as e:
if module_name not in str(e):
warning(f"importorskip('{modulename}') caught an ImportError, but the message:\n{e}\n does not contain the module name. This behavior will change in the future to only catch ModuleNotFoundError")
skip() The idea is that we remove the |
I still don't follow. Why would we not want to warn if the message contains the module name? |
To let users know that this might break their test suite in the future, suppose a user has this in their code base: def test_numpy_support() -> None:
pytest.importorskip("numpy")
... Currently, the test is skipped, but not because If we just change We might decide instead to introduce a warning, so users would see a warning like this currently:
Then in a future release we change the code to only catch But now that I write this, perhaps does not make sense to introduce this warning at all? Seems like users would like for this to fail right away? |
But in any case the module actually isn't present, we already will get a |
Oh you are right, we will already be catching Sorry for the noise! We need to decide if we want to generate a warning, or go with the "breaking" change directly (I'm using quotes because one might argue that this is a clear improvement). |
#12220 has been submitted, we need to decide if we will go with a standard improvement, or want to introduce a warning first. |
What's the problem this feature will solve?
sometimes I install a package eg PyTables and it fails to compile properly and fails with an ImportError with an undefined symbol, however I don't see this in CI because I only do
pytest.importorskip("pytables")
Describe the solution you'd like
a new kwarg to importorskip or a new function that catches ModuleNotFoundError instead of ImportError eg
importorskip(exc=ModuleNotFoundError)
orfindmoduleorskip(...)
Alternative Solutions
N/A
Additional context
Originally asked on discord
The text was updated successfully, but these errors were encountered: