Skip to content

Commit

Permalink
chore(deps): update ruff to 0.1.1 and fix lint issues (#4417)
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau authored Oct 25, 2023
1 parent 927f66c commit 4b1cff5
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 36 deletions.
15 changes: 14 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ ignore = [
"C405", "C408", "C414",
"Q000", # 2 single-quoted strings - probably accidental
"RET504", "RET506", # Return value related.

"PLR2004", # Magic values - widely used
"PLC1901", # Checking for empty string vs. falsey - many of these
"S603", # Untrusted input for subprocess calls
"S604", # shell=True parameter to a function
"S607", # Partial executable path for subprocess calls
]

[tool.ruff.pylint]
max-args = 6
max-branches = 16

[tool.ruff.per-file-ignores]
"tests/**.py" = [
"D", # Ignore docstring rules in tests
Expand All @@ -163,6 +171,11 @@ ignore = [
"S105", # Allow Possible hardcoded password.
"S106", # Allow Possible hardcoded password.
"S108", # Allow Probable insecure usage of temporary file or directory.
"PLR0913", # Allow many arguments to tests
]
"tests/unit/parts/plugins/test_kernel.py" = [
"E101", # Mixed tabs and spaces. Ruff gets confused by tabs in multiline strings
"W191", # Indentation contains tabs - another Ruff false positive
]
"__init__.py" = ["I001"] # Imports in __init__ filesare allowed to be out of order

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def recursive_data_files(directory, install_directory):
"pytest-cov",
"pytest-mock",
"pytest-subprocess",
"ruff==0.0.220",
"ruff~=0.1.1",
"tox>=4.5",
"types-PyYAML",
"types-requests",
Expand Down
6 changes: 3 additions & 3 deletions snapcraft/commands/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def run(self, parsed_args):
)

# Extensions from snapcraft_legacy.
for extension_name in supported_extension_names():
extension_class = find_extension(extension_name)
extension_name = extension_name.replace("_", "-")
for _extension_name in supported_extension_names():
extension_class = find_extension(_extension_name)
extension_name = _extension_name.replace("_", "-")
extension_bases = list(extension_class.get_supported_bases())
if extension_name in extension_presentation:
extension_presentation[extension_name].bases += extension_bases
Expand Down
8 changes: 4 additions & 4 deletions snapcraft/elf/_elf_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class _Library:
:param soname_cache: The soname cache manager.
"""

def __init__(
def __init__( # noqa PLR0913
self,
*,
soname: str,
Expand Down Expand Up @@ -338,10 +338,10 @@ def get_required_glibc(self) -> str:

version_required = ""
for lib in self.needed.values():
for version in lib.versions:
if not version.startswith("GLIBC_"):
for _version in lib.versions:
if not _version.startswith("GLIBC_"):
continue
version = version[6:]
version = _version[6:]
# TODO: pkg_resources is deprecated in setuptools>66 (CRAFT-1598)
if parse_version(version) > parse_version(version_required): # type: ignore
version_required = version
Expand Down
4 changes: 2 additions & 2 deletions snapcraft/meta/appstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ def _get_transformed_dom(path: str):

def _get_dom(path: str) -> lxml.etree.ElementTree:
try:
return lxml.etree.parse(path)
return lxml.etree.parse(path) # noqa S320
except OSError as err:
raise errors.SnapcraftError(str(err)) from err
except lxml.etree.ParseError as err:
raise errors.MetadataExtractionError(path, str(err)) from err


def _get_xslt():
xslt = lxml.etree.parse(StringIO(_XSLT))
xslt = lxml.etree.parse(StringIO(_XSLT)) # noqa S320
return lxml.etree.XSLT(xslt)


Expand Down
2 changes: 1 addition & 1 deletion snapcraft/meta/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Config: # pylint: disable=too-few-public-methods
alias_generator = lambda s: s.replace("_", "-") # noqa: E731


def write(
def write( # noqa PLR0913
project: Project,
prime_dir: Path,
*,
Expand Down
4 changes: 2 additions & 2 deletions snapcraft/meta/snap_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ def get_content_dirs(self, installed_path: Path) -> Set[Path]:
"""Obtain the slot's content directories."""
content_dirs: Set[Path] = set()

for path in self.read + self.write:
for path_ in self.read + self.write:
# Strip leading "$SNAP" and "/".
path = re.sub(r"^\$SNAP", "", path)
path = re.sub(r"^\$SNAP", "", path_)
path = re.sub(r"^/", "", path)
path = re.sub(r"^./", "", path)
content_dirs.add(installed_path / path)
Expand Down
10 changes: 5 additions & 5 deletions snapcraft/parts/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ def process_parts(
:param yaml_data: unprocessed snapcraft.yaml.
:returns: process snapcraft.yaml.
"""

def self_check(value: Any) -> bool:
return value == value # pylint: disable=comparison-with-itself # noqa PLR0124

# TODO: make checker optional in craft-grammar.
processor = GrammarProcessor(
arch=arch,
target_arch=target_arch,
checker=lambda x: x == x, # pylint: disable=comparison-with-itself
)
processor = GrammarProcessor(arch=arch, target_arch=target_arch, checker=self_check)

for part_name in parts_yaml_data:
parts_yaml_data[part_name] = process_part(
Expand Down
6 changes: 3 additions & 3 deletions snapcraft/parts/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def run(command_name: str, parsed_args: "argparse.Namespace") -> None:
)


def _run_command(
def _run_command( # noqa PLR0913
command_name: str,
*,
project: Project,
Expand Down Expand Up @@ -217,7 +217,7 @@ def _run_command(
raise errors.SnapcraftError(str(err)) from err


def _run_lifecycle_and_pack(
def _run_lifecycle_and_pack( # noqa PLR0913
lifecycle: PartsLifecycle,
*,
command_name: str,
Expand Down Expand Up @@ -368,7 +368,7 @@ def _clean_provider(project: Project, parsed_args: "argparse.Namespace") -> None


# pylint: disable-next=too-many-branches, too-many-statements
def _run_in_provider(
def _run_in_provider( # noqa PLR0915
project: Project, command_name: str, parsed_args: "argparse.Namespace"
) -> None:
"""Pack image in provider instance."""
Expand Down
4 changes: 2 additions & 2 deletions snapcraft/parts/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PartsLifecycle:
:raises PartsLifecycleError: On error initializing the parts lifecycle.
"""

def __init__(
def __init__( # noqa PLR0913
self,
all_parts: Dict[str, Any],
*,
Expand Down Expand Up @@ -177,7 +177,7 @@ def run(
for action in actions:
# Workaround until canonical/craft-parts#540 is fixed
if action.step == target_step and rerun_step:
action = craft_parts.Action(
action = craft_parts.Action( # noqa PLW2901
part_name=action.part_name,
step=action.step,
action_type=ActionType.RERUN,
Expand Down
8 changes: 4 additions & 4 deletions snapcraft/parts/plugins/_ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _parse_rosdep_resolve_dependencies(
dependencies: Dict[str, Set[str]] = {}
dependency_set = None
for line in lines:
line = line.strip()
line = line.strip() # noqa PLW2901
if line.startswith("#"):
key = line.strip("# ")
dependencies[key] = set()
Expand Down Expand Up @@ -223,7 +223,7 @@ def _get_stage_runtime_dependencies_commands(self) -> List[str]:
@overrides
def get_build_commands(self) -> List[str]:
return (
[
[ # noqa S608 (false positive on SQL injection)
"if [ ! -f /etc/ros/rosdep/sources.list.d/20-default.list ]; then",
# Preserve http(s)_proxy env var in root for remote-build proxy since rosdep
# doesn't support proxy
Expand Down Expand Up @@ -317,7 +317,7 @@ def get_installed_dependencies(installed_packages_path: str) -> Set[str]:
@click.option("--target-arch", envvar="CRAFT_TARGET_ARCH", required=True)
@click.option("--stage-cache-dir", required=True)
@click.option("--base", required=True)
def stage_runtime_dependencies(
def stage_runtime_dependencies( # noqa: PLR0913 (too many arguments)
part_src: str,
part_install: str,
ros_version: str,
Expand All @@ -336,7 +336,7 @@ def stage_runtime_dependencies(
catkin_packages.find_packages(part_install).values(),
)
for pkg in catkin_packages.find_packages(part_src).values():
pkg = cast(catkin_pkg.package.Package, pkg)
pkg = cast(catkin_pkg.package.Package, pkg) # noqa PLW2901
# Evaluate the conditions of all dependencies
pkg.evaluate_conditions(
{
Expand Down
2 changes: 1 addition & 1 deletion snapcraft/store/_legacy_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def store_credentials(cls, config_content) -> None:
cls.CONFIG_PATH.write_text(config_content)

@overrides
def __init__(
def __init__( # noqa PLR0913
self,
*,
base_url: str,
Expand Down
2 changes: 1 addition & 1 deletion snapcraft/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_os_platform(
else:
os_release = {}
for line in lines:
line = line.strip()
line = line.strip() # noqa PLW2901
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.rstrip().split("=", 1)
Expand Down
13 changes: 13 additions & 0 deletions snapcraft_legacy/ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,18 @@ select = [
"W",
]

[per-file-ignores]
"snapcraft_legacy/plugins/v2/_kernel_build.py" = [
"E101", # Mixed tabs and spaces. Ruff gets confused by tabs in multiline strings
"W191", # Indentation contains tabs - another Ruff false positive
]
"tests/legacy/unit/plugins/v2/test_kernel.py" = [
"E101", # Mixed tabs and spaces. Ruff gets confused by tabs in multiline strings
"W191", # Indentation contains tabs - another Ruff false positive
]
"tests/legacy/**.py" = [
"E721", # Allowing type comparison
]

[mccabe]
max-complexity = 10
2 changes: 1 addition & 1 deletion tests/legacy/unit/meta/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ def make_snapcraft_project(self, common_id):
source: .
plugin: dump
parse-info: ["1.metainfo.xml", "2.metainfo.xml"]
"""
)

Expand Down
4 changes: 3 additions & 1 deletion tests/unit/elf/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def fake_tools(new_dir, monkeypatch):
monkeypatch.setenv("PATH", f"{bin_path!s}:{os.getenv('PATH')}")


def _fake_elffile_extract_attributes(self): # pylint: disable=too-many-statements
def _fake_elffile_extract_attributes( # noqa: PLR0915
self,
): # pylint: disable=too-many-statements
"""Mock method definition for ElfFile._extract_attributes()."""
name = self.path.name

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/parts/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
_PROCESSOR = GrammarProcessor(
arch="amd64",
target_arch="amd64",
checker=lambda x: x == x, # pylint: disable=comparison-with-itself
checker=lambda x: x == x, # pylint: disable=comparison-with-itself # noqa PLR0124
)
GrammarEntry = namedtuple("GrammarEntry", ["value", "expected"])

Expand Down
4 changes: 2 additions & 2 deletions tools/brew_install_from_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def download_snapcraft_source(dest_dir):
os.environ.get("TRAVIS_PULL_REQUEST_BRANCH") or "master",
)
print("Downloading branch source from {}".format(branch_source))
urllib.request.urlretrieve(branch_source, dest_file)
urllib.request.urlretrieve(branch_source, dest_file) # noqa S310
return dest_file


Expand All @@ -68,7 +68,7 @@ def download_brew_formula(destination_path):
"https://raw.githubusercontent.com/Homebrew/homebrew-core/master/"
"Formula/snapcraft.rb"
)
urllib.request.urlretrieve(brew_formula_url, destination_path)
urllib.request.urlretrieve(brew_formula_url, destination_path) # noqa S310


def patch_brew_formula_source(
Expand Down
4 changes: 3 additions & 1 deletion tools/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def determine_version():
subprocess.run(
["git", "describe", "--always", "--long"],
stdout=subprocess.PIPE,
check=False,
text=True,
)
.stdout.decode()
.stdout
.strip()
)

Expand Down

0 comments on commit 4b1cff5

Please sign in to comment.