Also, the import * form of import is pretty strongly discouraged in the first place, precisely because it introduces more names than you need. So adding syntax to patch over this problem seems like the wrong solution - instead, we should be encouraging people to explicitly import just the names they are using.
It’s like going to a restaurant and telling the waiter that you want everything on the menu except for those items you don’t want, which you then proceed to list…
Now the waiter has to ask how you want you beef, what sauce you’d like, what kind of buns. Sure some things can be eaten together, but you don’t order a burger with everything and get multiple buns.
I however would like to order a burger with normal buns, cheese, salad, tomatoes, chicken instead of beef, burger sauce, and that’s it.
When are you going to run into a case, where you really need to import hundreds of things. I could imagine that happening with stubs, which can easily be generated, and with other API abstractions, which are usually generated too.
Maybe you can use an import_except function (name is misleading I know…), so that you can do
import typing
import_except(typing, "NoReturn", "Union")
x: Any # Works
That would mean that it is hard to know where stuff came from though, and linters /IDEs/ type-checkers will likely have a very hard time.
In practice, it doesn’t make much difference because modules are totally loaded in memory even when you try to get only one item, except for the fact that it adds the item to global scope and then deletes it.
Can you give us a specific real-world example of an actual module from which you want to import all but a few names, in what context, and why importing those excluded names would cause problems for the rest of your code?
I don’t believe you have actual code that needs all names from the os module except system, path and mkdir. It just makes no sense.
hypothetically, it might be useful if you’re used to importing * and you want to do something like
from math import *
from numpy import * except (floor, ceil)
here you can’t use the
from math import *
from numpy import *
del floor, ceil
trick, you’d have to write
from math import *
from numpy import *
from math import floor, ceil
I also hope I never encounter something like the above in the wild, and am very happy people have settled on import numpy as np to get all of numpy in scope