Skip to content

Commit

Permalink
Enable explicit Ruff check rules (#23741)
Browse files Browse the repository at this point in the history
Co-authored-by: Rafał <[email protected]>
  • Loading branch information
brettcannon and rafrafek authored Jul 9, 2024
1 parent a5c539d commit 462b9bf
Show file tree
Hide file tree
Showing 69 changed files with 893 additions and 1,046 deletions.
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

0 comments on commit 462b9bf

Please sign in to comment.