Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable explicit Ruff check rules #23741

Merged
merged 61 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
130e87d
Enable F, E4, E7, E9, and I in Ruff check
brettcannon Jul 3, 2024
9012210
Turn on N
brettcannon Jul 3, 2024
91d801c
Turn on various D rules
brettcannon Jul 3, 2024
972353e
Turn on UP rules
brettcannon Jul 3, 2024
21ac03f
Turn on YTT rules
brettcannon Jul 3, 2024
1fedc31
Turn on ASYNC rules
brettcannon Jul 3, 2024
f888518
Turn on B rules
brettcannon Jul 3, 2024
586f121
Add A rules
brettcannon Jul 3, 2024
bdb1a1b
Add C4 rules
brettcannon Jul 3, 2024
09021d5
Add DTZ rules
brettcannon Jul 3, 2024
fc696a3
Turn on INP rules
brettcannon Jul 4, 2024
a10f505
Make Pyright happy
brettcannon Jul 4, 2024
d18042b
Pylance didn't succeed at fixing all calls
brettcannon Jul 4, 2024
44c99c2
Fix some formatting
brettcannon Jul 4, 2024
31f0e90
Simplify formatting config
brettcannon Jul 4, 2024
bce5041
Add PIE rules
brettcannon Jul 4, 2024
88b79b0
Add PYI rules
brettcannon Jul 4, 2024
e3107d7
Add PT rules
brettcannon Jul 4, 2024
db15c29
Fix a refactoring mistake
brettcannon Jul 4, 2024
a623b1e
Turn off PYI024
brettcannon Jul 4, 2024
133166e
Turn on RSE rules
brettcannon Jul 4, 2024
c6a5c7a
Add RET503 and 504 rules
brettcannon Jul 4, 2024
179b5e3
Add SLF rules
brettcannon Jul 4, 2024
62a3622
Add SIM rules
brettcannon Jul 4, 2024
548afc7
Add TCH rules
brettcannon Jul 4, 2024
dbac014
Add ARG rules
brettcannon Jul 4, 2024
13b309f
Revert "Add SIM rules"
brettcannon Jul 4, 2024
520aeda
Add SIM102 rule
brettcannon Jul 4, 2024
d0808b1
Add SIM115
brettcannon Jul 4, 2024
c5aeb4c
Add SIM118
brettcannon Jul 4, 2024
6ee0cdc
Fix formatting
brettcannon Jul 4, 2024
87e2eb3
Add SIM108
brettcannon Jul 4, 2024
2adbbd4
Add SIM105
brettcannon Jul 4, 2024
72bda7f
Add SIM117
brettcannon Jul 4, 2024
9bd2e33
Turn on all SIM rules
brettcannon Jul 4, 2024
0e2db76
Some fixes for PTH
brettcannon Jul 4, 2024
4e2ee2e
Fix a typing issue
brettcannon Jul 4, 2024
3296986
More PTH fixes
brettcannon Jul 4, 2024
82c33a4
Add PTH rules
brettcannon Jul 4, 2024
ad6e4d8
Fix a method name
brettcannon Jul 4, 2024
4f9c303
Fix a variable name
brettcannon Jul 4, 2024
9514c42
Turn on PD
brettcannon Jul 4, 2024
cec8dab
Add NPY rules
brettcannon Jul 4, 2024
34f3685
Add PERF rules
brettcannon Jul 4, 2024
f942904
Add RUF rules
brettcannon Jul 4, 2024
9db880b
Sort Ruff rules
brettcannon Jul 4, 2024
991c139
Turn on Q rules
brettcannon Jul 4, 2024
e38091e
Add LOG rules
brettcannon Jul 4, 2024
4063606
Add DJ rules
brettcannon Jul 4, 2024
e7abc7d
Add EXE rules
brettcannon Jul 4, 2024
43bf51f
Add FBT rules
brettcannon Jul 4, 2024
6e8bd1d
Add FLY rules
brettcannon Jul 4, 2024
b3f4296
Add FURB rules
brettcannon Jul 4, 2024
8fb4d0e
Add ISC rules
brettcannon Jul 4, 2024
8246c00
Add INT rules
brettcannon Jul 4, 2024
36b7685
Add SLOT rules
brettcannon Jul 4, 2024
5a634fb
Add W rules
brettcannon Jul 4, 2024
9c7a616
Merge branch 'main' into more-ruff-check
brettcannon Jul 4, 2024
00f7d9c
Remove ISC
brettcannon Jul 4, 2024
d11737e
Update python_files/pyproject.toml
brettcannon Jul 9, 2024
4f11ce5
Merge branch 'main' into more-ruff-check
brettcannon Jul 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build/ci/addEnvPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#Adds the virtual environment's executable path to json file

import json,sys
import json
import sys
import os.path
jsonPath = sys.argv[1]
key = sys.argv[2]
Expand Down
2 changes: 1 addition & 1 deletion build/update_ext_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def main(package_json: pathlib.Path, argv: Sequence[str]) -> None:
raise ValueError(
f"Major version [{major}] must be the current year [{current_year}].",
f"If changing major version after new year's, change to {current_year}.1.0",
f"Minor version must be updated based on release or pre-release channel.",
"Minor version must be updated based on release or pre-release channel.",
)

if args.release and not is_even(minor):
Expand Down
19 changes: 9 additions & 10 deletions python_files/create_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ def parse_args(argv: Sequence[str]) -> argparse.Namespace:


def file_exists(path: Union[str, pathlib.PurePath]) -> bool:
return os.path.exists(path)
return os.path.exists(path) # noqa: PTH110


def conda_env_exists(name: Union[str, pathlib.PurePath]) -> bool:
return os.path.exists(CWD / name)
return os.path.exists(CWD / name) # noqa: PTH110


def run_process(args: Sequence[str], error_message: str) -> None:
try:
print("Running: " + " ".join(args))
subprocess.run(args, cwd=os.getcwd(), check=True)
except subprocess.CalledProcessError:
raise VenvError(error_message)
subprocess.run(args, cwd=os.getcwd(), check=True) # noqa: PTH109
except subprocess.CalledProcessError as exc:
raise VenvError(error_message) from exc


def get_conda_env_path(name: str) -> str:
Expand Down Expand Up @@ -89,11 +89,10 @@ def install_packages(env_path: str) -> None:


def add_gitignore(name: str) -> None:
git_ignore = os.fspath(CWD / name / ".gitignore")
if not file_exists(git_ignore):
print(f"Creating: {git_ignore}")
with open(git_ignore, "w") as f:
f.write("*")
git_ignore = CWD / name / ".gitignore"
if not git_ignore.is_file():
print(f"Creating: {os.fsdecode(git_ignore)}")
git_ignore.write_text("*")


def main(argv: Optional[Sequence[str]] = None) -> None:
Expand Down
6 changes: 3 additions & 3 deletions python_files/create_microvenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class MicroVenvError(Exception):
def run_process(args: Sequence[str], error_message: str) -> None:
try:
print("Running: " + " ".join(args))
subprocess.run(args, cwd=os.getcwd(), check=True)
except subprocess.CalledProcessError:
raise MicroVenvError(error_message)
subprocess.run(args, cwd=os.getcwd(), check=True) # noqa: PTH109
except subprocess.CalledProcessError as exc:
raise MicroVenvError(error_message) from exc


def parse_args(argv: Sequence[str]) -> argparse.Namespace:
Expand Down
24 changes: 10 additions & 14 deletions python_files/create_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def venv_exists(name: str) -> bool:
def run_process(args: Sequence[str], error_message: str) -> None:
try:
print("Running: " + " ".join(args))
subprocess.run(args, cwd=os.getcwd(), check=True)
except subprocess.CalledProcessError:
raise VenvError(error_message)
subprocess.run(args, cwd=os.getcwd(), check=True) # noqa: PTH109
except subprocess.CalledProcessError as exc:
raise VenvError(error_message) from exc


def get_venv_path(name: str) -> str:
Expand Down Expand Up @@ -136,10 +136,9 @@ def upgrade_pip(venv_path: str) -> None:

def add_gitignore(name: str) -> None:
git_ignore = CWD / name / ".gitignore"
if not file_exists(git_ignore):
print("Creating: " + os.fspath(git_ignore))
with open(git_ignore, "w") as f:
f.write("*")
if git_ignore.is_file():
print("Creating:", os.fspath(git_ignore))
git_ignore.write_text("*")


def download_pip_pyz(name: str):
Expand All @@ -148,13 +147,10 @@ def download_pip_pyz(name: str):

try:
with url_lib.urlopen(url) as response:
pip_pyz_path = os.fspath(CWD / name / "pip.pyz")
with open(pip_pyz_path, "wb") as out_file:
data = response.read()
out_file.write(data)
out_file.flush()
except Exception:
raise VenvError("CREATE_VENV.DOWNLOAD_PIP_FAILED")
pip_pyz_path = CWD / name / "pip.pyz"
pip_pyz_path.write_bytes(data=response.read())
except Exception as exc:
raise VenvError("CREATE_VENV.DOWNLOAD_PIP_FAILED") from exc


