Skip to content

Commit

Permalink
Better output on wrong argument given to Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 14, 2024
1 parent 45015ad commit ea404a3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions prospector/formatters/grouped.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def render_messages(self) -> str:
groups: dict[Path, dict[int, list[Message]]] = defaultdict(lambda: defaultdict(list))

for message in self.messages:
assert message.location.line is not None
groups[self._make_path(message.location)][message.location.line].append(message)
line = message.location.line
groups[self._make_path(message.location)][-1 if line is None else line].append(message)

for filename in sorted(groups.keys()):
output.append(str(filename))
Expand Down
16 changes: 13 additions & 3 deletions prospector/formatters/pylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ def render_messages(self) -> list[str]:
# Missing function docstring

template_location = (
"%(path)s"
""
if message.location.path is None
else "%(path)s"
if message.location.line is None
else "%(path)s:%(line)s"
if message.location.character is None
else "%(path)s:%(line)s:%(character)s"
)
template_code = (
"%(code)s(%(source)s)" if message.location.function is None else "[%(code)s(%(source)s), %(function)s]"
"(%(source)s)"
if message.code is None
else "%(code)s(%(source)s)"
if message.location.function is None
else "[%(code)s(%(source)s), %(function)s]"
)
template = (
f"{template_location}: {template_code}: %(message)s"
if template_location
else f"{template_code}: %(message)s"
)
template = f"{template_location}: {template_code}: %(message)s"

output.append(
template
Expand Down
12 changes: 8 additions & 4 deletions prospector/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Location:
def __init__(
self,
path: Union[Path, str],
path: Optional[Union[Path, str]],
module: Optional[str],
function: Optional[str],
line: Optional[int],
Expand All @@ -15,6 +15,8 @@ def __init__(
self._path = path.absolute()
elif isinstance(path, str):
self._path = Path(path).absolute()
elif path is None:
self._path = None
else:
raise ValueError
self.module = module or None
Expand All @@ -23,13 +25,15 @@ def __init__(
self.character = None if character == -1 else character

@property
def path(self) -> Path:
def path(self) -> Optional[Path]:
return self._path

def absolute_path(self) -> Path:
def absolute_path(self) -> Optional[Path]:
return self._path

def relative_path(self, root: Optional[Path]) -> Path:
def relative_path(self, root: Optional[Path]) -> Optional[Path]:
if self._path is None:
return None
return self._path.relative_to(root) if root else self._path

def __repr__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion prospector/postfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def filter_messages(filepaths: List[Path], messages: List[Message]) -> List[Mess
filtered = []
for message in messages:
# first get rid of the pylint informational messages
relative_message_path = Path(message.location.path)
relative_message_path = None if message.location.path is None else Path(message.location.path)

if message.source == "pylint" and message.code in (
"suppressed-message",
Expand Down
10 changes: 10 additions & 0 deletions prospector/tools/ruff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def run(self, found_files: FileFinder) -> list[Message]:
completed_process = subprocess.run( # noqa: S603
[self.ruff_bin, *self.ruff_args, *found_files.python_modules], capture_output=True
)
if not completed_process.stdout:
messages.append(
Message(
"ruff",
"",
Location(None, None, None, None, None),
completed_process.stderr.decode(),
)
)
return messages
for message in json.loads(completed_process.stdout):
sub_message = {}
if message.get("url"):
Expand Down
1 change: 0 additions & 1 deletion tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_strings_or_paths(self):

def test_bad_path_input(self):
self.assertRaises(ValueError, Location, 3.2, "module", "func", 1, 2)
self.assertRaises(ValueError, Location, None, "module", "func", 1, 2)


class LocationOrderTest(TestCase):
Expand Down

0 comments on commit ea404a3

Please sign in to comment.