spacify 0.1.0__tar.gz → 0.2.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.
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ ## [0.2.0](https://github.com/avanserv/spacify/compare/v0.1.1...v0.2.0) (2026-10-19)
4
+
5
+
6
+ ### Features
7
+
8
+ * **rules:** Keep module constants apart from other assignments ([#6](https://github.com/avanserv/spacify/issues/6)) ([704113c](https://github.com/avanserv/spacify/commit/704113c9972804035c096f00b99015a4b11aa3f2))
9
+
10
+ ## [0.1.1](https://github.com/avanserv/spacify/compare/v0.1.0...v0.1.1) (2026-10-14)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * Install on Python 3.7 and 3.8 ([#4](https://github.com/avanserv/spacify/issues/4)) ([7ac45d9](https://github.com/avanserv/spacify/commit/7ac45d977c1f5546d9060316fbbf53780ddeaa75))
16
+
17
+ ## 0.1.0 (2026-10-14)
18
+
19
+
20
+ ### Features
21
+
22
+ * Format blank lines around Python logical blocks ([f099979](https://github.com/avanserv/spacify/commit/f099979f1224a687b16aa101386890b57a04d795))
@@ -502,7 +502,7 @@ dependencies = [
502
502
 
503
503
  [[package]]
504
504
  name = "spacify"
505
- version = "0.1.0"
505
+ version = "0.2.0"
506
506
  dependencies = [
507
507
  "anyhow",
508
508
  "clap",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "spacify"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  edition = "2024"
5
5
  rust-version = "1.85"
6
6
  description = "A fast formatter for the vertical rhythm of Python code, complementary to ruff"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: spacify
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: Environment :: Console
6
6
  Classifier: Intended Audience :: Developers
@@ -11,7 +11,7 @@ License-File: LICENSE
11
11
  Summary: A fast formatter for the vertical rhythm of Python code, complementary to ruff
12
12
  Keywords: python,formatter,blank-lines,ruff,pre-commit
13
13
  License-Expression: MIT
14
- Requires-Python: >=3.9
14
+ Requires-Python: >=3.7
15
15
  Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
16
16
  Project-URL: Changelog, https://github.com/avanserv/spacify/blob/main/CHANGELOG.md
17
17
  Project-URL: Repository, https://github.com/avanserv/spacify
@@ -113,7 +113,7 @@ Each rule can be turned on or off. They all apply to sibling statements in the s
113
113
  | `blank-before-block` | At least one blank line before `if`, `for`, `while`, `try`, `with` and `match` (including the `async` forms) when another statement comes before them. |
114
114
  | `blank-after-block` | At least one blank line after one of those blocks, before the next statement. |
115
115
  | `blank-before-exit` | At least one blank line before `return`, `raise`, `break`, `continue` and a bare `yield`, in blocks of `exit-min-block-statements` (default 3) or more statements. |
116
- | `collapse-groups` | No blank line between two single-line statements of the same kind: assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`. |
116
+ | `collapse-groups` | No blank line between two single-line statements of the same kind: assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`. Outside every function and class, an assignment whose target names all have an uppercase letter and no lowercase one (`MAX = 3`, `T = TypeVar("T")`, `A, *REST = items`) is a constant, a kind apart from other assignments. |
117
117
 
118
118
  The added blank lines take priority over `collapse-groups`. A few places are never touched:
119
119
 
@@ -95,7 +95,7 @@ Each rule can be turned on or off. They all apply to sibling statements in the s
95
95
  | `blank-before-block` | At least one blank line before `if`, `for`, `while`, `try`, `with` and `match` (including the `async` forms) when another statement comes before them. |
96
96
  | `blank-after-block` | At least one blank line after one of those blocks, before the next statement. |
97
97
  | `blank-before-exit` | At least one blank line before `return`, `raise`, `break`, `continue` and a bare `yield`, in blocks of `exit-min-block-statements` (default 3) or more statements. |
98
- | `collapse-groups` | No blank line between two single-line statements of the same kind: assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`. |
98
+ | `collapse-groups` | No blank line between two single-line statements of the same kind: assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`. Outside every function and class, an assignment whose target names all have an uppercase letter and no lowercase one (`MAX = 3`, `T = TypeVar("T")`, `A, *REST = items`) is a constant, a kind apart from other assignments. |
99
99
 
100
100
  The added blank lines take priority over `collapse-groups`. A few places are never touched:
101
101
 
@@ -10,7 +10,7 @@ description = "A fast formatter for the vertical rhythm of Python code, compleme
10
10
  readme = "README.md"
11
11
  license = "MIT"
12
12
  license-files = ["LICENSE"]
13
- requires-python = ">=3.9"
13
+ requires-python = ">=3.7"
14
14
  keywords = ["python", "formatter", "blank-lines", "ruff", "pre-commit"]
15
15
  classifiers = [
16
16
  "Development Status :: 3 - Alpha",
@@ -50,13 +50,14 @@ pub fn format_source(source: &str, rules: &Rules, stub: bool) -> Result<Option<S
50
50
 
51
51
  let lines: Vec<&str> = source.split_inclusive('\n').collect();
52
52
  let context = Context {
53
+ source,
53
54
  disabled: disabled_prefix(&lines),
54
55
  lines,
55
56
  rules,
56
57
  module_max: if stub { 1 } else { 2 },
57
58
  };
58
59
  let mut edits = Vec::new();
59
- context.container(root, &mut edits);
60
+ context.container(root, true, &mut edits);
60
61
 
61
62
  if edits.is_empty() {
62
63
  return Ok(None);
@@ -65,6 +66,7 @@ pub fn format_source(source: &str, rules: &Rules, stub: bool) -> Result<Option<S
65
66
  }
66
67
 
67
68
  struct Context<'a> {
69
+ source: &'a str,
68
70
  lines: Vec<&'a str>,
69
71
  /// `disabled[r]` counts the rows before `r` inside a `fmt: off` or `spacify: off` region.
70
72
  disabled: Vec<u32>,
@@ -74,7 +76,8 @@ struct Context<'a> {
74
76
 
75
77
  impl Context<'_> {
76
78
  /// Visits a `module` or a `block`: the gaps between its statements, then its nested blocks.
77
- fn container(&self, node: Node, edits: &mut Vec<Edit>) {
79
+ /// `module_scope` holds outside every function and class, even inside a top-level `if`.
80
+ fn container(&self, node: Node, module_scope: bool, edits: &mut Vec<Edit>) {
78
81
  let mut cursor = node.walk();
79
82
  let statements: Vec<Node> = node
80
83
  .named_children(&mut cursor)
@@ -91,33 +94,34 @@ impl Context<'_> {
91
94
 
92
95
  for (index, pair) in statements.windows(2).enumerate() {
93
96
  let docstring = index == 0 && has_docstring && is_docstring(pair[0]);
94
- if !docstring && let Some(edit) = self.gap(pair[0], pair[1], statements.len(), module) {
97
+ if !docstring && let Some(edit) = self.gap(pair[0], pair[1], statements.len(), module, module_scope) {
95
98
  edits.push(edit);
96
99
  }
97
100
  }
98
101
  }
99
102
 
100
103
  for statement in statements {
101
- self.descend(statement, edits);
104
+ self.descend(statement, module_scope, edits);
102
105
  }
103
106
  }
104
107
 
105
108
  /// Finds the blocks inside a compound statement, through its clauses and decorators.
106
- fn descend(&self, node: Node, edits: &mut Vec<Edit>) {
109
+ fn descend(&self, node: Node, module_scope: bool, edits: &mut Vec<Edit>) {
110
+ let module_scope = module_scope && !node.kind().ends_with("_definition");
107
111
  let mut cursor = node.walk();
108
112
  for child in node.named_children(&mut cursor) {
109
113
  let kind = child.kind();
110
114
  if kind == "block" {
111
- self.container(child, edits);
115
+ self.container(child, module_scope, edits);
112
116
  } else if kind.ends_with("_clause") || kind.ends_with("_definition") {
113
- self.descend(child, edits);
117
+ self.descend(child, module_scope, edits);
114
118
  }
115
119
  }
116
120
  }
117
121
 
118
- fn gap(&self, prev: Node, next: Node, block_len: usize, module: bool) -> Option<Edit> {
119
- let prev_kind = classify(prev);
120
- let next_kind = classify(next);
122
+ fn gap(&self, prev: Node, next: Node, block_len: usize, module: bool, module_scope: bool) -> Option<Edit> {
123
+ let prev_kind = self.classify(prev, module_scope);
124
+ let next_kind = self.classify(next, module_scope);
121
125
  if prev_kind == Kind::Definition || next_kind == Kind::Definition {
122
126
  return None;
123
127
  }
@@ -167,31 +171,66 @@ impl Context<'_> {
167
171
  .iter()
168
172
  .any(|&row| has_fmt_skip(self.lines[row]))
169
173
  }
170
- }
171
174
 
172
- fn classify(node: Node) -> Kind {
173
- match node.kind() {
174
- "if_statement" | "for_statement" | "while_statement" | "try_statement" | "with_statement"
175
- | "match_statement" => Kind::Compound,
176
- "function_definition" | "class_definition" | "decorated_definition" => Kind::Definition,
177
- "return_statement" | "raise_statement" | "break_statement" | "continue_statement" => Kind::Exit,
178
- "import_statement" | "import_from_statement" | "future_import_statement" => Kind::Import,
179
- "delete_statement" => Kind::Simple(Group::Del),
180
- "assert_statement" => Kind::Simple(Group::Assert),
181
- "global_statement" | "nonlocal_statement" => Kind::Simple(Group::Scope),
182
- "expression_statement" if node.named_child_count() == 1 => {
183
- match node.named_child(0).map(|child| child.kind()) {
184
- Some("assignment") => Kind::Simple(Group::Assign),
185
- Some("augmented_assignment") => Kind::Simple(Group::AugAssign),
186
- Some("call" | "await") => Kind::Simple(Group::Call),
187
- Some("yield") => Kind::Exit,
188
- _ => Kind::Other,
175
+ fn classify(&self, node: Node, module_scope: bool) -> Kind {
176
+ match node.kind() {
177
+ "if_statement" | "for_statement" | "while_statement" | "try_statement" | "with_statement"
178
+ | "match_statement" => Kind::Compound,
179
+ "function_definition" | "class_definition" | "decorated_definition" => Kind::Definition,
180
+ "return_statement" | "raise_statement" | "break_statement" | "continue_statement" => Kind::Exit,
181
+ "import_statement" | "import_from_statement" | "future_import_statement" => Kind::Import,
182
+ "delete_statement" => Kind::Simple(Group::Del),
183
+ "assert_statement" => Kind::Simple(Group::Assert),
184
+ "global_statement" | "nonlocal_statement" => Kind::Simple(Group::Scope),
185
+ "expression_statement" if node.named_child_count() == 1 => {
186
+ let Some(child) = node.named_child(0) else {
187
+ return Kind::Other;
188
+ };
189
+ match child.kind() {
190
+ "assignment" if module_scope && self.assigns_constants(child) => Kind::Simple(Group::Constant),
191
+ "assignment" => Kind::Simple(Group::Assign),
192
+ "augmented_assignment" => Kind::Simple(Group::AugAssign),
193
+ "call" | "await" => Kind::Simple(Group::Call),
194
+ "yield" => Kind::Exit,
195
+ _ => Kind::Other,
196
+ }
197
+ }
198
+ _ => Kind::Other,
199
+ }
200
+ }
201
+
202
+ /// Whether every name `assignment` binds, through a chain such as `A = B = 1`, is a constant
203
+ /// name: `A = 1`, `A: int = 1`, `A, *B = items`, `[A, (B, C)] = rows`.
204
+ fn assigns_constants(&self, assignment: Node) -> bool {
205
+ assignment
206
+ .child_by_field_name("left")
207
+ .is_some_and(|left| self.binds_constants(left))
208
+ && assignment
209
+ .child_by_field_name("right")
210
+ .is_none_or(|right| right.kind() != "assignment" || self.assigns_constants(right))
211
+ }
212
+
213
+ fn binds_constants(&self, target: Node) -> bool {
214
+ match target.kind() {
215
+ "identifier" => is_constant_name(&self.source[target.byte_range()]),
216
+ "pattern_list" | "tuple_pattern" | "list_pattern" => {
217
+ let mut cursor = target.walk();
218
+ target.named_child_count() > 0
219
+ && target
220
+ .named_children(&mut cursor)
221
+ .all(|child| self.binds_constants(child))
189
222
  }
223
+ "list_splat_pattern" => target.named_child(0).is_some_and(|name| self.binds_constants(name)),
224
+ _ => false,
190
225
  }
191
- _ => Kind::Other,
192
226
  }
193
227
  }
194
228
 
229
+ /// An uppercase letter and no lowercase one, in any script: `MAX`, `T`, `_A1`, `ÉTÉ`.
230
+ fn is_constant_name(name: &str) -> bool {
231
+ name.chars().any(char::is_uppercase) && !name.chars().any(char::is_lowercase)
232
+ }
233
+
195
234
  fn is_docstring(node: Node) -> bool {
196
235
  node.kind() == "expression_statement"
197
236
  && node.named_child_count() == 1
@@ -19,6 +19,9 @@ pub enum Kind {
19
19
  #[derive(Debug, Clone, Copy, PartialEq, Eq)]
20
20
  pub enum Group {
21
21
  Assign,
22
+ /// An assignment to `UPPER_CASE` names outside every function and class: configuration, which
23
+ /// reads apart from the statements setting the module up, such as `logger = getLogger(__name__)`.
24
+ Constant,
22
25
  AugAssign,
23
26
  Call,
24
27
  Del,
@@ -1,8 +0,0 @@
1
- # Changelog
2
-
3
- ## 0.1.0 (2026-10-14)
4
-
5
-
6
- ### Features
7
-
8
- * Format blank lines around Python logical blocks ([f099979](https://github.com/avanserv/spacify/commit/f099979f1224a687b16aa101386890b57a04d795))
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes