streetsidesoftware / cspell

A Spell Checker for Code!
https://cspell.org
MIT License
1.23k stars 92 forks source link

Python - do not spellcheck imports and attribute accesses #1447

Open dmytrokyrychuk opened 3 years ago

dmytrokyrychuk commented 3 years ago

I would like to ignore certain words based on their role in a file. Related to #12.

Case 1

Consider the following Python example:

from os import chdir

chdir(...)

It would be nice to recognize that the word chdir is defined elsewhere, and therefore it should not be flagged as misspelled in the current file.

Case 2

A more complicated example would be this:

import os

os.chdir(...)

It would be even better if there was a mechanism in cspell to recognize that chdir in os.chdir references something that was defined elsewhere, although the wordchdir did not occur on the import line directly. In general, attribute accesses should not be spellchecked, but attributes definitions should be checked. I think it would make sense to let the linter check that no illegal attribute accesses happen.


Question

Is either of these cases possible currently, or do they need considerable changes to cspell?

Jason3S commented 3 years ago

@dmytrokyrychuk,

The desire is to be able to support the scenario you describe. At the moment, it is not possible.

Jason3S commented 3 years ago

Related to #1253

Avasam commented 6 months ago

A simpler request could be: don't spell check anything that can be type-checked. At least that doesn't require redoing a type-checker's work with multi-file analysis.

Flimm commented 2 days ago

I'm interested in this too. I would like cspell to only spell-check identifiers that are defined by the code, not identifiers that are defined elsewhere. If there are spelling mistakes in referenced code, then cspell can find those spelling mistakes when checking the code where those identifiers are defined, and not in all references to those identifiers.

So I want these identifiers foobar to be spell-checked:

foobar = "example"

with open("example.txt") as foobar: ....

for foobar in []: ...

def foobar(): ....

try:
    ...
except Exception as foobar:
    ....

class Foobar:
    def __init__(self):
        self.foobar = 'example'

But I don't want these identifiers foobar to be spell-checked, because they are defined elsewhere:

import foobar

from foobar import foobar

foobar(foobar='example')