Skip to content

Commit

Permalink
fix(parse-utils): checking custom rules against file package names in…
Browse files Browse the repository at this point in the history
…stead (#225)
  • Loading branch information
RodrigoGonzalez authored Nov 10, 2023
1 parent 3cab9c0 commit 26829d9
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 16 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ repos:
types: [python]
stages: [commit, manual, push]

- id: tryceratops
name: tryceratops
entry: poetry run tryceratops
args: [--autofix]
language: system
types: [python]
stages: [commit, manual, push]
# - id: tryceratops
# name: tryceratops
# entry: poetry run tryceratops src
# args: [--autofix]
# language: system
# types: [python]
# stages: [commit, manual, push]

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
""" A basic module. """
from datetime import datetime
from uuid import UUID
from uuid import uuid4

from attrs import define
from attrs import field

import my_base_module.module_y
import my_second_base_package.module_one.file_one
from my_base_module.module_x import X
from my_second_base_package.module_three import ModuleThree


@define(slots=True)
class ModuleOne:
"""A basic class"""

_id: UUID | None = field(factory=uuid4)
_name: str | None = field(default="name")
_description: str | None = field(default="description")
_created_at: datetime | None = field(factory=datetime.now)

def id(self):
"""Get the id."""
return self._id

def name(self):
"""Get the name."""
return self._name

def description(self):
"""Get the description."""
return self._description

def created_at(self):
"""Get the created_at."""
return self._created_at

def initialize_class_x(self) -> X:
"""Initialize class x."""
return X(
name=f"{self._name}: X",
description=f"{self._description}: X",
)

def initialize_class_y(self) -> my_base_module.module_y.Y:
"""Initialize class y."""
return my_base_module.module_y.Y(
name=f"{self._name}: Y",
description=f"{self._description}: Y",
)

def initialize_class_one(self) -> my_second_base_package.module_one.file_one.ModuleOne:
"""Initialize class one."""
return my_second_base_package.module_one.file_one.ModuleOne(
name=f"{self._name}: ModuleOne",
description=f"{self._description}: ModuleOne",
)

def initialize_class_two(self) -> ModuleThree:
"""Initialize class two."""
return ModuleThree(
name=f"{self._name}: ModuleThree",
description=f"{self._description}: ModuleThree",
)
2 changes: 1 addition & 1 deletion src/flake8_custom_import_rules/core/import_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def match_custom_rule(instance: CustomImportRules) -> bool:
if custom_rule is None:
raise ValueError(f"Option key '{option_key}' not found in checker settings")

return does_file_match_custom_rule(instance.file_identifier, custom_rule)
return does_file_match_custom_rule(instance.file_packages, custom_rule)

return match_custom_rule

Expand Down
14 changes: 10 additions & 4 deletions src/flake8_custom_import_rules/utils/parse_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
""" Parse utils. """
import logging
import re

logger = logging.getLogger(__name__)


NOQA_INLINE_REGEXP = re.compile(
# We're looking for items that look like this:
# ``# noqa``
Expand Down Expand Up @@ -137,14 +141,16 @@ def check_string(
return False


def does_file_match_custom_rule(file_identifier: str, custom_rules: list[str] | str | None) -> bool:
def does_file_match_custom_rule(
file_packages: list[str] | str, custom_rules: list[str] | str | None
) -> bool:
"""
Check if a file identifier is in a custom rule.
Parameters
----------
file_identifier : str
The file identifier to check.
file_packages : list[str] | str
The file package identifiers to check.
custom_rules : list[str] | str | None
A list of custom rules or a single custom rule to check against.
Expand All @@ -155,7 +161,7 @@ def does_file_match_custom_rule(file_identifier: str, custom_rules: list[str] |
if custom_rules is None:
return False
custom_rules = [custom_rules] if isinstance(custom_rules, str) else custom_rules
return check_string(file_identifier, prefix=tuple(custom_rules), delimiter=" ")
return check_string(file_packages, substring_match=custom_rules, delimiter=" ")


def does_import_match_custom_import_restriction(
Expand Down
17 changes: 16 additions & 1 deletion tests/test_cases/custom_import_rules/base_package_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
["my_second_base_package.module_three"],
MODULE_THREE_ERRORS,
),
(
"example_repos/my_base_module/my_second_base_package_two/module_one.py",
["my_second_base_package"],
set(),
),
(
"example_repos/my_base_module/my_second_base_package_two/module_one.py",
["my_second_base_package.module_three"],
set(),
),
],
)
def test_base_package_only_imports(
Expand All @@ -55,9 +65,14 @@ def test_base_package_only_imports(
identifier = get_module_name_from_filename(filename)
root_package_name(identifier)
options = {
"base_packages": ["my_second_base_package", "my_base_module"],
"base_packages": ["my_third_base_package", "my_base_module", "my_second_base_package"],
"checker_settings": Settings(
**{
"BASE_PACKAGES": [
"my_second_base_package_two",
"my_base_module",
"my_second_base_package",
],
"BASE_PACKAGE_ONLY": base_package_only_imports,
"RESTRICT_DYNAMIC_IMPORTS": False,
"RESTRICT_LOCAL_SCOPE_IMPORTS": False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def test_standalone_modules_imports(
"base_packages": ["my_second_base_package", "my_base_module"],
"checker_settings": Settings(
**{
"BASE_PACKAGES": ["my_second_base_package", "my_base_module"],
"STANDALONE_MODULES": standalone_modules_imports,
"RESTRICT_DYNAMIC_IMPORTS": False,
"RESTRICT_LOCAL_SCOPE_IMPORTS": False,
Expand Down
9 changes: 6 additions & 3 deletions tests/utils/parse_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import pytest

from flake8_custom_import_rules.utils.node_utils import get_package_names
from flake8_custom_import_rules.utils.parse_utils import check_string
from flake8_custom_import_rules.utils.parse_utils import does_file_match_custom_rule
from flake8_custom_import_rules.utils.parse_utils import does_import_match_custom_import_restriction
Expand Down Expand Up @@ -95,7 +96,7 @@ def test_check_string(


@pytest.mark.parametrize(
("file_identifier", "custom_rules", "expected"),
("file_packages", "custom_rules", "expected"),
[
("my_base_package.package_c", PACKAGE_1, False),
("my_base_package.package_a", PACKAGE_1, True),
Expand All @@ -115,11 +116,13 @@ def test_check_string(
],
)
def test_does_file_match_custom_rule(
file_identifier: str, custom_rules: list[str], expected: bool
file_packages: str, custom_rules: list[str], expected: bool
) -> None:
"""Test does_file_match_custom_rule."""
assert (
does_file_match_custom_rule(file_identifier=file_identifier, custom_rules=custom_rules)
does_file_match_custom_rule(
file_packages=get_package_names(file_packages), custom_rules=custom_rules
)
== expected
)

Expand Down

0 comments on commit 26829d9

Please sign in to comment.