You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Differing exception between builtins and importlib when importing beyond top-level package #81625
We already raise that just above this for the "no dots at all" case, so also raising it for the "not enough dots" case makes more sense than leaving them different.
New changeset c5fa449 by Miss Islington (bot) (Ngalim Siregar) in branch 'master': bpo-37444: Update differing exception between builtins and importlib (GH-14869) c5fa449
While raising ImportError certainly makes much more sense in this case, this change is backwards incompatible and there was no DeprecationWarning. I don't consider that fair.
As a certain compromise, we could maybe raise a custom ImportError+ValueError offspring (e.g. BeyondToplevelImportError)?
If feeling fancy, we could rise a DeprecationWarning when issublcass(BeyondToplevelImportError, ValueError) is used?
There's no deprecation warning because how do you warn about that? You raise DeprecationWarning about an exception you have yet to raise?
And I personally think calling this unfair is a bit harsh.
hroncok commented on Jan 21, 2020
hroncokmannequin
Mannequin
More actions
I know that raising a DeprecationWarning here is most likely not possible or too black-magical to do. My intention was not to be harsh, sorry about that. I just wanted to point out that a backwards incompatible behavior like this without a (possible) deprecation warning might not counterweigh the gain of raising the logical exception type. As a compromise, I proposed to raise a hybrid that inherits from both.
The problem with the hybrid exception is that importlib would then still raise a different exception from import itself, so the discrepancy would persist. And if you updated import you could then break people in another way where they were catching ValueError and ImportError separately for different cases and now they would potentially catch the wrong exception for that ValueError case.
Basically changing what exceptions get raised sucks as someone is almost inevitably broken. But since import and importlib strive to mirror each other as much as possible and be drop-in replacements, the consistency for the next 30 years of Python's life is more important I think than the case of:
Someone using importlib.import_module()
Who is specifically worried about relative imports going too far and no other import-related import and thus only catching ValueError and not ImportError
And are supporting older versions of Python
And will have a difficult time updating their except clause to now also capture ImportError
Activity
brettcannon commentedon Jun 28, 2019
builtins.__import__() raises ValueError (as it did in Python 2.7) while importlib.__import__() raises ImportError (which makes more sense to me).
Found by Ben Lewis.
brettcannon commentedon Jun 28, 2019
Importllib code raising the exception is
cpython/Lib/importlib/_bootstrap.py
Lines 874 to 876 in f9f8e3c
brettcannon commentedon Jun 28, 2019
I think this should be an ImportError and so that means builtins.__import__() is wrong. Anyone want to argue for the other side?
brettcannon commentedon Jun 28, 2019
import.c code raising the exception:
cpython/Python/import.c
Lines 1675 to 1677 in f9f8e3c
ncoghlan commentedon Jul 1, 2019
ImportError sounds right to me.
We already raise that just above this for the "no dots at all" case, so also raising it for the "not enough dots" case makes more sense than leaving them different.
rhettinger commentedon Jul 20, 2019
I also think ImportError is what people would expect when an import fails.
miss-islington commentedon Aug 3, 2019
New changeset c5fa449 by Miss Islington (bot) (Ngalim Siregar) in branch 'master':
bpo-37444: Update differing exception between builtins and importlib (GH-14869)
c5fa449
hroncok commentedon Jan 21, 2020
While raising ImportError certainly makes much more sense in this case, this change is backwards incompatible and there was no DeprecationWarning. I don't consider that fair.
As a certain compromise, we could maybe raise a custom ImportError+ValueError offspring (e.g. BeyondToplevelImportError)?
If feeling fancy, we could rise a DeprecationWarning when issublcass(BeyondToplevelImportError, ValueError) is used?
brettcannon commentedon Jan 21, 2020
There's no deprecation warning because how do you warn about that? You raise DeprecationWarning about an exception you have yet to raise?
And I personally think calling this unfair is a bit harsh.
hroncok commentedon Jan 21, 2020
I know that raising a DeprecationWarning here is most likely not possible or too black-magical to do. My intention was not to be harsh, sorry about that. I just wanted to point out that a backwards incompatible behavior like this without a (possible) deprecation warning might not counterweigh the gain of raising the logical exception type. As a compromise, I proposed to raise a hybrid that inherits from both.
brettcannon commentedon Jan 22, 2020
The problem with the hybrid exception is that importlib would then still raise a different exception from import itself, so the discrepancy would persist. And if you updated import you could then break people in another way where they were catching ValueError and ImportError separately for different cases and now they would potentially catch the wrong exception for that ValueError case.
Basically changing what exceptions get raised sucks as someone is almost inevitably broken. But since import and importlib strive to mirror each other as much as possible and be drop-in replacements, the consistency for the next 30 years of Python's life is more important I think than the case of:
exceptclause to now also capture ImportErrorhroncok commentedon Jan 22, 2020
For the record, looking at https://docs.python.org/3.9/whatsnew/3.9.html I had an impression that this also affects regular imports, as described in https://bugzilla.redhat.com/show_bug.cgi?id=1791769#c2 -- if that's not that case, I'm sorry.
Anyway, I get your point of "next 30 years of Python".
brettcannon commentedon Jan 23, 2020
Sorry, you're right; I forgot importlib got it "right" while import got it "wrong". :)