Skip to content

Commit

Permalink
Fix dmypy inspect on Windows (#16355)
Browse files Browse the repository at this point in the history
Fixes #15780
  • Loading branch information
ilevkivskyi authored Oct 30, 2023
1 parent 4e30e89 commit b064a5c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
24 changes: 16 additions & 8 deletions mypy/inspections.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,6 @@ def __init__(
# Module for which inspection was requested.
self.module: State | None = None

def parse_location(self, location: str) -> tuple[str, list[int]]:
if location.count(":") not in [2, 4]:
raise ValueError("Format should be file:line:column[:end_line:end_column]")
parts = location.split(":")
module, *rest = parts
return module, [int(p) for p in rest]

def reload_module(self, state: State) -> None:
"""Reload given module while temporary exporting types."""
old = self.fg_manager.manager.options.export_types
Expand Down Expand Up @@ -575,7 +568,7 @@ def run_inspection(
This can be re-used by various simple inspections.
"""
try:
file, pos = self.parse_location(location)
file, pos = parse_location(location)
except ValueError as err:
return {"error": str(err)}

Expand Down Expand Up @@ -617,3 +610,18 @@ def get_definition(self, location: str) -> dict[str, object]:
result["out"] = f"No name or member expressions at {location}"
result["status"] = 1
return result


def parse_location(location: str) -> tuple[str, list[int]]:
if location.count(":") < 2:
raise ValueError("Format should be file:line:column[:end_line:end_column]")
parts = location.rsplit(":", maxsplit=2)
start, *rest = parts
# Note: we must allow drive prefix like `C:` on Windows.
if start.count(":") < 2:
return start, [int(p) for p in rest]
parts = start.rsplit(":", maxsplit=2)
start, *start_rest = parts
if start.count(":") < 2:
return start, [int(p) for p in start_rest + rest]
raise ValueError("Format should be file:line:column[:end_line:end_column]")
5 changes: 5 additions & 0 deletions mypy/test/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from unittest import TestCase, mock

from mypy.inspections import parse_location
from mypy.util import get_terminal_width


Expand All @@ -15,3 +16,7 @@ def test_get_terminal_size_in_pty_defaults_to_80(self) -> None:
with mock.patch.object(os, "get_terminal_size", return_value=ret):
with mock.patch.dict(os.environ, values=mock_environ, clear=True):
assert get_terminal_width() == 80

def test_parse_location_windows(self) -> None:
assert parse_location(r"C:\test.py:1:1") == (r"C:\test.py", [1, 1])
assert parse_location(r"C:\test.py:1:1:1:1") == (r"C:\test.py", [1, 1, 1, 1])
3 changes: 3 additions & 0 deletions test-data/unit/daemon.test
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ foo.py:3: error: Incompatible types in assignment (expression has type "str", va
$ dmypy inspect foo:1
Format should be file:line:column[:end_line:end_column]
== Return code: 2
$ dmypy inspect foo:1:2:3
Source file is not a Python file
== Return code: 2
$ dmypy inspect foo.py:1:2:a:b
invalid literal for int() with base 10: 'a'
== Return code: 2
Expand Down

0 comments on commit b064a5c

Please sign in to comment.