basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

a way to configure or remember the preferred order of import suggestions

Open DetachHead opened this issue 1 year ago • 4 comments

there are plenty of symbols in the ast module with the same name as symbols from typing. ive wanted to avoid special-casing solutions for annoyances like this with import suggestions, but this particular situation happens often enough that it's starting to really get on my nerves, so i want to specifically handle this case.

image

DetachHead avatar Dec 02 '24 06:12 DetachHead

There are also some other modules provide names that are not suitable in many cases such as imp, re.{A,I,L,M,X,S,U}, ctypes.Union and so on.

One idea I can provide is to provide a configuration to specify priorities to control the order. Here is a scratch example:

"importSuggestions.namePriorities": {
    "ast": {
        "*": -1
    },
    "imp": -1,
    "re": {
        "A": -1,
        "I": -1
    }
}

If possible, completions in vscode or something else would also benefit from this.

NCBM avatar Dec 02 '24 15:12 NCBM

looking into this further, it seems that the affected symbols are TypeVar, ParamSpec and TypeVarTuple but these are only present in the ast module when targeting python >=3.12, in which case you should be using the new syntax instead anyway. there's also ast.FunctionType which conflicts with types.FunctionType but i think that type is rarely used so i don't think it's as much of a concern.

DetachHead avatar Dec 03 '24 11:12 DetachHead

I have a very similar annoyance that import suggestions appear in an inconvenient order. e.g. I would always like pathlib.Path to be the first suggestion.

LSP autocompletion menu

Of course this depends on the use-case and is highly individual. Someone else might prefer anyio.Path. Overall I agree with @NCBM's idea to customize the priority to the user's preference.

disrupted avatar Feb 13 '25 09:02 disrupted

An idea from #1255 that I like:

Alternatively or additionally, basedpyright could remember the most recently chosen module for a given unqualified symbol, and suggest that option first.

DetachHead avatar Apr 25 '25 23:04 DetachHead

An idea from #1225 that I like:

I just noticed a typo in the above. May I suggest: s/1225/1255/g?

finite-state-machine avatar Aug 12 '25 19:08 finite-state-machine

Fixed, thanks

DetachHead avatar Aug 12 '25 23:08 DetachHead