kokada
a day 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
a day ago
javcasas
a day ago
> 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.
fulafel
20 hours ago
It's unrelated to the lazy keyword. Instead it's another feature related to error messages.
The example:
>> 'hello'.toUpperCase()
Traceback (most recent call last):
...
AttributeError: 'str' object has no attribute 'toUpperCase'. Did you mean '.upper'?estebank
19 hours ago
In the Rust toolchain we've done the same. It just so happens that rustdoc already has introduced annotations for "aliases" so that when someone searches for push and it doesn't exist, append would show up. Having those annotations already meant that bootstrapping the feature to check the aliases during name resolution errors in rustc was almost trivial. I love it when improving one thing improves another indirectly too.
I really appreciate them going out of their way to do this, being quite aware of the hidden complexity in doing it.
brainzap
40 minutes ago
thats cool, I have the problem alot because I switch languages multiple times per day
tuveson
19 hours ago
I’ve often thought it would be funny if instead of an error message for stuff like this, a language could be designed to be “typo-insensitive”. If a method or function call is similar enough to an existing one or a common one from other languages, to just have it silently use that.
aix1
8 hours ago
Funny, yes, but IMO a terrible idea. :)
It would help the writer once, but impose a cost on all future readers for the lifetime of the code.
It's a bit like reading English with bits of German, French and Russian. All of sudden you have to know that Buch, livre and книга all mean the same thing.
Not to mention that there are often subtle differences in meaning between words that on the face of it seem equivalent (in both human and computer languages).
It could be a nice feature for an IDE though, to help someone learn a language.
sirsinsalot
26 minutes ago
Fuzzy function calling. What could go wrong?!
estebank
19 hours ago
VisualBasic did that. I think it is a mistake. But that doesn't mean that the compiler can't detect that and tell you how to fix it instead.
tuveson
18 hours ago
Sure VB ignores case, but what I want is for it to compare each method against a dictionary of similar terms. And maybe calculate the Levenshtein distance between all terms if it’s not found, and just assume it’s the closest one. You could also assume that full-width characters or similar-looking glyphs are equivalent (BASIC was pre-Unicode, so I can forgive them for not including that).
lmm
11 hours ago
> And maybe calculate the Levenshtein distance between all terms if it’s not found, and just assume it’s the closest one.
So when a library adds a new method, it silently changes which method client code calls? That's a bit too magic IMO. I think the best you can do is be case-insensitive and ban methods that differ only in case (or, if you want to extend the idea a bit more radically, ban having things in the namespace within Levenshtein distance x of each other, and then you can autocorrect errors smaller than x/2).
vic20forever
13 hours ago
Say whaaat? VB (v.3 through v.6, at least) wouldn't compile if you misspelled the name of a function or subroutine.
estebank
13 hours ago
VB had case insensitive name resolution.
vic20forever
12 hours ago
Yes, but that was the standard behavior in DOS & Windows world (not including C/C++). We thought that case sensitivity was the broken behavior ;)
I was referring to the parent's statement "If a method or function call is similar enough to an existing one or a common one from other languages, to just have it silently use that." A compiler that substitutes a different function for the one I specified because it "knows what I really want" is horrifying.
orthoxerox
2 hours ago
At the very least Python could quit on `quit` instead of saying that it knows what I want, but won't do it.
nickserv
2 hours ago
If you have a variable named `quit`, you would have a different behavior in running a file vs running in the CLI.
QuesnayJr
18 hours ago
Lisp had a package for that, DWIM, in the late 60s: https://en.wikipedia.org/wiki/DWIM.
pansa2
13 hours ago
`npm isntall`
MarkusQ
19 hours ago
I hope you mean "funny" in the "hilarity ensues" sense.
Because the alternative is a rather sociopathic level of schadenfreude.
tuveson
18 hours ago
Yes, I say “funny” because it would be impractical and weird, definitely not a good idea. It’s already a bad enough that so many popular languages don’t (and can’t) check if a field or method is misspelled at compile time…
sfink
17 hours ago
We already have it. In fact, Python added it with this change! Not intentionally, but in a world of AI, any error message containing a suggestion of what to do to fix it is a directive to the AI to actually do that thing.
Example: to build our system, you run `mach build`. For faster rebuilds, you can do `mach build <subdir>`, but it's unreliable. AI agents love to use it, often get errors that would be fixed by a full-tree build, and will chase their tails endlessly trying to fix things that aren't broken. So someone turned off that capability by default and added a flag `--allow-subdirectory-build` for if you want to use it anyway. So that people would know about it, they added a helpful warning message pointing you to the option[1].
The inevitable (in retrospect) happened: now the AI would try to do a subdirectory build, it would fail, the AI would see the warning message, so it would rerun with the magic flag set.
So now the warning message is suppressed when running under an AI[2][3]. The comment says it all:
# Don't tell agents how to override, because they do override
"The user does not want me to create the Torment Nexus but did not specify why it would be a problem, so I will first create the Torment Nexus in order to understand the danger of creating the Torment Nexus."[1] https://searchfox.org/firefox-main/rev/fc94d7bda17ecb8ac2fa9...
[2] https://bugzilla.mozilla.org/show_bug.cgi?id=2034163
[3] https://searchfox.org/firefox-main/rev/cebc55aab4d2661d1f6c2...
embedding-shape
20 hours ago
Now I'm wishing for a single cross-language library, that I can somehow inject into every compiler/runtime/checker to get this, but with a single source of truth and across a wide range of languages. I hit this damn issue all the time, writing code in one language for another, would truly be a bliss to have that problem solved once and for all.
estebank
19 hours ago
If you had a "canonical datastructure database", you could have very short annotations on every standard library for any language that indexes a function to their canonical name. After that you only need to update the database.
kzrdude
a day ago
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?
js2
21 hours ago
Type annotations are lazily evaluated by moving them behind a special annotations scope as of 3.14:
https://peps.python.org/pep-0649/
https://docs.python.org/3/reference/compound_stmts.html#anno...
With 3.15, using lazy typing imports is more or less an alternative to putting such imports behind an "if TYPE_CHECKING" guard.
kzrdude
21 hours ago
Ah, thanks for the update. My only check before asking was to check if the future feature for annotations had been enabled by default yet. It has then effectively been abandoned instead, I guess.
js2
20 hours ago
Yup, "from __future__ import annotations" will eventually be removed:
> from __future__ import annotations (PEP 563) will continue to exist with its current behavior at least until Python 3.13 reaches its end-of-life. Subsequently, it will be deprecated and eventually removed.
toxik
20 hours ago
So the future behavior is deprecated before it ever became the default?
nyrikki
19 hours ago
It was an abandoned path even before 3.10, it just took longer to implement 649 and 749 than they expected.
But this is a "...will continue to exist with its current behavior at least..." is an important bit there.
From pep-0749:
Sometime after the last release that did not support PEP 649 semantics (expected to be 3.13) reaches its end-of-life, from __future__ import annotations is deprecated. Compiling any code that uses the future import will emit a DeprecationWarning. This will happen no sooner than the first release after Python 3.13 reaches its end-of-life, but the community may decide to wait longer.
It has a good overview of the history.js2
19 hours ago
Correct. Before the "from __future__ import annotations" behavior that converts annotations to strings became the default, they figured out a better mechanism for circular type annotations (making them lazy) that is implicitly backwards compatible and that didn't need to be guarded behind a future statement.
Ironically, the new default behavior (making type annotation evaluation lazy) is not backwards compatible with the "from __future__ import annotations" behavior of converting annotations to strings, so they can't just rip out "from __future__ import annotations" and instead it needs to be deprecated and removed over multiple releases.
Oh, what tangled webs we weave! :-)
karpetrosyan
a day ago
Note that you can work around it by implementing `def __getattr__(name: str) -> object:` at the module level on earlier Python versions
ActorNightly
11 hours ago
Python has had lazy imports from like day one, where you could have an import statement in a function, and the library won't be imported until that function is hit.
boxed
a day ago
Yes, 3.15+