DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.13k stars 20 forks source link

[Docs] improve language server config instructions, including config examples for different editors #581

Closed neel04 closed 1 month ago

neel04 commented 2 months ago

Hi, thank you for this amazing fork. Especially loving the inlay hints with python 😄

The docs mention a couple IDEs, but not helix - how exactly can you set it up in that editor?

For reference, in helix's languages.toml, I believe you can set the analysis options like this:

[language-server.basedpyright]
command = "basedpyright-langserver"
args = ["--stdio"]

[language-server.basedpyright.config]

basedpyright.analysis.typeCheckingMode = "standard"
...
basedpyright.analysis.reportMissingTypeStubs = true
basedpyright.analysis.analyzeUnannotatedFunctions = true
basedpyright.analysis.venvPath = "/Users/neel/miniconda3/envs/react_jax"

However, how/why do we know that the toml key here happened to be basedpyright.analysis and not just specifying the config, like typeCheckingMode directly?

Plus for something like venvPath - it's not working for me, I have to activate the conda environment before I open helix in my shell to get it to work. So clearly, its the wrong key.

If you could document/explain what toml key corresponds to toggling what features, it would be really helpful for helix users setting it up, as they won't have to mess with the internal basedpyright.{json | toml} config.

Thank you so much! let me know if there's some other information you'd need from me.

cc @lemontheme who figured out the config options I'm using above :)

DetachHead commented 2 months ago

However, how/why do we know that the toml key here happened to be basedpyright.analysis and not just specifying the config, like typeCheckingMode directly?

the language server specific options are documented here. not sure why some settings are under analysis but others aren't

Plus for something like venvPath - it's not working for me, I have to activate the conda environment before I open helix in my shell to get it to work. So clearly, its the wrong key.

try python.venvPath instead. for whatever reason i didn't update that one from python.venvPath to basedpyright.venvPath, maybe because of compatibility issues with the vscode python extension, i don't remember

neel04 commented 2 months ago

I put it under the [language-server.basedpyright.config] as

python.venvPath = "/Users/me/miniconda3/envs/react_jax"

still doesn't work.

the language server specific options are documented here. not sure why some settings are under analysis but others aren't

Also, those are the pyright settings - not the basedpyright ones documented here. How do I configure those in my config?

DetachHead commented 2 months ago

the the way config is handled in (based)pyright is kind of a mess (#64). the code that reads the language server config is separate to the code that reads the pyproject.toml/pyrightconfig.json settings, but both those pages in the documentation are relevant to basedpyright, i just haven't updated all references to the name "pyright", which i guess adds to the confusion (#582)

can you see if setting venvPath and venv in pyproject.toml or pyrightconfig.json works? it would look something like this:

# pyproject.toml
[tool.basedpyright]
venvPath = "/Users/me/miniconda3/envs/react_jax"
venv = ".venv"

or

// pyrightconfig.json
{
    "venvPath": "/Users/me/miniconda3/envs/react_jax"
    "venv": ".venv"
}

note that this assumes your react_jax directory isn't the venv itself, but a directory that contains a .venv directory (i have no idea why it's like this and not just one setting...)

i also just noticed that the language server config doesn't have a python.venv option, no idea why that is either (#583)

sorry i'm probably just giving you more questions than answers lol

neel04 commented 2 months ago

No thats the directory to a conda environment, so it doesn't have a .venv there.

And it doesn't make sense to use the toml/json files as I want this setting LSP wide - so it resolves to that conda environment no matter the context of the project/folder.

DetachHead commented 2 months ago

that's probably the issue then. i'm not familiar with conda but it sounds like the only way to get pyright to work with it is to activate the venv first

neel04 commented 2 months ago

Cool, but what about basedpyright's own config settings? like changing reportPrivateImportUsage = false which is not a pyright config 🤔

DetachHead commented 2 months ago

you mean the new rules exclusive to basedpyright like reportPrivateLocalImportUsage? they are configured the same way as the rest of the diagnostic rules: basedpyright.analysis.reportPrivateLocalImportUsage

neel04 commented 2 months ago

right so uh, that doesn't work at all for me...

image

where in my languages.toml for helix,

[language-server.basedpyright.config]
basedpyright.analysis.typeCheckingMode = "standard"
...
basedpyright.reportPrivateUsage = false

I've tried variants of this and none of them work

DetachHead commented 2 months ago

you need the analysis part:

[language-server.basedpyright.config]
basedpyright.analysis.typeCheckingMode = "standard"
...
basedpyright.analysis.reportPrivateUsage = false

if that's still not working, if you happened to add a basedpyright or pyright section in pyproject.toml or a pyrightconfig.json file like i mentioned earlier, try deleting them because you may be running into #513

if that still doesn't help, i might need a self-contained repro since i'm not familiar with conda or helix. the exact steps you took to set everything up if possible

also, which version of basedpyright are you using?

neel04 commented 2 months ago

also, which version of basedpyright are you using?

The latest - I installed it yesterday from homebrew.

