python-checks 0.11.0__tar.gz → 0.12.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. {python_checks-0.11.0 → python_checks-0.12.0}/PKG-INFO +2 -2
  2. {python_checks-0.11.0 → python_checks-0.12.0}/pyproject.toml +3 -2
  3. {python_checks-0.11.0 → python_checks-0.12.0}/pyproject.toml.orig +6 -2
  4. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/_kind.py +43 -1
  5. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_model_columns.py +42 -40
  6. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_statement_keys.py +8 -2
  7. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/placement/_operation_shape.py +17 -25
  8. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/placement/_required_class.py +22 -29
  9. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_callers.py +54 -12
  10. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_function_length.py +6 -2
  11. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_functions.py +0 -5
  12. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_keyword_only.py +5 -2
  13. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/types/_annotation_shapes.py +6 -18
  14. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/types/_constant_annotations.py +3 -10
  15. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_mutation.py +23 -1
  16. python_checks-0.12.0/src/py_checks/mutation/_cache.py +250 -0
  17. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/_gate.py +113 -6
  18. python_checks-0.12.0/src/py_checks/mutation/_mutmut.py +230 -0
  19. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/_process.py +9 -3
  20. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/_scope.py +5 -8
  21. python_checks-0.11.0/src/py_checks/mutation/_mutmut.py +0 -162
  22. {python_checks-0.11.0 → python_checks-0.12.0}/LICENSE +0 -0
  23. {python_checks-0.11.0 → python_checks-0.12.0}/README.md +0 -0
  24. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/__init__.py +0 -0
  25. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/__init__.py +0 -0
  26. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/_layout.py +0 -0
  27. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/_location.py +0 -0
  28. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/_names.py +0 -0
  29. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/api/__init__.py +0 -0
  30. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/api/_edge_declarations.py +0 -0
  31. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/api/_marker.py +0 -0
  32. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/calls/__init__.py +0 -0
  33. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/calls/_confined_functions.py +0 -0
  34. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/calls/_marker.py +0 -0
  35. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/__init__.py +0 -0
  36. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_bound_checks.py +0 -0
  37. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_confined_calls.py +0 -0
  38. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_marker.py +0 -0
  39. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_model_boundary.py +0 -0
  40. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_raw_sql.py +0 -0
  41. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/database/_schema_drift.py +0 -0
  42. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/effects/__init__.py +0 -0
  43. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/effects/_determinism.py +0 -0
  44. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/effects/_log_events.py +0 -0
  45. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/effects/_marker.py +0 -0
  46. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/errors/__init__.py +0 -0
  47. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/errors/_marker.py +0 -0
  48. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/errors/_refusals.py +0 -0
  49. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/hygiene/__init__.py +0 -0
  50. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/hygiene/_dependency_bounds.py +0 -0
  51. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/hygiene/_marker.py +0 -0
  52. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/hygiene/_translations.py +0 -0
  53. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/imports/__init__.py +0 -0
  54. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/imports/_confined.py +0 -0
  55. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/imports/_marker.py +0 -0
  56. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/imports/_sealed.py +0 -0
  57. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/imports/_statements.py +0 -0
  58. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/placement/__init__.py +0 -0
  59. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/placement/_class_modules.py +0 -0
  60. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/placement/_class_placement.py +0 -0
  61. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/placement/_marker.py +0 -0
  62. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/__init__.py +0 -0
  63. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_marker.py +0 -0
  64. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_module_length.py +0 -0
  65. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_nesting.py +0 -0
  66. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_signature_layout.py +0 -0
  67. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_stops.py +0 -0
  68. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/signatures/_targets.py +0 -0
  69. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/types/__init__.py +0 -0
  70. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/types/_config_fields.py +0 -0
  71. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/types/_confined_types.py +0 -0
  72. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/types/_frozen_dataclasses.py +0 -0
  73. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/checks/types/_marker.py +0 -0
  74. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/__init__.py +0 -0
  75. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/_app.py +0 -0
  76. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/_protocols.py +0 -0
  77. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/__init__.py +0 -0
  78. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_doctor.py +0 -0
  79. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_explain.py +0 -0
  80. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_list.py +0 -0
  81. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_run.py +0 -0
  82. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_summary.py +0 -0
  83. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_sync.py +0 -0
  84. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/cli/commands/_translations.py +0 -0
  85. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/config/__init__.py +0 -0
  86. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/config/_base.py +0 -0
  87. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/config/_config.py +0 -0
  88. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/config/_constants.py +0 -0
  89. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/config/_errors.py +0 -0
  90. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/config/_loader.py +0 -0
  91. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/config/_toml.py +0 -0
  92. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/contracts/__init__.py +0 -0
  93. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/contracts/_constants.py +0 -0
  94. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/contracts/_layout.py +0 -0
  95. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/contracts/_render.py +0 -0
  96. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/contracts/_settings.py +0 -0
  97. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/__init__.py +0 -0
  98. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_constants.py +0 -0
  99. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_discovery.py +0 -0
  100. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_edit.py +0 -0
  101. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_errors.py +0 -0
  102. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_fixer.py +0 -0
  103. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_format.py +0 -0
  104. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_markers.py +0 -0
  105. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_paths.py +0 -0
  106. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_protocols.py +0 -0
  107. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_registry.py +0 -0
  108. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_report.py +0 -0
  109. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_runner.py +0 -0
  110. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_settings.py +0 -0
  111. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_source.py +0 -0
  112. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/core/_violation.py +0 -0
  113. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/environment/__init__.py +0 -0
  114. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/environment/_constants.py +0 -0
  115. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/environment/_render.py +0 -0
  116. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/environment/_settings.py +0 -0
  117. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/__init__.py +0 -0
  118. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/_baseline.py +0 -0
  119. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/_constants.py +0 -0
  120. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/_errors.py +0 -0
  121. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/mutation/_settings.py +0 -0
  122. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/py.typed +0 -0
  123. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/sync/__init__.py +0 -0
  124. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/sync/_sync.py +0 -0
  125. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/translations/__init__.py +0 -0
  126. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/translations/_constants.py +0 -0
  127. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/translations/_errors.py +0 -0
  128. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/translations/_settings.py +0 -0
  129. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/translations/_stamp.py +0 -0
  130. {python_checks-0.11.0 → python_checks-0.12.0}/src/py_checks/translations/_tree.py +0 -0
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-checks
3
- Version: 0.11.0
3
+ Version: 0.12.0
4
4
  Summary: Проверки архитектурных соглашений проекта
