You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is from a convo with @DanielRosenwasser about this suggestion: microsoft/pyright#7579
He brought up a good point that it seems to be a pretty common mistake to try to access TypedDict attributes using . instead of [""]. For example:
from typing import TypedDict
class Person(TypedDict):
name: str
def func(p: Person):
print(p.name) ## ---> should be p["name"]
It'd be nice if we could surface a diagnostic even when type checking mode is set to off to help users catch this error sooner, as many times it may cause a run time error. He suggested we could surface it as a suggestion diagnostic too, which I think it's a good idea if we believe error would be too intrusive.
We'd probably get bonus points for surfacing a "Fix code spelling" code action to translate from .key to ["key"] :)
The text was updated successfully, but these errors were encountered:
Do we have evidence of this statement, or is this just conjecture? I suspect this mistake is more common for someone coming from TypeScript, but I'm guessing that it's much more rare for Python programmers. Also, if someone is using TypedDict in their code, they are much more likely to have type checking enabled (at least in basic mode). Most Python programs don't use TypedDict and instead just use regular dict classes.
I'd be reluctant to create a separate diagnostic category that is particular to this very specific error case.
It's probably better to focus less on the specific error message being discussed and more broadly on where suggestion diagnostics can be applied.
In TypeScript, we issue suggestion diagnostics in unchecked JavaScript files for several error messages. I briefly called it out in microsoft/pyright#7579.
A couple of example errors that get surfaced as suggestion diagnostics include:
Misspelled local bindings
Misspelled properties/attributes
Unnecessary await (which I believe is actually a hard error in Python?)
For Pylance, the first one already has a quick fix in the language service (though the error message doesn't mention any suggestion). The next two don't have any fixes.
This is from a convo with @DanielRosenwasser about this suggestion: microsoft/pyright#7579
He brought up a good point that it seems to be a pretty common mistake to try to access TypedDict attributes using
.
instead of[""]
. For example:It'd be nice if we could surface a diagnostic even when type checking mode is set to off to help users catch this error sooner, as many times it may cause a run time error. He suggested we could surface it as a suggestion diagnostic too, which I think it's a good idea if we believe error would be too intrusive.
We'd probably get bonus points for surfacing a "Fix code spelling" code action to translate from
.key
to["key"]
:)The text was updated successfully, but these errors were encountered: