Remix.run Logo
kokada 2 hours ago

From this example:

    lazy from typing import Iterator

    def stream_events(...) -> Iterator[str]:
        while True:
            yield blocking_get_event(...)

    events = stream_events(...)

    for event in events:
        consume(event)
Do we finally have "lazy imports" in Python? I think I missed this change. Is this also something from Python 3.15 or earlier?
llimllib an hour ago | parent | next [-]

3.15: https://docs.python.org/3.15/whatsnew/3.15.html#whatsnew315-...

javcasas 5 minutes ago | parent [-]

> When an AttributeError on a builtin type has no close match via Levenshtein distance, the error message now checks a static table of common method names from other languages (JavaScript, Java, Ruby, C#) and suggests the Python equivalent

Oh, that is such a nice thing.

karpetrosyan an hour ago | parent | prev | next [-]

Note that you can work around it by implementing `def __getattr__(name: str) -> object:` at the module level on earlier Python versions

saghm 30 minutes ago | parent [-]

Somehow I have no trouble imagining this being used as a rationale to avoid unnecessary "magic" to the language for years

boxed 2 hours ago | parent | prev | next [-]

Yes, 3.15+

rad120 an hour ago | parent | prev [-]

Python is such a weird language. Lazy imports are a bandaid for AI code base monstrosities with 1000 imports (1% of which are probably Shai Hulud now).

And now even type imports are apparently so slow that you have to disable them if unused during the normal untyped execution.

If Instagram or others wants a professional language, they should switch to Go or PHP instead of shoehorning strange features into a language that wasn't built for their use cases.

stingraycharles an hour ago | parent | next [-]

> Python is such a weird language. Lazy imports are a bandaid for AI code base monstrosities with 1000 imports

Just because you don’t like a feature doesn’t mean it’s because of AI and bad code.

sigmoid10 an hour ago | parent [-]

I think this is just a natural consequence of an easy-to-use package system. The exact same story as with node. If you don't want lots of imports, don't make it so damn easy to pile them into projects. I'm frankly surprised we still see so few supply chain attacks, even though they picked up their cadence dramatically.

saghm 35 minutes ago | parent | next [-]

This seems a lot more due to an import running arbitrary code because stuff can happen in the top-level of a module rather than only happening in functions. From what I can tell, it seems pretty common for dynamically typed languages and pretty much entirely absent from statically typed ones, which tend to have a main function that everything else happens inside transitively. I guess this makes it easy if what you're writing is something that runs with no dependencies, but it's a pretty terrible experience as soon as you try to introduce the concept of a library.

kokada 31 minutes ago | parent [-]

> it seems pretty common for dynamically typed languages and pretty much entirely absent from statically typed ones

Counter-example is Go and init() function.

stevesimmons 34 minutes ago | parent | prev [-]

What would your alternative look like?

novov an hour ago | parent | prev | next [-]

Empirically, I have used the current accepted way to do lazy imports (import statement inside a function) before AI coding was even a mainstream thing, for personal code that sometimes needs a heavy import and sometimes doesn’t.

The lazy statement would be an improvement as it allows one to see all the imports at the top where you expect them to be.

afH12 an hour ago | parent [-]

As a now deleted comment pointed out, lazy imports had been requested forever. They were rejected forever and were accepted just when BigCorps wanted them.

Python-dev now is paid to shore up the failed Instagram stack.

brookst an hour ago | parent [-]

I too am outraged that a product would prioritize its biggest users.

saghm 31 minutes ago | parent [-]

Is the biggest user larger than the combined set of individual users who had asked for (or would benefit from) the same thing? I honestly don't know, but I don't think that things are always as simple as you're implying in a world where we have the collective action problem.

brookst 13 minutes ago | parent [-]

If you’re asking some some kind of abstract moral value sense, I have idea.

If you’re asking whether project leads give more weight to a single, tangible, vocal stakeholder than they do to unknown numbers of anonymous and lightly-engaged stakeholders? Yes.

formerly_proven an hour ago | parent | prev | next [-]

On most unix-likes all "imports" via shared libraries (e.g. in C / C++) are lazy by default.

an hour ago | parent | prev [-]
[deleted]
ziml77 an hour ago | parent [-]

But also great for speed. Larger libraries can take a measurable amount of time to import (even if they have no transitive dependencies). If only some of your code paths actually need the large library then it makes sense to import it lazily. Without lazy you have to do it conditionally which can lead to the imports happening in strange places rather than all being listed out at the top of the file.