Skip to content

Commit

Permalink
Update to ruff==0.2.0, add new rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Feb 4, 2024
1 parent 106180d commit f2e7cb0
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 29 deletions.
35 changes: 25 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,39 @@ where = ["src"]

[tool.ruff]
line-length = 120
src = ["src"]
exclude = ["tests/codemodtestproj", "tests/stubstestproj", "tests/stubbing/testfiles"]
target-version = "py38"

[tool.ruff.lint]
preview = true
explicit-preview-rules = true
select = [
"E", # pycodestyle (E)
"W", # pycodestyle (W)
"F", # Pyflakes
"UP", # pyupgrade
"I", # isort
"PL", # Pylint
"E", # pycodestyle (E)
"W", # pycodestyle (W)
"F", # Pyflakes
"UP", # pyupgrade
"I", # isort
"PL", # Pylint
"RUF", # Ruff
"RUF022", # Ruff-preview
"YTT", # flake8-2020
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"T10", # flake8-debugger
"PIE", # flake8-pie
"T20", # flake8-print
"RSE", # flake8-raise
"PTH", # flake8-use-pathlib
]
ignore = [
"PLR0912",
"PLR0913",
"PLW2901",
"RUF012",
]
src = ["src"]
exclude = ["tests/codemodtestproj", "tests/stubstestproj", "tests/stubbing/testfiles"]
target-version = "py38"

[tool.ruff.isort]
[tool.ruff.lint.isort]
known-first-party = ["django_autotyping"]

[tool.pytest.ini_options]
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mypy-extensions==1.0.0
# via
# -c requirements/requirements.txt
# mypy
ruff==0.1.7
ruff==0.2.0
# via -r requirements/requirements-dev.in
sqlparse==0.4.4
# via
Expand Down
2 changes: 1 addition & 1 deletion src/django_autotyping/codemodding/codemods/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

__all__ = ("ForwardRelationTypingCodemod", "RulesT", "rules", "gather_codemods")
__all__ = ("ForwardRelationTypingCodemod", "RulesT", "gather_codemods", "rules")

from typing import Container, Literal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def _get_attribute_path(node: cst.Name | cst.Attribute) -> str:

if isinstance(node.value, cst.Attribute):
prefix = _get_attribute_path(node.value)
print("prefix", prefix)
else:
prefix = node.value.value
return f"{prefix}.{node.attr.value}"
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def handle(self, *args: Any, **options: Unpack[CommandOptions]) -> None:
codemods = gather_codemods(options["ignore"])

# TODO codemods should specify which type of file they apply to.
model_filenames = set(model_info.filename for model_info in django_context.model_infos)
model_filenames = {model_info.filename for model_info in django_context.model_infos}

for filename in model_filenames:
intput_source = Path(filename).read_text("utf-8")
Expand Down
6 changes: 3 additions & 3 deletions src/django_autotyping/stubbing/codemods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

__all__ = (
"AuthFunctionsCodemod",
"StubVisitorBasedCodemod",
"CallCommandCodemod",
"CreateOverloadCodemod",
"ForwardRelationOverloadCodemod",
"GetModelOverloadCodemod",
"QueryLookupsOverloadCodemod",
"ReverseOverloadCodemod",
"SettingCodemod",
"RulesT",
"rules",
"SettingCodemod",
"StubVisitorBasedCodemod",
"gather_codemods",
"rules",
)

RulesT: TypeAlias = Literal["DJAS001", "DJAS002", "DJAS003", "DJAS010", "DJAS011", "DJAS015", "DJAS016"]
Expand Down
1 change: 0 additions & 1 deletion src/django_autotyping/stubbing/codemods/_model_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,3 @@ def mutate_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.F
@abstractmethod
def get_self_annotation(self, model_name: str, class_name: str) -> cst.BaseExpression:
"""Return the annotation to be set on the `self` parameter."""
pass
4 changes: 2 additions & 2 deletions src/django_autotyping/stubbing/codemods/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def get_param(node: cst.FunctionDef, param_name: str) -> cst.Param:
"""Get the `Param` node matching `param_name`."""
try:
return next(param for param in node.params.params if param.name.value == param_name)
except StopIteration:
except StopIteration as e:
raise RuntimeError(
f"The `FunctionDef` node with name {node.name.value!r} does not have any parameter named {param_name!r}"
)
) from e


def get_kw_param(node: cst.FunctionDef, param_name: str) -> cst.Param:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def mutate_CallCommandFunctionDef(
old_node=get_param(overload, "command_name"), annotation=cst.Annotation(cst.Name("BaseCommand"))
)

return cst.FlattenSentinel(overloads + [fallback_overload])
return cst.FlattenSentinel([*overloads, fallback_overload])

def _mutate_CallCommandFunctionDef(
self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef
Expand Down Expand Up @@ -186,4 +186,4 @@ def _mutate_CallCommandFunctionDef(
old_node=get_param(overload, "command_name"), annotation=cst.Annotation(cst.Name("BaseCommand"))
)

return cst.FlattenSentinel(overloads + [fallback_overload])
return cst.FlattenSentinel([*overloads, fallback_overload])
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ def mutate_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.F
annotation=cst.Annotation(helpers.parse_template_expression("Callable[..., Any] | None")),
)

return cst.FlattenSentinel(overloads + [overload])
return cst.FlattenSentinel([*overloads, overload])
4 changes: 2 additions & 2 deletions src/django_autotyping/stubbing/codemods/settings_codemod.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def mutate_ClassDef(self, original_node: cst.ClassDef, updated_node: cst.ClassDe

for setting_name, setting_typing_conf in GLOBAL_SETTINGS.items():
if (
setting_typing_conf.get("no_default")
and setting_name not in all_settings
(setting_typing_conf.get("no_default")
and setting_name not in all_settings)
or setting_typing_conf.get("added_in", (0,)) > DJANGO_VERSION
or setting_typing_conf.get("removed_in", (float("inf"),)) <= DJANGO_VERSION
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ def _iter_actions(
yield from _iter_actions(
parser=subparser_action.choices[act.dest],
is_subparser=True,
parent_args=arg_infos
+ [
parent_args=[
*arg_infos,
ArgInfo(
nargs=None,
dest=subparser_action.dest if subparser_action.dest != SUPPRESS else None,
subparser_arg=act.dest,
)
),
],
parent_options=options,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def typeddict_name(self) -> str:

def is_mergeable(self, arguments: dict[str, bool]) -> bool:
"""Return whether the keys of the provided arguments are the same as the current instance."""
return set(arg[0] for arg in self.arguments) == set(arguments)
return {arg[0] for arg in self.arguments} == set(arguments)

def with_new_arguments(self, arguments: dict[str, bool]) -> PathArguments:
new_arguments = frozenset((k, False if not arguments[k] else is_required) for k, is_required in self.arguments)
Expand Down

0 comments on commit f2e7cb0

Please sign in to comment.