def install_pip(name: str):
Expand Down
8 changes: 4 additions & 4 deletions python_files/download_get_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Licensed under the MIT License.

import json
import os
import pathlib
import urllib.request as url_lib

from packaging.version import parse as version_parser

EXTENSION_ROOT = pathlib.Path(__file__).parent.parent
Expand All @@ -14,20 +14,20 @@


def _get_package_data():
json_uri = "https://pypi.org/pypi/{0}/json".format(PIP_PACKAGE)
json_uri = f"https://pypi.org/pypi/{PIP_PACKAGE}/json"
# Response format: https://warehouse.readthedocs.io/api-reference/json/#project
# Release metadata format: https://github.com/pypa/interoperability-peps/blob/master/pep-0426-core-metadata.rst
with url_lib.urlopen(json_uri) as response:
return json.loads(response.read())


def _download_and_save(root, version):
root = os.getcwd() if root is None or root == "." else root
root = pathlib.Path.cwd() if root is None or root == "." else pathlib.Path(root)
url = f"https://raw.githubusercontent.com/pypa/get-pip/{version}/public/get-pip.py"
print(url)
with url_lib.urlopen(url) as response:
data = response.read()
get_pip_file = pathlib.Path(root) / "get-pip.py"
get_pip_file = root / "get-pip.py"
get_pip_file.write_bytes(data)


Expand Down
4 changes: 2 additions & 2 deletions python_files/get_output_via_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
del sys.argv[0]
exec(code, ns, ns)
elif module.startswith("-m"):
moduleName = sys.argv[2]
module_name = sys.argv[2]
sys.argv = sys.argv[2:] # It should begin with the module name.
runpy.run_module(moduleName, run_name="__main__", alter_sys=True)
runpy.run_module(module_name, run_name="__main__", alter_sys=True)
elif module.endswith(".py"):
sys.argv = sys.argv[1:]
runpy.run_path(module, run_name="__main__")
Expand Down
4 changes: 1 addition & 3 deletions python_files/installed_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def parse_args(argv: Optional[Sequence[str]] = None):
def parse_requirements(line: str) -> Optional[Requirement]:
try:
req = Requirement(line.strip("\\"))
if req.marker is None:
return req
elif req.marker.evaluate():
if req.marker is None or req.marker.evaluate():
return req
except Exception:
pass
Expand Down
1 change: 0 additions & 1 deletion python_files/linter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import subprocess
import sys