5
5
  Author: Armontex
6
6
  Author-email: Armontex <windle1337@gmail.com>
7
7
  License-Expression: MIT
8
8
  License-File: LICENSE
9
- Requires-Dist: jedi>=0.19.2,<0.20
9
+ Requires-Dist: jedi>=0.19.2,<0.21
10
10
  Requires-Dist: libcst>=1.9,<2
11
11
  Requires-Dist: pathspec>=1.1,<2
12
12
  Requires-Dist: pydantic>=2.13.5,<3
@@ -1,13 +1,13 @@
1
1
  [project]
2
2
  name = "python-checks"
3
- version = "0.11.0"
3
+ version = "0.12.0"
4
4
  description = "Проверки архитектурных соглашений проекта"
5
5
  readme = "README.md"
6
6
  license = "MIT"
7
7
  license-files = ["LICENSE"]
8
8
  requires-python = ">=3.14"
9
9
  dependencies = [
10
- "jedi>=0.19.2,<0.20",
10
+ "jedi>=0.19.2,<0.21",
11
11
  "libcst>=1.9,<2",
12
12
  "pathspec>=1.1,<2",
13
13
  "pydantic>=2.13.5,<3",
@@ -77,6 +77,7 @@ dev = [
77
77
  "actionlint-py>=1.7.12,<2",
78
78
  "commitizen>=4.10,<5",
79
79
  "import-linter>=2.15,<3",
80
+ "jscpd>=5.4.2,<6",
80
81
  "pre-commit>=4.6.2,<5",
81
82
  "pyright>=1.1.414,<2",
82
83
  "pytest>=9.1.1,<10",
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "python-checks"
3
- version = "0.11.0"
3
+ version = "0.12.0"
4
4
  description = "Проверки архитектурных соглашений проекта"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -11,7 +11,7 @@ dependencies = [
11
11
  # `--fix` у keyword-only-arguments: чей это вызов — наш, чужой или ничей.
12
12
  # Без вывода типов `self._repo.get(x)` не отличить от `dict.get`, а вызов,
13
13
  # который не разрешился, останавливает правку, а не ломает его.
14
- "jedi>=0.19.2,<0.20",
14
+ "jedi>=0.19.2,<0.21",
15
15
  "libcst>=1.9,<2",
16
16
  # gitignore-шаблоны `[translations].files`: `**`, ведущий `/`, директории и
17
17
  # отрицание — краёв столько, что самописный разбор ошибся бы на части из них.
@@ -92,6 +92,10 @@ dev = [
92
92
  "actionlint-py>=1.7.12,<2",
93
93
  "commitizen>=4.10,<5",
94
94
  "import-linter>=2.15,<3",
95
+ # Бинарник Rust в колесе, Node.js не нужен. Пол на версии, на которой
96
+ # режим проверен: до 5.4.0 включительно `similarity = 1` выключал поиск,
97
+ # а не требовал ту же структуру.
98
+ "jscpd>=5.4.2,<6",
95
99
  "pre-commit>=4.6.2,<5",
96
100
  "pyright>=1.1.414,<2",
97
101
  "pytest>=9.1.1,<10",
@@ -15,7 +15,7 @@ from typing import TYPE_CHECKING, Final
15
15
  from py_checks.checks._names import name, names
16
16
 
17
17
  if TYPE_CHECKING:
18
- from collections.abc import Iterator
18
+ from collections.abc import Iterator, Sequence
19
19
 
20
20
  ABSTRACT: Final[frozenset[str]] = frozenset({"Protocol", "ABC"})
21
21
  ABSTRACT_METACLASS: Final = "ABCMeta"
@@ -118,6 +118,12 @@ def declarations(*, tree: ast.Module) -> Iterator[Declaration]:
118
118
  kind=Kind.FUNCTION,
119
119
  node=node,
120
120
  )
121
+ case ast.TypeAlias(name=ast.Name(id=name)):
122
+ yield Declaration(
123
+ name=name,
124
+ kind=Kind.ALIAS,
125
+ node=node,
126
+ )
121
127
  case ast.AnnAssign(target=ast.Name(id=name)) | ast.Assign(targets=[ast.Name(id=name)]):
122
128
  if _alias(node=node):
123
129
  yield Declaration(
@@ -129,6 +135,25 @@ def declarations(*, tree: ast.Module) -> Iterator[Declaration]:
129
135
  continue
130
136
 
131
137
 
138
+ def enums(*, tree: ast.Module) -> frozenset[str]:
139
+ """The module's classes that are enums: on an `Enum` base, or on one of these.
140
+
141
+ `class OrderStatus(WireStatus)` is an enum when `WireStatus(StrEnum)` is
142
+ declared in the same file — a shared `_missing_` or `__str__` on a base
143
+ enum. A base imported from another module cannot be read from here.
144
+ """
145
+ classes = [node for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
146
+ found: set[str] = set()
147
+ grown = True
148
+ while grown:
149
+ grown = False
150
+ for node in classes:
151
+ if node.name not in found and frozenset(names(nodes=node.bases)) & (ENUMS | found):
152
+ found.add(node.name)
153
+ grown = True
154
+ return frozenset(found)
155
+
156
+
132
157
  def _class(*, node: ast.ClassDef) -> Kind | None:
133
158
  """The class's kind; `None` if one file does not show it.
134
159
 
@@ -184,3 +209,20 @@ def _alias(*, node: ast.AnnAssign | ast.Assign) -> bool:
184
209
  return True
185
210
  case _:
186
211
  return False
212
+
213
+
214
+ def signature_end(
215
+ *,
216
+ node: ast.FunctionDef | ast.AsyncFunctionDef,
217
+ lines: Sequence[str],
218
+ ) -> int:
219
+ """The last line of the signature: the mark goes there when the signature is in a column.
220
+
221
+ The body usually starts on the line after the `:`. A stub may share that
222
+ line — ruff writes `) -> None: ...` — and then it is the signature's end.
223
+ """
224
+ first = node.body[0]
225
+ # `col_offset` counts utf-8 bytes, so the line is cut as bytes.
226
+ if lines[first.lineno - 1].encode("utf-8")[: first.col_offset].strip():
227
+ return first.lineno
228
+ return max(first.lineno - 1, node.lineno)
@@ -6,7 +6,7 @@ import ast
6
6
  from typing import TYPE_CHECKING, ClassVar, Final
7
7
 
8
8
  from py_checks.checks._location import ZonedSettings, zoned
9
- from py_checks.checks._names import name
9
+ from py_checks.checks._names import name, walked
10
10
  from py_checks.checks.database._marker import MARKER
11
11
  from py_checks.core import Scope, Violation, settings_as
12
12
 
@@ -23,6 +23,8 @@ AWARE: Final = "timezone"
23
23
  NULLABLE: Final = "nullable"
24
24
  RELATIONSHIP: Final = "relationship"
25
25
  LAZY: Final = "lazy"
26
+ OPTIONAL: Final = "Optional"
27
+ UNION: Final = "Union"
26
28
 
27
29
 
28
30
  class ModelColumnsSettings(ZonedSettings):
@@ -128,18 +130,20 @@ class ModelColumns:
128
130
  """The material, and whatever the column fills itself in with."""
129
131
  written = name(node=node.func)
130
132
  if written in limits.instead and limits.wrappers.get(written) != file.path.stem:
131
- yield cls._says(
132
- file=file,
133
+ yield Violation.from_node(
133
134
  node=node,
135
+ path=file.path,
136
+ code=CODE,
134
137
  message=limits.instead[written],
135
138
  )
136
139
  if written in limits.aware and not cls._said(
137
140
  node=node,
138
141
  named=AWARE,
139
142
  ):
140
- yield cls._says(
141
- file=file,
143
+ yield Violation.from_node(
142
144
  node=node,
145
+ path=file.path,
146
+ code=CODE,
143
147
  message=(
144
148
  f"{written} without timezone=True stores a naive timestamp; say timezone=True"
145
149
  ),
@@ -154,9 +158,10 @@ class ModelColumns:
154
158
  return
155
159
  for keyword in node.keywords:
156
160
  if keyword.arg in limits.defaults:
157
- yield cls._says(
158
- file=file,
161
+ yield Violation.from_node(
159
162
  node=node,
163
+ path=file.path,
164
+ code=CODE,
160
165
  message=(
161
166
  f"{keyword.arg}= fills the column in for a writer who did not write it; "
162
167
  f"pass the value in the statement"
@@ -176,9 +181,10 @@ class ModelColumns:
176
181
  if isinstance(written, ast.Constant) and written.value in allowed:
177
182
  return
178
183
  said = "without lazy=" if written is None else f"with lazy={ast.unparse(written)}"
179
- yield cls._says(
180
- file=file,
184
+ yield Violation.from_node(
181
185
  node=node,
186
+ path=file.path,
187
+ code=CODE,
182
188
  message=(
183
189
  f"relationship() {said} loads without the query saying so; "
184
190
  f"say lazy={allowed[0]!r} and load it at the query"
@@ -197,8 +203,8 @@ class ModelColumns:
197
203
  inner = cls._mapped(node=node.annotation)
198
204
  if inner is None:
199
205
  return
200
- written, optional = inner
201
- if written in limits.skip:
206
+ written = next((one for one in walked(node=inner) if one in limits.skip), None)
207
+ if written is not None:
202
208
  yield Violation.from_node(
203
209
  node=node,
204
210
  path=file.path,
@@ -211,7 +217,7 @@ class ModelColumns:
211
217
  node=node.value,
212
218
  limits=limits,
213
219
  )
214
- if said is not None and said != optional:
220
+ if said is not None and said != cls._optional(node=inner):
215
221
  yield Violation.from_node(
216
222
  node=node,
217
223
  path=file.path,
@@ -236,22 +242,29 @@ class ModelColumns:
236
242
  return bool(keyword.value.value)
237
243
  return None
238
244
 
239
- @classmethod
240
- def _mapped(cls, *, node: ast.expr) -> tuple[str, bool] | None:
241
- """The name inside `Mapped[...]`, and whether it allows `None`."""
245
+ @staticmethod
246
+ def _mapped(*, node: ast.expr) -> ast.expr | None:
247
+ """What is inside `Mapped[...]`."""
242
248
  if not isinstance(node, ast.Subscript) or name(node=node.value) != MAPPED:
243
249
  return None
244
- match node.slice:
245
- case ast.Name(id=inside) | ast.Attribute(attr=inside):
246
- return inside, False
247
- case ast.BinOp(left=ast.Name(id=inside), op=ast.BitOr(), right=right):
248
- return inside, cls._none(node=right)
249
- case _:
250
- return None
250
+ return node.slice
251
251
 
252
- @staticmethod
253
- def _none(*, node: ast.expr) -> bool:
254
- return isinstance(node, ast.Constant) and node.value is None
252
+ @classmethod
253
+ def _optional(cls, *, node: ast.expr) -> bool:
254
+ """Whether the type allows `None`: `X | None`, `None | X`, `Optional[X]`, `Union[...]`."""
255
+ match node:
256
+ case ast.Constant(value=None):
257
+ return True
258
+ case ast.BinOp(left=left, op=ast.BitOr(), right=right):
259
+ return cls._optional(node=left) or cls._optional(node=right)
260
+ case ast.Subscript(value=outer) if name(node=outer) == OPTIONAL:
261
+ return True
262
+ case ast.Subscript(value=outer, slice=ast.Tuple(elts=arms)) if (
263
+ name(node=outer) == UNION
264
+ ):
265
+ return any(cls._optional(node=arm) for arm in arms)
266
+ case _:
267
+ return False
255
268
 
256
269
  @staticmethod
257
270
  def _said(
@@ -259,21 +272,10 @@ class ModelColumns:
259
272
  node: ast.Call,
260
273
  named: str,
261
274
  ) -> bool:
275
+ """Whether the keyword is the literal `True`: nothing short of it says what is stored."""
262
276
  return any(
263
- keyword.arg == named and keyword.value != ast.Constant(value=False)
277
+ keyword.arg == named
278
+ and isinstance(keyword.value, ast.Constant)
279
+ and keyword.value.value is True
264
280
  for keyword in node.keywords
265
281
  )
266
-
267
- @staticmethod
268
- def _says(
269
- *,
270
- file: ParsedFile,
271
- node: ast.Call,
272
- message: str,
273
- ) -> Violation:
274
- return Violation.from_node(
275
- node=node,
276
- path=file.path,
277
- code=CODE,
278
- message=message,
279
- )
@@ -125,8 +125,14 @@ class StatementKeys:
125
125
  node: ast.For | ast.AsyncFor | ast.While,
126
126
  limits: StatementKeysSettings,
127
127
  ) -> Iterator[Violation]:
128
- """A trip to the database on every iteration."""
129
- for child in ast.walk(node):
128
+ """A trip to the database on every iteration.
129
+
130
+ Only what runs on every iteration counts: the body, and a `while`'s
131
+ test. A `for`'s iterable is evaluated once, before the first one, and
132
+ its `else` once, after the last.
133
+ """
134
+ repeated = [*node.body, *([node.test] if isinstance(node, ast.While) else [])]
135
+ for child in (inner for part in repeated for inner in ast.walk(part)):
130
136
  if not isinstance(child, ast.Call) or name(node=child.func) not in limits.loops:
131
137
  continue
132
138
  yield Violation.from_node(
@@ -6,7 +6,7 @@ import ast
6
6
  from dataclasses import dataclass
7
7
  from typing import TYPE_CHECKING, ClassVar, Final
8
8
 
9
- from py_checks.checks._kind import Kind, declarations
9
+ from py_checks.checks._kind import Kind, declarations, signature_end
10
10
  from py_checks.checks._layout import SECTION, Layout, innermost
11
11
  from py_checks.checks._location import place
12
12
  from py_checks.checks.placement._marker import MARKER
@@ -223,18 +223,20 @@ class OperationShape:
223
223
  if one.kind is Kind.ALIAS or one is subject:
224
224
  continue
225
225
  if one.kind is Kind.FUNCTION:
226
- yield cls._says(
227
- file=file,
228
- declared=one,
226
+ yield Violation.from_node(
227
+ node=one.node,
228
+ path=file.path,
229
+ code=CODE,
229
230
  message=(
230
231
  f"{one.name}() stands beside the operation; a helper it needs is "
231
232
  f"a private method, one that two need is shared code"
232
233
  ),
233
234
  )
234
235
  continue
235
- yield cls._says(
236
- file=file,
237
- declared=one,
236
+ yield Violation.from_node(
237
+ node=one.node,
238
+ path=file.path,
239
+ code=CODE,
238
240
  message=(
239
241
  f"{one.name} stands beside the operation; in {rule.address} a module "
240
242
  f"declares one class and nothing else"
@@ -258,9 +260,10 @@ class OperationShape:
258
260
  if names == [rule.method]:
259
261
  return
260
262
  if not public:
261
- yield cls._says(
262
- file=file,
263
- declared=subject,
263
+ yield Violation.from_node(
264
+ node=subject.node,
265
+ path=file.path,
266
+ code=CODE,
264
267
  message=(
265
268
  f"{subject.name} has no public method; an operation is asked "
266
269
  f"through one, and it is called {rule.method}()"
@@ -303,7 +306,10 @@ class OperationShape:
303
306
  f"{rule.max_arguments}; pass a command, a query or a DTO"
304
307
  ),
305
308
  # The mark is accepted on any line of the signature.
306
- end_line=max(method.body[0].lineno - 1, method.lineno),
309
+ end_line=signature_end(
310
+ node=method,
311
+ lines=file.lines,
312
+ ),
307
313
  )
308
314
 
309
315
  @classmethod
@@ -427,17 +433,3 @@ class OperationShape:
427
433
  yield text
428
434
  case _:
429
435
  continue
430
-
431
- @staticmethod
432
- def _says(
433
- *,
434
- file: ParsedFile,
435
- declared: Declaration,
436
- message: str,
437
- ) -> Violation:
438
- return Violation.from_node(
439
- node=declared.node,
440
- path=file.path,
441
- code=CODE,
442
- message=message,
443
- )
@@ -47,8 +47,9 @@ class RequiredClass:
47
47
  With `named`, the directory keeps many classes of one kind rather than
48
48
  one per module, and each of them carries the suffix: a `Price` among the
49
49
  `...Value` classes is the one nobody will look for under that name. A
50
- class with a leading underscore is the module's machinery and is left
51
- alone.
50
+ class built on a base from another module carries it too: one file does
51
+ not show its kind, but it is in the directory. A class with a leading
52
+ underscore is the module's machinery and is left alone.
52
53
 
53
54
  `__init__.py` re-exports rather than declares; an empty module has not
54
55
  promised anything yet; a module with a leading underscore (`_base.py`)
@@ -117,9 +118,10 @@ class RequiredClass:
117
118
  ) -> Iterator[Violation]:
118
119
  required = [one for one in declared if one.name.endswith(suffix)]
119
120
  if not required:
120
- yield cls._says(
121
- file=file,
122
- node=declared[0],
121
+ yield Violation.from_node(
122
+ node=declared[0].node,
123
+ path=file.path,
124
+ code=CODE,
123
125
  message=(
124
126
  f"the module declares {cls._listed(declared=declared)}, "
125
127
  f"but a module in this directory declares a ...{suffix} class"
@@ -127,9 +129,10 @@ class RequiredClass:
127
129
  )
128
130
  return
129
131
  if len(required) > 1:
130
- yield cls._says(
131
- file=file,
132
- node=required[1],
132
+ yield Violation.from_node(
133
+ node=required[1].node,
134
+ path=file.path,
135
+ code=CODE,
133
136
  message=(
134
137
  f"the module holds {cls._listed(declared=required)}; "
135
138
  f"one ...{suffix} per module, and the module is named after it"
@@ -141,9 +144,10 @@ class RequiredClass:
141
144
  suffix=suffix,
142
145
  )
143
146
  if ahead is not None:
144
- yield cls._says(
145
- file=file,
146
- node=ahead,
147
+ yield Violation.from_node(
148
+ node=ahead.node,
149
+ path=file.path,
150
+ code=CODE,
147
151
  message=(
148
152
  f"{ahead.name} is declared above the ...{suffix} the module "
149
153
  f"exists for; helpers belong below it"
@@ -164,13 +168,16 @@ class RequiredClass:
164
168
  for one in declared:
165
169
  if not isinstance(one.node, ast.ClassDef) or one.name.startswith("_"):
166
170
  continue
167
- if directory.only and one.kind not in directory.only:
171
+ # A kind no file shows — a base from another module — is still this
172
+ # directory's class: only a kind known to be foreign is let off.
173
+ if directory.only and one.kind is not None and one.kind not in directory.only:
168
174
  continue
169
175
  if one.name.endswith(suffix):
170
176
  continue
171
- yield cls._says(
172
- file=file,
173
- node=one,
177
+ yield Violation.from_node(
178
+ node=one.node,
179
+ path=file.path,
180
+ code=CODE,
174
181
  message=f"{one.name} does not end with {suffix}; in {address} every class does",
175
182
  )
176
183
 
@@ -188,20 +195,6 @@ class RequiredClass:
188
195
  return one
189
196
  return None
190
197
 
191
- @staticmethod
192
- def _says(
193
- *,
194
- file: ParsedFile,
195
- node: Declaration,
196
- message: str,
197
- ) -> Violation:
198
- return Violation.from_node(
199
- node=node.node,
200
- path=file.path,
201
- code=CODE,
202
- message=message,
203
- )
204
-
205
198
  @staticmethod
206
199
  def _listed(*, declared: list[Declaration]) -> str:
207
200
  return ", ".join(one.name for one in declared)
@@ -356,20 +356,62 @@ def _rewritten(
356
356
  return None, [
357
357
  Patch(
358
358
  path=site.path,
359
- edit=Edit(
360
- line=argument.lineno,
361
- column=(
362
- at := column(
363
- line=lines[argument.lineno - 1],
364
- offset=argument.col_offset,
365
- )
366
- ),
367
- end_line=argument.lineno,
368
- end_column=at,
369
- text=f"{parameter}=",
370
- ),
359
+ edit=edit,
371
360
  )
372
361
  for argument, parameter in zip(moved, target.names, strict=False)
362
+ for edit in _named(
363
+ call=call,
364
+ argument=argument,
365
+ parameter=parameter,
366
+ lines=lines,
367
+ )
368
+ ]
369
+
370
+
371
+ def _named(
372
+ *,
373
+ call: ast.Call,
374
+ argument: ast.expr,
375
+ parameter: str,
376
+ lines: Sequence[str],
377
+ ) -> list[Edit]:
378
+ """`name=` before the argument; a bare generator also gets parentheses of its own.
379
+
380
+ `f(x for x in y)`: the generator's parentheses are the call's, and the tree
381
+ starts it at the call's `(`. The name goes inside them, and a `)` after
382
+ the call closes it again: `f(name=(x for x in y))`.
383
+ """
384
+ end = argument.end_lineno or argument.lineno
385
+ bare = isinstance(argument, ast.GeneratorExp) and (end, argument.end_col_offset) == (
386
+ call.end_lineno,
387
+ call.end_col_offset,
388
+ )
389
+ at = column(
390
+ line=lines[argument.lineno - 1],
391
+ offset=argument.col_offset,
392
+ ) + int(bare)
393
+ named = Edit(
394
+ line=argument.lineno,
395
+ column=at,
396
+ end_line=argument.lineno,
397
+ end_column=at,
398
+ text=f"{parameter}=" + "(" * bare,
399
+ )
400
+ if not bare:
401
+ return [named]
402
+ closing = column(
403
+ line=lines[end - 1],
404
+ offset=argument.end_col_offset or 0,
405
+ )
406
+ return [
407
+ named,
408
+ Edit(
409
+ line=end,
410
+ column=closing,
411
+ end_line=end,
412
+ end_column=closing,
413
+ text=")",
414
+ ),
373
415
  ]
374
416
 
375
417
 
@@ -6,7 +6,8 @@ from typing import TYPE_CHECKING, ClassVar, Final
6
6
 
7
7
  from pydantic import Field
8
8
 
9
- from py_checks.checks.signatures._functions import definitions, signature_end
9
+ from py_checks.checks._kind import signature_end
10
+ from py_checks.checks.signatures._functions import definitions
10
11
  from py_checks.checks.signatures._marker import MARKER
11
12
  from py_checks.config import CheckSettings
12
13
  from py_checks.core import Scope, Violation, settings_as
@@ -70,7 +71,10 @@ class FunctionLength:
70
71
  # The mark belongs at the end of the signature: it does not
71
72
  # always fit on the `def` line, and a signature laid out in a
72
73
  # column ends far from where the violation points.
73
- end_line=signature_end(node=definition.node),
74
+ end_line=signature_end(
75
+ node=definition.node,
76
+ lines=file.lines,
77
+ ),
74
78
  message=(
75
79
  f"{definition.name}: {length} lines, the limit is {limits.max_lines}; "
76
80
  "move part of it into a separate function"
@@ -86,8 +86,3 @@ def name(*, node: ast.expr) -> str:
86
86
  return found
87
87
  case _:
88
88
  return ""
89
-
90
-
91
- def signature_end(*, node: Function) -> int:
92
- """The last line of the signature: the mark goes there when the signature is in a column."""
93
- return max(node.body[0].lineno - 1, node.lineno)
@@ -4,12 +4,12 @@ from __future__ import annotations
4
4
 
5
5
  from typing import TYPE_CHECKING, ClassVar, Final
6
6
 
7
+ from py_checks.checks._kind import signature_end
7
8
  from py_checks.checks.signatures._callers import callers
8
9
  from py_checks.checks.signatures._functions import (
9
10
  Definition,
10
11
  definitions,
11
12
  receiver,
12
- signature_end,
13
13
  )
14
14
  from py_checks.checks.signatures._marker import MARKER
15
15
  from py_checks.config import CheckSettings
@@ -78,7 +78,10 @@ class KeywordOnlyArguments:
78
78
  file: ParsedFile,
79
79
  ) -> Iterator[Violation]:
80
80
  node, name = definition.node, definition.name
81
- end_line = signature_end(node=node)
81
+ end_line = signature_end(
82
+ node=node,
83
+ lines=file.lines,
84
+ )
82
85
  if positional := cls._positional(definition=definition):
83
86
  yield Violation.from_node(
84
87
  node=node,
@@ -73,18 +73,20 @@ class AnnotationShapes:
73
73
  arguments=arguments,
74
74
  keys=limits.keys,
75
75
  ):
76
- yield cls._says(
77
- file=file,
76
+ yield Violation.from_node(
78
77
  node=node,
78
+ path=file.path,
79
+ code=CODE,
79
80
  message=(
80
81
  f"{ast.unparse(node)} reads as a set of named fields; "
81
82
  f"a TypedDict or a dataclass will say which ones"
82
83
  ),
83
84
  )
84
85
  if written == TUPLE and limits.tuples and cls._fixed(arguments=arguments):
85
- yield cls._says(
86
- file=file,
86
+ yield Violation.from_node(
87
87
  node=node,
88
+ path=file.path,
89
+ code=CODE,
88
90
  message=(
89
91
  f"{ast.unparse(node)} names its fields by position; a dataclass gives "
90
92
  f"them names, or a NamedTuple if it has to stay a tuple"
@@ -111,17 +113,3 @@ class AnnotationShapes:
111
113
  def _arguments(*, node: ast.Subscript) -> list[ast.expr]:
112
114
  inside = node.slice
113
115
  return list(inside.elts) if isinstance(inside, ast.Tuple) else [inside]
114
-
115
- @staticmethod
116
- def _says(
117
- *,
118
- file: ParsedFile,
119
- node: ast.Subscript,
120
- message: str,
121
- ) -> Violation:
122
- return Violation.from_node(
123
- node=node,
124
- path=file.path,
125
- code=CODE,
126
- message=message,
127
- )