Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surface reportAttributeAccessIssue even when type checking mode set to off #5758

Open
luabud opened this issue Apr 12, 2024 · 2 comments
Open
Assignees
Labels
enhancement New feature or request needs decision Do we want this enhancement?

Comments

@luabud
Copy link
Member

luabud commented Apr 12, 2024

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"] :)

@luabud luabud added the enhancement New feature or request label Apr 12, 2024
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Apr 12, 2024
@erictraut
Copy link
Contributor

erictraut commented Apr 12, 2024

it seems to be a pretty common mistake

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.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Apr 13, 2024

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.

image

image

A couple of example errors that get surfaced as suggestion diagnostics include:

  1. Misspelled local bindings
  2. Misspelled properties/attributes
  3. 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.

Related:

@debonte debonte assigned luabud and unassigned StellaHuang95 Apr 18, 2024
@debonte debonte added needs decision Do we want this enhancement? and removed needs repro Issue has not been reproduced yet labels Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs decision Do we want this enhancement?
Projects
None yet
Development

No branches or pull requests

5 participants