linter_settings = {
"pylint": {
"args": ["--reports=n", "--output-format=json"],
Expand Down
42 changes: 19 additions & 23 deletions python_files/normalizeSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ def split_lines(source):


def _get_statements(selection):
"""
Process a multiline selection into a list of its top-level statements.
"""Process a multiline selection into a list of its top-level statements.

This will remove empty newlines around and within the selection, dedent it,
and split it using the result of `ast.parse()`.
"""

# Remove blank lines within the selection to prevent the REPL from thinking the block is finished.
lines = (line for line in split_lines(selection) if line.strip() != "")

Expand Down Expand Up @@ -57,7 +56,7 @@ def _get_statements(selection):
# Also, not all AST objects can have decorators.
if hasattr(node, "decorator_list") and sys.version_info >= (3, 8):
# Using getattr instead of node.decorator_list or pyright will complain about an unknown member.
line_end -= len(getattr(node, "decorator_list"))
line_end -= len(getattr(node, "decorator_list")) # noqa: B009
ends.append(line_end)
ends.append(len(lines))

Expand All @@ -74,7 +73,7 @@ def _get_statements(selection):
# Special handling of decorators similar to what's above.
if hasattr(node, "decorator_list") and sys.version_info >= (3, 8):
# Using getattr instead of node.decorator_list or pyright will complain about an unknown member.
start -= len(getattr(node, "decorator_list"))
start -= len(getattr(node, "decorator_list")) # noqa: B009
block = "\n".join(lines[start:end])

# If the block is multiline, add an extra newline character at its end.
Expand Down Expand Up @@ -134,26 +133,24 @@ def normalize_lines(selection):


def check_exact_exist(top_level_nodes, start_line, end_line):
exact_nodes = []
for node in top_level_nodes:
if node.lineno == start_line and node.end_lineno == end_line:
exact_nodes.append(node)
return [
node
for node in top_level_nodes
if node.lineno == start_line and node.end_lineno == end_line
]

return exact_nodes

def traverse_file(whole_file_content, start_line, end_line, was_highlighted): # noqa: ARG001
"""Intended to traverse through a user's given file content and find, collect all appropriate lines that should be sent to the REPL in case of smart selection.

def traverse_file(wholeFileContent, start_line, end_line, was_highlighted):
"""
Intended to traverse through a user's given file content and find, collect all appropriate lines
that should be sent to the REPL in case of smart selection.
This could be exact statement such as just a single line print statement,
or a multiline dictionary, or differently styled multi-line list comprehension, etc.
Then call the normalize_lines function to normalize our smartly selected code block.
"""
parsed_file_content = None

try:
parsed_file_content = ast.parse(wholeFileContent)
parsed_file_content = ast.parse(whole_file_content)
except Exception:
# Handle case where user is attempting to run code where file contains deprecated Python code.
# Let typescript side know and show warning message.
Expand Down Expand Up @@ -192,8 +189,7 @@ def traverse_file(wholeFileContent, start_line, end_line, was_highlighted):
ast.ExceptHandler,
)
if isinstance(node, ast_types_with_nodebody) and isinstance(node.body, Iterable):
for child_nodes in node.body:
top_level_nodes.append(child_nodes)
top_level_nodes.extend(node.body)

exact_nodes = check_exact_exist(top_level_nodes, start_line, end_line)

Expand All @@ -202,7 +198,7 @@ def traverse_file(wholeFileContent, start_line, end_line, was_highlighted):
which_line_next = 0
for same_line_node in exact_nodes:
should_run_top_blocks.append(same_line_node)
smart_code += f"{ast.get_source_segment(wholeFileContent, same_line_node)}\n"
smart_code += f"{ast.get_source_segment(whole_file_content, same_line_node)}\n"
which_line_next = get_next_block_lineno(should_run_top_blocks)
return {
"normalized_smart_result": smart_code,
Expand All @@ -216,7 +212,7 @@ def traverse_file(wholeFileContent, start_line, end_line, was_highlighted):
if start_line == top_node.lineno and end_line == top_node.end_lineno:
should_run_top_blocks.append(top_node)

smart_code += f"{ast.get_source_segment(wholeFileContent, top_node)}\n"
smart_code += f"{ast.get_source_segment(whole_file_content, top_node)}\n"
break # If we found exact match, don't waste computation in parsing extra nodes.
elif start_line >= top_node.lineno and end_line <= top_node.end_lineno:
# Case to apply smart selection for multiple line.
Expand All @@ -231,7 +227,7 @@ def traverse_file(wholeFileContent, start_line, end_line, was_highlighted):

should_run_top_blocks.append(top_node)

smart_code += str(ast.get_source_segment(wholeFileContent, top_node))
smart_code += str(ast.get_source_segment(whole_file_content, top_node))
smart_code += "\n"

normalized_smart_result = normalize_lines(smart_code)
Expand Down Expand Up @@ -262,7 +258,7 @@ def get_next_block_lineno(which_line_next):
raw = stdin.read()
contents = json.loads(raw.decode("utf-8"))
# Empty highlight means user has not explicitly selected specific text.
empty_Highlight = contents.get("emptyHighlight", False)
empty_highlight = contents.get("emptyHighlight", False)

# We also get the activeEditor selection start line and end line from the typescript VS Code side.
# Remember to add 1 to each of the received since vscode starts line counting from 0 .
Expand All @@ -273,12 +269,12 @@ def get_next_block_lineno(which_line_next):
data = None
which_line_next = 0

if empty_Highlight and contents.get("smartSendSettingsEnabled"):
if empty_highlight and contents.get("smartSendSettingsEnabled"):
result = traverse_file(
contents["wholeFileContent"],
vscode_start_line,
vscode_end_line,
not empty_Highlight,
not empty_highlight,
)
normalized = result["normalized_smart_result"]
which_line_next = result["which_line_next"]
Expand Down
2 changes: 1 addition & 1 deletion python_files/printEnvVariables.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
import json
import os

print(json.dumps(dict(os.environ)))
2 changes: 1 addition & 1 deletion python_files/printEnvVariablesToFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
# Last argument is the target file into which we'll write the env variables line by line.
output_file = sys.argv[-1]

with open(output_file, "w") as outfile:
with open(output_file, "w") as outfile: # noqa: PTH123
for key, val in os.environ.items():
outfile.write(f"{key}={val}\n")
Loading
Loading