if that still doesn't help, i might need a self-contained repro since i'm not familiar with conda or helix. the exact steps you took to set everything up if possible

I think you can just spin up any environment, do:

brew install basedpyright
brew install helix
pip3 install jax ruff

and put this in the languages.toml:

~/.config/helix/languages.toml

```py [[language]] name = "python" scope = "source.python" injection-regex = "python" file-types = ["py","pyi","py3","pyw",".pythonstartup",".pythonrc"] shebangs = ["python"] roots = [".", "pyproject.toml", "pyrightconfig.json"] comment-token = "#" language-servers = ["basedpyright", "ruff"] indent = { tab-width = 4, unit = " " } auto-format = true [language.formatter] command = "ruff" [language.debugger] name = "debugpy" transport = "stdio" command = "python3" args = ["-m", "debugpy.adapter"] [[language.debugger.templates]] name = "source" request = "launch" completion = [ { name = "entrypoint", completion = "filename", default = "." } ] args = { mode = "debug", program = "{0}" } [language-server.basedpyright] command = "basedpyright-langserver" args = ["--stdio"] [language-server.basedpyright.config] basedpyright.analysis.typeCheckingMode = "standard" basedpyright.analysis.autoSearchPaths = true basedpyright.analysis.useLibraryCodeForTypes = true basedpyright.analysis.reportMissingTypeStubs = true basedpyright.analysis.analyzeUnannotatedFunctions = true basedpyright.analysis.strictParameterNoneValue = true basedpyright.analysis.reportImportCycles = true basedpyright.analysis.reportUnreachable = true basedpyright.analysis.reportPrivateUsage = false [language-server.ruff] command = "ruff-lsp" [language-server.ruff.config.settings] args = ["--ignore", "E402", "E731"] [language-server.ruff.config.settings.format] preview = true [[language]] name = "markdown" formatter = { command = 'prettier', args = ["--parser", "markdown"] } auto-format = true ```

helix's config.toml is irrelevant here as its only editor settings.

and then do, in a newly created py file:

import jax
jax.distributed.initialize()

No need to run it ofc, and see if it gives you a warning.

DetachHead commented 2 months ago

thanks, i'll play around with it and try to get back to you within a day or so. appreciate your patience, i know how much of a pain it is configuring stuff like this

DetachHead commented 2 months ago

sorry i dunno how i missed this. the issue was that you need to specify the diagnostic severity overrides under basedpyright.analysis.diagnosticSeverityOverrides. this works:

[language-server.basedpyright.config]
basedpyright.analysis.typeCheckingMode = "standard"
basedpyright.analysis.diagnosticSeverityOverrides.autoSearchPaths = true
basedpyright.analysis.diagnosticSeverityOverrides.useLibraryCodeForTypes = true
basedpyright.analysis.diagnosticSeverityOverrides.reportMissingTypeStubs = true
basedpyright.analysis.diagnosticSeverityOverrides.analyzeUnannotatedFunctions = true
basedpyright.analysis.diagnosticSeverityOverrides.strictParameterNoneValue = true
basedpyright.analysis.diagnosticSeverityOverrides.reportImportCycles = true
basedpyright.analysis.diagnosticSeverityOverrides.reportUnreachable = true
basedpyright.analysis.diagnosticSeverityOverrides.reportPrivateImportUsage = false
neel04 commented 2 months ago

That works! How did you find that? and how can someone find it for other config settings as well 👍

DetachHead commented 2 months ago

it's mentioned in the docs for the language server settings, but it's easy to miss:

basedpyright.analysis.diagnosticSeverityOverrides [map]: Allows a user to override the severity levels for individual diagnostic rules. "reportXXX" rules in the type check diagnostics settings in configuration are supported. Use the rule name as a key and one of "error," "warning," "information," "true," "false," or "none" as value.

i should probably add some examples for how to configure these, including examples for different editors

neel04 commented 2 months ago

That would be amazing 🔥 Thank you for such prompt responses, especially during a weekend. I appreciate you taking out the time here to fix silly problems. Would definitely recommend basedpyright to all my friends ;)

PS: The inlay hints work wonderfully well. Can't believe I lived so long without them

DetachHead commented 2 months ago

glad i could help, i'll keep this issue until i update the docs with helix (and other editor) specific examples

summersz commented 2 months ago

I am using basedpyright with helix and rye. It works fine until I add a [tool.basedpyright] section to myproject.toml to configure it, and then it reports missing imports. I have tried adding the venv and venvPath settings that were working with pyright but with or without them basedpyright is unable to resolve imports. Is there a helix or myproject.toml setting that I am missing?

DetachHead commented 2 months ago

can you send the full pyproject.toml?

summersz commented 2 months ago

It was an issue with having left my pyright config in. It works after I removed the tool.pyright section.

# [tool.pyright]
# exclude = [ ".venv" ]
# venvPath = "."
# venv = ".venv"

[tool.basedpyright]
venvPath = "."
venv = ".venv"
DetachHead commented 2 months ago

yeah there should be an error warning you about that (#614)