logoalt Hacker News

kokadatoday at 11:54 AM6 repliesview on HN

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?

Replies

kzrdudetoday at 2:17 PM

What benefit does the lazy import have here - if we use the value in a type hint at module scope anyway? Would that require Deferred evaluation of annotations -- which I don't think are enabled by default?

show 2 replies
karpetrosyantoday at 12:51 PM

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

show 2 replies
boxedtoday at 11:59 AM

Yes, 3.15+

alcazartoday at 4:03 PM

[dead]

rad120today at 12:17 PM

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.

show 5 replies