Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b064a5c

Browse files
authored
Fix dmypy inspect on Windows (#16355)
Fixes #15780
1 parent 4e30e89 commit b064a5c

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

mypy/inspections.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,6 @@ def __init__(
215215
# Module for which inspection was requested.
216216
self.module: State | None = None
217217

218-
def parse_location(self, location: str) -> tuple[str, list[int]]:
219-
if location.count(":") not in [2, 4]:
220-
raise ValueError("Format should be file:line:column[:end_line:end_column]")
221-
parts = location.split(":")
222-
module, *rest = parts
223-
return module, [int(p) for p in rest]
224-
225218
def reload_module(self, state: State) -> None:
226219
"""Reload given module while temporary exporting types."""
227220
old = self.fg_manager.manager.options.export_types
@@ -575,7 +568,7 @@ def run_inspection(
575568
This can be re-used by various simple inspections.
576569
"""
577570
try:
578-
file, pos = self.parse_location(location)
571+
file, pos = parse_location(location)
579572
except ValueError as err:
580573
return {"error": str(err)}
581574

@@ -617,3 +610,18 @@ def get_definition(self, location: str) -> dict[str, object]:
617610
result["out"] = f"No name or member expressions at {location}"
618611
result["status"] = 1
619612
return result
613+
614+
615+
def parse_location(location: str) -> tuple[str, list[int]]:
616+
if location.count(":") < 2:
617+
raise ValueError("Format should be file:line:column[:end_line:end_column]")
618+
parts = location.rsplit(":", maxsplit=2)
619+
start, *rest = parts
620+
# Note: we must allow drive prefix like `C:` on Windows.
621+
if start.count(":") < 2:
622+
return start, [int(p) for p in rest]
623+
parts = start.rsplit(":", maxsplit=2)
624+
start, *start_rest = parts
625+
if start.count(":") < 2:
626+
return start, [int(p) for p in start_rest + rest]
627+
raise ValueError("Format should be file:line:column[:end_line:end_column]")

mypy/test/testutil.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
from unittest import TestCase, mock
55

6+
from mypy.inspections import parse_location
67
from mypy.util import get_terminal_width
78

89

@@ -15,3 +16,7 @@ def test_get_terminal_size_in_pty_defaults_to_80(self) -> None:
1516
with mock.patch.object(os, "get_terminal_size", return_value=ret):
1617
with mock.patch.dict(os.environ, values=mock_environ, clear=True):
1718
assert get_terminal_width() == 80
19+
20+
def test_parse_location_windows(self) -> None:
21+
assert parse_location(r"C:\test.py:1:1") == (r"C:\test.py", [1, 1])
22+
assert parse_location(r"C:\test.py:1:1:1:1") == (r"C:\test.py", [1, 1, 1, 1])

test-data/unit/daemon.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ foo.py:3: error: Incompatible types in assignment (expression has type "str", va
372372
$ dmypy inspect foo:1
373373
Format should be file:line:column[:end_line:end_column]
374374
== Return code: 2
375+
$ dmypy inspect foo:1:2:3
376+
Source file is not a Python file
377+
== Return code: 2
375378
$ dmypy inspect foo.py:1:2:a:b
376379
invalid literal for int() with base 10: 'a'
377380
== Return code: 2

0 commit comments

Comments
 (0)