PEP 810: Explicit lazy imports

I’ve put up a PR to address some of the comments.

I’ve added a mention of this to the rejected ideas section. Adding eager would be confusing unless we were actively working to changing the default behaviour. We aren’t, and certainly as part of this PEP we won’t be.

I’ve removed the assertions about performance.

Yes, it should. The default doesn’t change. This PEP does not anticipate the default will change. PEP 690 was the closest thing to a proposal that might lead to changing the default, and it was already rejected.

Making all imports lazy is a big change. There’s a reason it’s a knob the PEP provides but discourages. It’s a huge breaking change. There are too many modules that just won’t work right, or will work right only in specific circumstances (like something already imported one of its dependencies.) It may be doable in ten or fifteen years, after we’ve shown that lazy modules work great, and we’ve developed new patterns of module behaviour to replace the existing ones that rely on eager imports. Me, I’m not holding my breath.

The closest proposal to that would be PEP 690. It was rejected in part because it made all imports lazy. (It’s mentioned in this PEP.)

That wouldn’t fix the issues with the existing context-manager based solutions. A hybrid where the syntax this PEP used is based on with would open a whole new world, since with has never been semantically important to the compiled code, which is quite a big lift. I’ve added it to rejected ideas, but if you want to propose a competing PEP with that syntax, feel free :slight_smile:

The names are there, we just need to something extra if they’re of a specific type. Or we could store them somewhere else, but then we would need to do multiple lookups and keep the dicts in sync. And either way, it still requires a dict subclass, which is problematic for all the reasons mentioned in the PEP.

No, the filter intentionally is only called for potentially lazy imports. It’s meant as a safety measure, not an inversion of responsibility. And yes, it is intentional that disabling lazy imports globally would expose import cycles that only work with lazy imports. The advice for import cycles remains pretty much the same, even with lazy imports: refactor the code so you don’t have an unresolvable cycle.

I’ve added it to rejected ideas.

6 Likes