Skip to content

Commit

Permalink
run mypy in the directory of the nearest pyproject.toml or mypy.ini (#…
Browse files Browse the repository at this point in the history
…316)

* run mypy in the directory of the nearest pyproject.toml

* check for mypy.ini too

* ${filePyproject} -> ${nearestConfig}

* move `doc is not None` to early guard in get_cwd

* update README.md and add ${nearestConfig} to settings.cwd UI description as well

Co-authored-by: Luciana Abud <[email protected]>

* fixup pyfmt and logs

---------

Co-authored-by: Luciana Abud <[email protected]>
  • Loading branch information
jwhitaker-gridcog and luabud authored Sep 27, 2024
1 parent d4de647 commit a356c8e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ There are several settings you can configure to customize the behavior of this e
<tr>
<td>mypy-type-checker.cwd</td>
<td><code>${workspaceFolder}</code></td>
<td>Sets the current working directory used to lint Python files with Mypy. By default, it uses the root directory of the workspace <code>${workspaceFolder}</code>. You can set it to <code>${fileDirname}</code> to use the parent folder of the file being linted as the working directory for Mypy.
<td>Sets the current working directory used to lint Python files with Mypy. By default, it uses the root directory of the workspace <code>${workspaceFolder}</code>. You can set it to <code>${fileDirname}</code> to use the parent folder of the file being linted as the working directory for Mypy. You can also set it to <code>${nearestConfig}</code> to use the nearest parent/ancestor folder which contains a <code>pyproject.toml</code> or a <code>mypy.ini</code> file.
</td>
</tr>
<tr>
Expand Down
32 changes: 29 additions & 3 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,13 +642,39 @@ def _get_env_vars(settings: Dict[str, Any]) -> Dict[str, str]:

def get_cwd(settings: Dict[str, Any], document: Optional[workspace.Document]) -> str:
"""Returns cwd for the given settings and document."""
# this happens when running dmypy.
if document is None:
return settings["workspaceFS"]

if settings["cwd"] == "${workspaceFolder}":
return settings["workspaceFS"]

if settings["cwd"] == "${fileDirname}":
if document is not None:
return os.fspath(pathlib.Path(document.path).parent)
return settings["workspaceFS"]
return os.fspath(pathlib.Path(document.path).parent)

if settings["cwd"] == "${nearestConfig}":
workspaceFolder = pathlib.Path(settings["workspaceFS"])
candidate = pathlib.Path(document.path).parent
# check if pyproject exists
check_for = ["pyproject.toml", "mypy.ini"]
# until we leave the workspace
while candidate.is_relative_to(workspaceFolder):
for n in check_for:
candidate_file = candidate / n
if candidate_file.is_file():
log_to_output(
f"found {n}, using {candidate}", lsp.MessageType.Debug
)
return os.fspath(candidate)
# starting from the current file and working our way up
else:
candidate = candidate.parent
else:
log_to_output(
f"failed to find {', '.join(check_for)}; using workspace root",
lsp.MessageType.Debug,
)
return settings["workspaceFS"]

return settings["cwd"]

Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extension.description": "Type checking support for Python files using Mypy.",
"command.restartServer": "Restart Server",
"settings.args.description": "Arguments passed to Mypy to enable type checking on Python files. Each argument should be provided as a separate string in the array. \n Example: \n `\"mypy-type-checker.args\" = [\"--config-file=<file>\"]`",
"settings.cwd.description": "Sets the current working directory used to lint Python files with Mypy. By default, it uses the root directory of the workspace `${workspaceFolder}`. You can set it to `${fileDirname}` to use the parent folder of the file being linted as the working directory for Mypy.",
"settings.cwd.description": "Sets the current working directory used to lint Python files with Mypy. By default, it uses the root directory of the workspace `${workspaceFolder}`. You can set it to `${fileDirname}` to use the parent folder of the file being linted as the working directory for Mypy. You can also set it to `${nearestConfig}` to use the nearest parent/ancestor folder which contains a `pyproject.toml` or a `mypy.ini` file.",
"settings.severity.description": "Mapping of Mypy's message types to VS Code's diagnostic severity levels as displayed in the Problems window. You can also use it to override specific Mypy error codes. <br>For example: `{\"error\": \"Error\", \"note\": \"Information\", \"name-defined\": \"Warning\" }`",
"settings.path.description": "Path or command to be used by the extension to type check Python files with Mypy. Accepts an array of a single or multiple strings. If passing a command, each argument should be provided as a separate string in the array. If set to `[\"mypy\"]`, it will use the version of Mypy available in the `PATH` environment variable. <br> Note: Using this option may slowdown type checking. \n Examples: \n- `[\"~/global_env/mypy\"]` \n- `[\"conda\", \"run\", \"-n\", \"lint_env\", \"python\", \"-m\", \"mypy\"]`",
"settings.ignorePatterns.description": "Configure [glob patterns](https://docs.python.org/3/library/fnmatch.html) as supported by the fnmatch Python library to exclude files or folders from being linted with Mypy.",
Expand Down
13 changes: 12 additions & 1 deletion src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ async function createServer(
initializationOptions: IInitOptions,
): Promise<LanguageClient> {
const command = settings.interpreter[0];
const cwd = settings.cwd === '${fileDirname}' ? Uri.parse(settings.workspace).fsPath : settings.cwd;
let cwd: string;
if (settings.cwd === '${fileDirname}') {
cwd = Uri.parse(settings.workspace).fsPath;
} else if (settings.cwd === '${nearestConfig}') {
cwd = Uri.parse(settings.workspace).fsPath;
} else {
cwd = settings.cwd;
}

if (!fsapi.existsSync(cwd)) {
traceError(`Server cwd ${cwd} doesn't exist, server startup will probably fail`);
}

// Set debugger path needed for debugging python code.
const newEnv = { ...process.env };
Expand Down

0 comments on commit a356c8e

Please sign in to comment.