Skip to content

Commit 3fed49f

Browse files
authored
Add Ruff to CI (#21739)
Add Ruff to (lint > action.yml) CI #21738
1 parent c490339 commit 3fed49f

File tree

16 files changed

+73
-30
lines changed

16 files changed

+73
-30
lines changed

.github/actions/lint/action.yml

+7
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ runs:
4747
python -m black . --check
4848
working-directory: pythonFiles
4949
shell: bash
50+
51+
- name: Run Ruff
52+
run: |
53+
python -m pip install -U ruff
54+
python -m ruff check .
55+
working-directory: pythonFiles
56+
shell: bash

pythonFiles/installed_check.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def parse_requirements(line: str) -> Optional[Requirement]:
3636
return req
3737
elif req.marker.evaluate():
3838
return req
39-
except:
39+
except Exception:
4040
return None
4141

4242

@@ -51,7 +51,7 @@ def process_requirements(req_file: pathlib.Path) -> List[Dict[str, Union[str, in
5151
try:
5252
# Check if package is installed
5353
metadata(req.name)
54-
except:
54+
except Exception:
5555
diagnostics.append(
5656
{
5757
"line": n,
@@ -79,7 +79,7 @@ def process_pyproject(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]
7979
try:
8080
raw_text = req_file.read_text(encoding="utf-8")
8181
pyproject = tomli.loads(raw_text)
82-
except:
82+
except Exception:
8383
return diagnostics
8484

8585
lines = raw_text.splitlines()
@@ -91,7 +91,7 @@ def process_pyproject(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]
9191
try:
9292
# Check if package is installed
9393
metadata(req.name)
94-
except:
94+
except Exception:
9595
diagnostics.append(
9696
{
9797
"line": n,

pythonFiles/normalizeSelection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def normalize_lines(selection):
118118

119119
# Insert a newline between each top-level statement, and append a newline to the selection.
120120
source = "\n".join(statements) + "\n"
121-
except:
121+
except Exception:
122122
# If there's a problem when parsing statements,
123123
# append a blank line to end the block and send it as-is.
124124
source = selection + "\n\n"

pythonFiles/pyproject.toml

+31
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,34 @@ ignore = [
3434
'tests/testing_tools/adapter/pytest/test_cli.py',
3535
'tests/testing_tools/adapter/pytest/test_discovery.py',
3636
]
37+
38+
[tool.ruff]
39+
line-length = 140
40+
ignore = ["E402"]
41+
exclude = [
42+
# Ignore testing_tools files same as Pyright way
43+
'get-pip.py',
44+
'install_debugpy.py',
45+
'tensorboard_launcher.py',
46+
'testlauncher.py',
47+
'visualstudio_py_testlauncher.py',
48+
'testing_tools/unittest_discovery.py',
49+
'testing_tools/adapter/util.py',
50+
'testing_tools/adapter/pytest/_discovery.py',
51+
'testing_tools/adapter/pytest/_pytest_item.py',
52+
'tests/debug_adapter/test_install_debugpy.py',
53+
'tests/testing_tools/adapter/.data',
54+
'tests/testing_tools/adapter/test___main__.py',
55+
'tests/testing_tools/adapter/test_discovery.py',
56+
'tests/testing_tools/adapter/test_functional.py',
57+
'tests/testing_tools/adapter/test_report.py',
58+
'tests/testing_tools/adapter/test_util.py',
59+
'tests/testing_tools/adapter/pytest/test_cli.py',
60+
'tests/testing_tools/adapter/pytest/test_discovery.py',
61+
'pythonFiles/testing_tools/*',
62+
'pythonFiles/testing_tools/adapter/pytest/__init__.py',
63+
'pythonFiles/tests/pytestadapter/expected_execution_test_output.py',
64+
'pythonFiles/tests/unittestadapter/.data/discovery_error/file_one.py',
65+
'pythonFiles/tests/unittestadapter/test_utils.py',
66+
67+
]

pythonFiles/shell_exec.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
import os
5-
import sys
64
import subprocess
5+
import sys
76

87
# This is a simple solution to waiting for completion of commands sent to terminal.
98
# 1. Intercept commands send to a terminal

pythonFiles/testing_tools/adapter/pytest/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
from __future__ import absolute_import
55

6-
from ._cli import add_subparser as add_cli_subparser
7-
from ._discovery import discover
6+
from ._cli import add_subparser as add_cli_subparser # noqa: F401
7+
from ._discovery import discover # noqa: F401

pythonFiles/tests/pytestadapter/expected_execution_test_output.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
TEST_ADD_FUNCTION = "unittest_folder/test_add.py::TestAddFunction::"
66
SUCCESS = "success"
77
FAILURE = "failure"
8-
TEST_SUBTRACT_FUNCTION_NEGATIVE_NUMBERS_ERROR = "self = <test_subtract.TestSubtractFunction testMethod=test_subtract_negative_numbers>\n\n def test_subtract_negative_numbers( # test_marker--test_subtract_negative_numbers\n self,\n ):\n result = subtract(-2, -3)\n> self.assertEqual(result, 100000)\nE AssertionError: 1 != 100000\n\nunittest_folder/test_subtract.py:25: AssertionError"
8+
9+
TEST_SUBTRACT_FUNCTION_NEGATIVE_NUMBERS_ERROR = "self = <test_subtract.TestSubtractFunction testMethod=test_subtract_negative_numbers>\n\n def test_subtract_negative_numbers( # test_marker--test_subtract_negative_numbers\n self,\n ):\n result = subtract(-2, -3)\n> self.assertEqual(result, 100000)\nE AssertionError: 1 != 100000\n\nunittest_folder/test_subtract.py:25: AssertionError" # noqa: E501
10+
911

1012
# This is the expected output for the unittest_folder execute tests
1113
# └── unittest_folder

pythonFiles/tests/pytestadapter/helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
import threading
1212
import uuid
13-
from typing import Any, Dict, List, Optional, Tuple, Union
13+
from typing import Any, Dict, List, Optional, Tuple
1414

1515
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
1616
from typing_extensions import TypedDict

pythonFiles/tests/pytestadapter/test_execution.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import shutil
55

66
import pytest
7+
78
from tests.pytestadapter import expected_execution_test_output
89

910
from .helpers import TEST_DATA_PATH, runner
@@ -161,7 +162,7 @@ def test_pytest_execution(test_ids, expected_const):
161162
Keyword arguments:
162163
test_ids -- an array of test_ids to run.
163164
expected_const -- a dictionary of the expected output from running pytest discovery on the files.
164-
"""
165+
""" # noqa: E501
165166
args = test_ids
166167
actual = runner(args)
167168
assert actual
@@ -179,6 +180,6 @@ def test_pytest_execution(test_ids, expected_const):
179180
or actual_result_dict[key]["outcome"] == "error"
180181
):
181182
actual_result_dict[key]["message"] = "ERROR MESSAGE"
182-
if actual_result_dict[key]["traceback"] != None:
183+
if actual_result_dict[key]["traceback"] is not None:
183184
actual_result_dict[key]["traceback"] = "TRACEBACK"
184185
assert actual_result_dict == expected_const

pythonFiles/tests/test_create_microvenv.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import sys
77

88
import create_microvenv
9-
import pytest
109

1110

1211
def test_create_microvenv():
@@ -26,4 +25,4 @@ def run_process(args, error_message):
2625
create_microvenv.run_process = run_process
2726

2827
create_microvenv.main()
29-
assert run_process_called == True
28+
assert run_process_called is True

pythonFiles/tests/test_create_venv.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import os
66
import sys
77

8-
import create_venv
98
import pytest
109

10+
import create_venv
11+
1112

1213
@pytest.mark.skipif(
1314
sys.platform == "win32", reason="Windows does not have micro venv fallback."
@@ -35,7 +36,7 @@ def run_process(args, error_message):
3536
create_venv.main(["--name", ".test_venv"])
3637

3738
# run_process is called when the venv does not exist
38-
assert run_process_called == True
39+
assert run_process_called is True
3940

4041

4142
@pytest.mark.skipif(

pythonFiles/tests/unittestadapter/.data/discovery_error/file_one.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import unittest
55

6-
import something_else # type: ignore
6+
import something_else # type: ignore # noqa: F401
77

88

99
class DiscoveryErrorOne(unittest.TestCase):

pythonFiles/tests/unittestadapter/test_utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import unittest
77

88
import pytest
9+
910
from unittestadapter.utils import (
1011
TestNode,
1112
TestNodeTypeEnum,
@@ -284,7 +285,8 @@ def test_build_decorated_tree() -> None:
284285

285286

286287
def test_build_empty_tree() -> None:
287-
"""The build_test_tree function should return None if there are no discovered test suites, and an empty list of errors if there are none in the discovered data."""
288+
"""The build_test_tree function should return None if there are no discovered test suites,
289+
and an empty list of errors if there are none in the discovered data."""
288290

289291
start_dir = os.fsdecode(TEST_DATA_PATH)
290292
pattern = "does_not_exist*"

pythonFiles/unittestadapter/execution.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
sys.path.append(os.fspath(script_dir))
1818
sys.path.insert(0, os.fspath(script_dir / "lib" / "python"))
1919

20-
from testing_tools import process_json_util, socket_manager
2120
from typing_extensions import NotRequired, TypeAlias, TypedDict
21+
22+
from testing_tools import process_json_util, socket_manager
2223
from unittestadapter.utils import parse_unittest_args
2324

2425
DEFAULT_PORT = "45454"
@@ -194,12 +195,12 @@ def run_tests(
194195
# Discover tests at path with the file name as a pattern (if any).
195196
loader = unittest.TestLoader()
196197

197-
args = {
198+
args = { # noqa: F841
198199
"start_dir": start_dir,
199200
"pattern": pattern,
200201
"top_level_dir": top_level_dir,
201202
}
202-
suite = loader.discover(start_dir, pattern, top_level_dir)
203+
suite = loader.discover(start_dir, pattern, top_level_dir) # noqa: F841
203204

204205
# Run tests.
205206
runner = unittest.TextTestRunner(resultclass=UnittestTestResult)

pythonFiles/unittestadapter/utils.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def get_source_line(obj) -> str:
6060
"""Get the line number of a test case start line."""
6161
try:
6262
sourcelines, lineno = inspect.getsourcelines(obj)
63-
except:
63+
except Exception:
6464
try:
6565
# tornado-specific, see https://github.com/microsoft/vscode-python/issues/17285.
6666
sourcelines, lineno = inspect.getsourcelines(obj.orig_method)
67-
except:
67+
except Exception:
6868
return "*"
6969

7070
# Return the line number of the first line of the test case definition.
@@ -226,7 +226,8 @@ def parse_unittest_args(args: List[str]) -> Tuple[str, str, Union[str, None]]:
226226
The returned tuple contains the following items
227227
- start_directory: The directory where to start discovery, defaults to .
228228
- pattern: The pattern to match test files, defaults to test*.py
229-
- top_level_directory: The top-level directory of the project, defaults to None, and unittest will use start_directory behind the scenes.
229+
- top_level_directory: The top-level directory of the project, defaults to None,
230+
and unittest will use start_directory behind the scenes.
230231
"""
231232

232233
arg_parser = argparse.ArgumentParser()

pythonFiles/vscode_datascience_helpers/tests/logParser.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from io import TextIOWrapper
2-
import sys
31
import argparse
42
import os
3+
from io import TextIOWrapper
54

65
os.system("color")
7-
from pathlib import Path
86
import re
7+
from pathlib import Path
98

109
parser = argparse.ArgumentParser(description="Parse a test log into its parts")
1110
parser.add_argument("testlog", type=str, nargs=1, help="Log to parse")
@@ -63,14 +62,14 @@ def splitByPid(testlog):
6362
pid = int(match.group(1))
6463

6564
# See if we've created a log for this pid or not
66-
if not pid in pids:
65+
if pid not in pids:
6766
pids.add(pid)
6867
logFile = "{}_{}.log".format(baseFile, pid)
6968
print("Writing to new log: " + logFile)
7069
logs[pid] = Path(logFile).open(mode="w")
7170

7271
# Add this line to the log
73-
if pid != None:
72+
if pid is not None:
7473
logs[pid].write(line)
7574
# Close all of the open logs
7675
for key in logs:

0 commit comments

Comments
 (0)