Documentation
¶
Overview ¶
Package python is the Python language pack (SPEC §8.1, §10.15, §10.34): Python. The grammars and language configurations are in assets/, copied by scripts/update-assets.sh; the licenses are in third_party/ and NOTICE. Since step 24b.4 the pack also runs a language server: basedpyright by default, Pyright or python-lsp-server when python.languageServer says so, or none. The server is found, started and shown by extensions/internal/langserver (the path settings basedpyright.path, pyright.path and pylsp.path, PATH, the folder of the interpreter, the status item, the notification for a missing server).
The pack also owns the choice of the Python interpreter (interpreter.go, interpreters.go): the discovery, "Python: Select Interpreter" (python.setInterpreter) with the status item that shows the interpreter's version and environment, the choice per folder kept in Emetos's state, and Interpreters.Current, which the debugger (M25) and the Testing provider (M26) of the pack read so that every feature runs the same Python.
Since step 26.6 the pack is also the Testing provider of Python (testctl.go, testexec.go, testcover.go; SPEC §10.32): the controller "python" runs pytest or unittest through the helper scripts in helpers/, with coverage.py for coverage and debugpy for Debug.
Index ¶
- Constants
- func CheckInterpreter(env langserver.Env, text string) string
- func New() extension.Extension
- func Normalize(env langserver.Env, text string) (string, error)
- type Interpreter
- type Interpreters
- func (s *Interpreters) Choose(path string) error
- func (s *Interpreters) Chosen() string
- func (s *Interpreters) Current() (Interpreter, bool)
- func (s *Interpreters) OnChange(fn func(Interpreter, bool)) (cancel func())
- func (s *Interpreters) Refresh()
- func (s *Interpreters) Resolve(env langserver.Env) (Interpreter, string, bool)
- func (s *Interpreters) Select()
- type Kind
Constants ¶
const ( // SetInterpreterCommand lists the interpreters and keeps the one the user picks for the folder ("Python: Select Interpreter"). A string // argument is the path to choose without asking. SetInterpreterCommand = "python.setInterpreter" // OrganizeImportsCommand sorts the imports of the file in front with the language server (Pyright's command, or the code action source.organizeImports). OrganizeImportsCommand = "python.organizeImports" // RestartCommand restarts the language server ("Python: Restart Language Server"). RestartCommand = "python.analysis.restartLanguageServer" )
The ids of the commands.
const ( // DebugType is the debug type of the debugger, and DebugTypeOld the name it had in the Python extension of VS Code: a launch.json // that says "python" is served by the same debugger. DebugType = "debugpy" DebugTypeOld = "python" // EnvFileSetting is the file of environment variables a launch reads when its configuration names none (VS Code's python.envFile). EnvFileSetting = "python.envFile" // DebugJustMyCodeSetting is the default of justMyCode (VS Code's debugpy.debugJustMyCode). DebugJustMyCodeSetting = "debugpy.debugJustMyCode" )
The ids of the debugger and its settings.
const ( // ID is the id of the extension, and ServerID the id of its language server in the manifest: one server, whichever program runs it. ID = "emetos.python" ServerID = "python" // LanguageServerSetting chooses the program: ServerBasedpyright, ServerPyright, ServerPylsp or ServerNone. LanguageServerSetting = "python.languageServer" // DefaultInterpreterSetting names the interpreter used when the user chose none for the folder. DefaultInterpreterSetting = "python.defaultInterpreterPath" // TraceSetting traces the traffic with the server (the host's). TraceSetting = "python.trace.server" // The path settings of the three programs. BasedpyrightPathSetting = "basedpyright.path" PyrightPathSetting = "pyright.path" PylspPathSetting = "pylsp.path" // StateKey is the key of the state under which the interpreter the user chose for the folder is kept (extension.State). StateKey = "python.interpreter" )
The ids and the values of the settings the pack reads.
const ( ServerBasedpyright = "basedpyright" ServerPyright = "pyright" ServerPylsp = "pylsp" ServerNone = "none" )
The values of python.languageServer.
const ( // TestControllerID is the id of the controller and TestControllerLabel its name. TestControllerID = "python" TestControllerLabel = "Python" )
The names of the controller and of its three run profiles (VS Code's Python profiles are "Run Tests", "Debug Tests" and "Coverage").
const ( // PytestEnabledSetting and UnittestEnabledSetting choose the framework; with neither set the pack uses pytest when the interpreter has // it and unittest otherwise. PytestEnabledSetting = "python.testing.pytestEnabled" UnittestEnabledSetting = "python.testing.unittestEnabled" // PytestArgsSetting and UnittestArgsSetting are the arguments of the frameworks. PytestArgsSetting = "python.testing.pytestArgs" UnittestArgsSetting = "python.testing.unittestArgs" // TestCwdSetting is the folder the tests run in (the folder of the workspace when empty). TestCwdSetting = "python.testing.cwd" // AutoDiscoverSetting and AutoDiscoverPatternSetting make the tests be found again when a file is saved. AutoDiscoverSetting = "python.testing.autoTestDiscoverOnSaveEnabled" AutoDiscoverPatternSetting = "python.testing.autoTestDiscoverOnSavePattern" )
The settings of the Testing provider (SPEC §10.32 "The providers", Python): VS Code Python's python.testing.* names.
Variables ¶
This section is empty.
Functions ¶
func CheckInterpreter ¶
func CheckInterpreter(env langserver.Env, text string) string
CheckInterpreter tells why the text is not an interpreter a person may choose, "" when it is (the input box shows it as it is typed).
func Normalize ¶
func Normalize(env langserver.Env, text string) (string, error)
Normalize turns what a person typed or a setting holds into the path of a program: ~, ${userHome}, ${workspaceFolder} and ${env:NAME} are expanded, a relative path is relative to the folder, a folder is an environment (its bin/python), and a name without a slash (python3) is looked for on PATH. An empty text gives "". It fails with PythonInterpreterInvalid when no program results.
Types ¶
type Interpreter ¶
type Interpreter struct {
// Path is the absolute path of the python program.
Path string
// Version is "3.12.1", empty when it could not be told.
Version string
// Name is the name of the environment (".venv", "base", "3.12.1" of pyenv); empty for a global interpreter.
Name string
// Kind says where it was found.
Kind Kind
}
Interpreter is a Python interpreter: the program that runs Python code, with the environment it belongs to.
func Describe ¶
func Describe(ctx context.Context, env langserver.Env, i Interpreter) Interpreter
Describe fills in the version and the name of the environment of the interpreter. The version comes from the pyvenv.cfg of an environment, else from running the program with --version; a program inside the folder of the tab is never run to describe it (a project must not decide what Emetos runs when it opens), so its version is empty when it has no pyvenv.cfg. ctx ends the run.
func Discover ¶
func Discover(env langserver.Env) []Interpreter
Discover lists the interpreters of this machine for the tab: python.defaultInterpreterPath, the environments .venv, venv and env in the folder, the VIRTUAL_ENV and CONDA_PREFIX Emetos was started with, the versions of pyenv ($PYENV_ROOT or ~/.pyenv) and the python programs of PATH, each once. It reads folders and runs nothing; Describe adds the versions.
func ResolveInterpreter ¶
func ResolveInterpreter(env langserver.Env, chosen string) (i Interpreter, why string, ok bool)
ResolveInterpreter chooses the interpreter a folder uses now, without running anything: the one the user chose (when its program is still there), else python.defaultInterpreterPath, else the active environment (VIRTUAL_ENV, then CONDA_PREFIX), else the first python3 or python on PATH. A folder's own .venv is not chosen for the user (Discover lists it; the user accepts it once, which keeps it as the chosen one). why says in words which rule decided; ok is false when there is no interpreter at all.
func (Interpreter) BinDir ¶
func (i Interpreter) BinDir() string
BinDir is the folder of the program (the bin folder of an environment): the place the tools installed with it are found.
func (Interpreter) Label ¶
func (i Interpreter) Label() string
Label is how the status item names the interpreter, as VS Code's does: "Python 3.12.1 ('.venv')", "Python 3.11.4 ('base': conda)", or "Python 3.12.1" for a global one. A version that is not known is left out.
func (Interpreter) Prefix ¶
func (i Interpreter) Prefix() string
Prefix is the folder of the environment: the parent of the bin folder.
type Interpreters ¶
type Interpreters struct {
// contains filtered or unexported fields
}
Interpreters is the interpreter of one scope (SPEC §10.34 "As built (step 24b.4)"): the one the user chose for the folder, kept in Emetos's state, or the one the rules of ResolveInterpreter give. The language server gets it, and the debugger and the Testing provider of the pack (M25, M26) read it with Current and follow it with OnChange, so that all of them run the same Python.
Resolve, Chosen and Current may be called from any goroutine; Choose, Refresh, OnChange, Select and the picker belong to the UI thread.
func (*Interpreters) Choose ¶
func (s *Interpreters) Choose(path string) error
Choose makes the program at path (or the environment in the folder at path) the interpreter of this folder, keeps the choice in the state, and restarts the language server, which gets the new one. An empty path forgets the choice, and the rules of ResolveInterpreter decide again. It fails with PythonInterpreterInvalid when path is no program, and with the error of State().Set. UI thread.
func (*Interpreters) Chosen ¶
func (s *Interpreters) Chosen() string
Chosen is the path the user chose for the folder of the tab, kept in the state; "" when none was.
func (*Interpreters) Current ¶
func (s *Interpreters) Current() (Interpreter, bool)
Current is the interpreter in use as Refresh last found it, with its version and environment name; false when the tab has none.
func (*Interpreters) OnChange ¶
func (s *Interpreters) OnChange(fn func(Interpreter, bool)) (cancel func())
OnChange calls fn, on the UI thread, whenever the interpreter in use changes (another path, or none), with the new one and whether there is one; it returns the function that stops the calls.
func (*Interpreters) Refresh ¶
func (s *Interpreters) Refresh()
Refresh finds the interpreter in use again, off the UI thread, and tells the subscribers and the pack when it changed. UI thread.
func (*Interpreters) Resolve ¶
func (s *Interpreters) Resolve(env langserver.Env) (Interpreter, string, bool)
Resolve is the interpreter the tab uses now, with the rule that decided (ResolveInterpreter); the version is not told (Describe does).
func (*Interpreters) Select ¶
func (s *Interpreters) Select()
Select shows the picker "Select Interpreter" over the editor: the interpreters of this machine are found off the UI thread, then listed. A client of a hosted workspace is told the interpreter is the host's choice (SPEC §10.18).
type Kind ¶
type Kind string
Kind says where an interpreter was found.
const ( // KindConfigured is the one python.defaultInterpreterPath names. KindConfigured Kind = "configured" // KindWorkspace is an environment in the folder of the tab: .venv, venv or env. KindWorkspace Kind = "workspace" // KindActive is the environment Emetos itself was started from (VIRTUAL_ENV). KindActive Kind = "active" // KindConda is the active conda environment (CONDA_PREFIX). KindConda Kind = "conda" // KindPyenv is a version pyenv installed. KindPyenv Kind = "pyenv" // KindGlobal is a python or python3 program on PATH. KindGlobal Kind = "global" // KindCustom is a path the user typed that none of the above lists. KindCustom Kind = "custom" )
The kinds of interpreter, in the order the picker lists them.