matchify 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.
Files changed (54) hide show
  1. {matchify-0.1.0 → matchify-0.2.0}/CHANGELOG.md +13 -0
  2. {matchify-0.1.0 → matchify-0.2.0}/PKG-INFO +268 -127
  3. {matchify-0.1.0 → matchify-0.2.0}/README.md +267 -126
  4. {matchify-0.1.0 → matchify-0.2.0}/pyproject.toml +1 -1
  5. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/assumptions.py +6 -0
  6. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/compiler.py +2 -2
  7. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/conditions.py +36 -14
  8. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/patterns.py +4 -2
  9. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/safety.py +4 -2
  10. {matchify-0.1.0 → matchify-0.2.0}/tests/test_cli.py +27 -0
  11. {matchify-0.1.0 → matchify-0.2.0}/tests/test_edge_cases.py +21 -0
  12. {matchify-0.1.0 → matchify-0.2.0}/tests/test_readme.py +22 -1
  13. {matchify-0.1.0 → matchify-0.2.0}/tests/test_transform.py +61 -0
  14. {matchify-0.1.0 → matchify-0.2.0}/uv.lock +1 -1
  15. {matchify-0.1.0 → matchify-0.2.0}/.copier-answers.yml +0 -0
  16. {matchify-0.1.0 → matchify-0.2.0}/.copier-enforce.json +0 -0
  17. {matchify-0.1.0 → matchify-0.2.0}/.github/FUNDING.yml +0 -0
  18. {matchify-0.1.0 → matchify-0.2.0}/.github/actions/setup/action.yml +0 -0
  19. {matchify-0.1.0 → matchify-0.2.0}/.github/pull_request_template.md +0 -0
  20. {matchify-0.1.0 → matchify-0.2.0}/.github/workflows/ci.yml +0 -0
  21. {matchify-0.1.0 → matchify-0.2.0}/.github/workflows/release-pr.yml +0 -0
  22. {matchify-0.1.0 → matchify-0.2.0}/.github/workflows/weekly.yml +0 -0
  23. {matchify-0.1.0 → matchify-0.2.0}/.gitignore +0 -0
  24. {matchify-0.1.0 → matchify-0.2.0}/.pre-commit-config.yaml +0 -0
  25. {matchify-0.1.0 → matchify-0.2.0}/.pre-commit-hooks.yaml +0 -0
  26. {matchify-0.1.0 → matchify-0.2.0}/AGENTS.md +0 -0
  27. {matchify-0.1.0 → matchify-0.2.0}/CONTRIBUTING.md +0 -0
  28. {matchify-0.1.0 → matchify-0.2.0}/COPIER_TEMPLATE_CHANGES.md +0 -0
  29. {matchify-0.1.0 → matchify-0.2.0}/LICENSE +0 -0
  30. {matchify-0.1.0 → matchify-0.2.0}/changelog.d/.gitignore +0 -0
  31. {matchify-0.1.0 → matchify-0.2.0}/design.md +0 -0
  32. {matchify-0.1.0 → matchify-0.2.0}/find_new_issue.py +0 -0
  33. {matchify-0.1.0 → matchify-0.2.0}/find_new_issue_2.py +0 -0
  34. {matchify-0.1.0 → matchify-0.2.0}/limitations.md +0 -0
  35. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/__init__.py +0 -0
  36. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/__main__.py +0 -0
  37. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/access_path.py +0 -0
  38. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/capture_patterns.py +0 -0
  39. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/cli.py +0 -0
  40. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/facts.py +0 -0
  41. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/lookup_tables.py +0 -0
  42. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/pattern_builder.py +0 -0
  43. {matchify-0.1.0 → matchify-0.2.0}/src/matchify/transform.py +0 -0
  44. {matchify-0.1.0 → matchify-0.2.0}/test-repos.json +0 -0
  45. {matchify-0.1.0 → matchify-0.2.0}/test-repos.py +0 -0
  46. {matchify-0.1.0 → matchify-0.2.0}/tests/helpers.py +0 -0
  47. {matchify-0.1.0 → matchify-0.2.0}/tests/samples/.gitkeep +0 -0
  48. {matchify-0.1.0 → matchify-0.2.0}/tests/test_access_path.py +0 -0
  49. {matchify-0.1.0 → matchify-0.2.0}/tests/test_find_new_issue_2.py +0 -0
  50. {matchify-0.1.0 → matchify-0.2.0}/tests/test_generated_roundtrip.py +0 -0
  51. {matchify-0.1.0 → matchify-0.2.0}/tests/test_guard_rendering.py +0 -0
  52. {matchify-0.1.0 → matchify-0.2.0}/tests/test_lookup_tables.py +0 -0
  53. {matchify-0.1.0 → matchify-0.2.0}/tests/test_public_coverage.py +0 -0
  54. {matchify-0.1.0 → matchify-0.2.0}/tests/test_sequence_assumptions.py +0 -0
@@ -2,6 +2,19 @@
2
2
 
3
3
  <!-- scriv-insert-here -->
4
4
 
5
+ <a id='changelog-0.2.0'></a>
6
+ # 0.2.0 — 2026-08-08
7
+
8
+ ## Added
9
+
10
+ - Convert membership tests against eligible literal sets into OR patterns when
11
+ the `hashable-subjects` assumption is enabled.
12
+
13
+ ## Fixed
14
+
15
+ - Apply `--no-types` patterns to qualified `isinstance` targets such as
16
+ `typing.Mapping`, including targets contained in type tuples.
17
+
5
18
  <a id='changelog-0.1.0'></a>
6
19
  # 0.1.0 — 2026-08-04
7
20
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: matchify
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: converts if-else code to match statements
5
5
  Project-URL: Documentation, https://github.com/15r10nk/matchify#readme
6
6
  Project-URL: Issues, https://github.com/15r10nk/matchify/issues
@@ -21,6 +21,8 @@ Requires-Python: >=3.10
21
21
  Requires-Dist: libcst>=1.8.6
22
22
  Description-Content-Type: text/markdown
23
23
 
24
+ # Matchify
25
+
24
26
  ![ci](https://github.com/15r10nk/matchify/actions/workflows/ci.yml/badge.svg?branch=main)
25
27
 
26
28
  [![pypi version](https://img.shields.io/pypi/v/matchify.svg)](https://pypi.org/project/matchify/)
@@ -28,135 +30,14 @@ Description-Content-Type: text/markdown
28
30
  [![PyPI - Downloads](https://img.shields.io/pypi/dw/matchify)](https://pypacktrends.com/?packages=matchify&time_range=2years)
29
31
  [![GitHub Sponsors](https://img.shields.io/github/sponsors/15r10nk)](https://github.com/sponsors/15r10nk)
30
32
 
31
- ## Installation
32
-
33
- Install "matchify" as a command-line tool with
34
- [uv](https://docs.astral.sh/uv/):
35
-
36
- ```bash
37
- uv tool install matchify
38
- ```
39
-
40
- Or run it without installing:
41
-
42
- ```bash
43
- uvx matchify path/to/project/
44
- ```
45
-
46
- ## Key Features
47
-
48
- - **Automatic conversion** of if/elif/else chains to Python 3.10+ match statements
49
- - **Preserves formatting** and code structure using LibCST
50
- - **Supports multiple pattern types**:
51
- - Literal comparisons (`x == 1`, `x == "value"`)
52
- - Identity checks (`x is None`, `x is True`)
53
- - isinstance checks (`isinstance(x, MyClass)`)
54
- - Class patterns with attributes (`isinstance(p, Point) and p.x == 5`)
55
- - Sequence patterns (`len(x) == 2 and x[0] == 0 and x[1] == 1`)
56
- - Nested sequences (`[[1, 2], 3]`)
57
- - Sequence attributes in class patterns (`Data(value=[1, 2, 3])`)
58
- - Or patterns for isinstance tuples (`isinstance(x, (int, float))`)
59
- - **Parallel processing** for fast conversion of large codebases
60
- - **Safe transformations** - only converts when semantics are preserved
33
+ Matchify automatically converts eligible `if`/`elif`/`else` chains into
34
+ Python 3.10+ `match` statements while preserving runtime behavior and source
35
+ formatting.
61
36
 
62
- ## Usage
63
-
64
- ```bash
65
- # Convert a single file
66
- matchify path/to/file.py
67
-
68
- # Convert all Python files in a directory
69
- matchify path/to/project/
70
-
71
- # Convert with verbose output
72
- matchify path/to/project/ -v
73
-
74
- # Check whether files would be converted without writing changes
75
- matchify path/to/project/ --check
76
-
77
- # Use parallel processing (default: number of CPUs)
78
- matchify path/to/project/ -j 8
79
-
80
- # Enable one risky assumption explicitly
81
- matchify path/to/project/ --assume pure-subjects
82
-
83
- # Disable all risky assumptions
84
- matchify path/to/project/ --safe
85
-
86
- # Enable all risky assumptions
87
- matchify path/to/project/ --risky
88
- ```
89
-
90
- By default, Matchify enables no risky assumptions. `--safe` makes that explicit.
91
- `--risky` enables all available risky assumptions.
92
- When a skipped `if`/`elif` chain would require a risky assumption, the CLI
93
- prints the file location and the required `--assume` value instead of converting
94
- that chain.
95
-
96
- ## pre-commit
97
-
98
- Matchify provides two pre-commit hooks.
99
-
100
- Use `matchify` to automatically rewrite files, similar to the default Black
101
- hook:
102
-
103
- ```yaml
104
- repos:
105
- - repo: https://github.com/15r10nk/matchify
106
- rev: v0.0.1
107
- hooks:
108
- - id: matchify
109
- ```
110
-
111
- Use `matchify-check` to only report files that would be converted without
112
- modifying them:
113
-
114
- ```yaml
115
- repos:
116
- - repo: https://github.com/15r10nk/matchify
117
- rev: v0.0.1
118
- hooks:
119
- - id: matchify-check
120
- ```
121
-
122
- Available risky assumptions:
123
-
124
- - `pure-subjects`: permits transformations such as
125
- `a.x == 1 and b.y == 2` into a match on `(a.x, b.y)`. This evaluates every
126
- subject eagerly, so enable it only when those name, attribute, and subscript
127
- reads cannot raise exceptions or produce observable side effects. Without the
128
- option, later `and` operands remain guards and preserve short-circuiting.
129
- - `use-object`: permits generic attribute patterns such as `object(x=1)` when
130
- different branches inspect attributes of a common object without an explicit
131
- `isinstance` check. This performs pattern-time attribute lookups, so enable it
132
- only when those lookups cannot raise exceptions or produce observable side
133
- effects.
134
- - `identity-equality`: permits conversions from qualified identity comparisons
135
- such as `op is Op.ADD` to value patterns such as `case Op.ADD`. Match value
136
- patterns compare with equality, not identity, so enable it only when identity
137
- and equality are equivalent for those values.
138
- - `list-sequence-pattern`: permits a sequence pattern to imply an explicit
139
- `isinstance(value, list)` check. Python sequence patterns can also match other
140
- sequence types, so enable it only when that broader match is acceptable.
141
- - `tuple-sequence-pattern`: permits a sequence pattern to imply an explicit
142
- `isinstance(value, tuple)` check. Python sequence patterns can also match other
143
- sequence types, so enable it only when that broader match is acceptable.
144
- Checks against `(list, tuple)` require both sequence assumptions.
145
- - `lookup-equality`: permits dictionary lookup tables embedded in statements to
146
- become `match` statements. Dictionary lookup uses hashing while patterns use
147
- equality, and dictionary values are evaluated only in the selected case
148
- instead of eagerly when constructing the dictionary. Enable it only when
149
- those equality and evaluation-order differences are acceptable. Tuple keys,
150
- including nested tuples, become sequence patterns and can therefore also
151
- match equivalent non-tuple sequences.
152
-
153
- Development and repository-testing notes are in
154
- [CONTRIBUTING.md](CONTRIBUTING.md).
155
-
156
-
157
- ### Examples
37
+ ## Examples
158
38
 
159
39
  **Simple equality chain:**
40
+
160
41
  ```python
161
42
  # Before
162
43
  if x == 1:
@@ -177,6 +58,7 @@ match x:
177
58
  ```
178
59
 
179
60
  **isinstance with attributes:**
61
+
180
62
  ```python
181
63
  # Before
182
64
  if isinstance(node, Point) and node.x == 5:
@@ -193,6 +75,7 @@ match node:
193
75
  ```
194
76
 
195
77
  **Sequence patterns:**
78
+
196
79
  ```python
197
80
  # Before
198
81
  if len(point) == 2 and point[0] == 0 and point[1] == 1:
@@ -209,6 +92,7 @@ match point:
209
92
  ```
210
93
 
211
94
  **Nested patterns (isinstance inside sequences):**
95
+
212
96
  ```python
213
97
  # Before
214
98
  if len(x) == 2 and isinstance(x[0], Point) and x[1] == 2:
@@ -225,6 +109,7 @@ match x:
225
109
  ```
226
110
 
227
111
  **Nested sequences:**
112
+
228
113
  ```python
229
114
  # Before
230
115
  if (
@@ -253,6 +138,7 @@ match data:
253
138
  ```
254
139
 
255
140
  **Class patterns with sequence attributes:**
141
+
256
142
  ```python
257
143
  # Before
258
144
  class Data:
@@ -287,6 +173,261 @@ match obj:
287
173
  print("other data")
288
174
  ```
289
175
 
176
+ ## Installation
177
+
178
+ Install "matchify" as a command-line tool with
179
+ [uv](https://docs.astral.sh/uv/):
180
+
181
+ ```bash
182
+ uv tool install matchify
183
+ ```
184
+
185
+ Or run it without installing:
186
+
187
+ ```bash
188
+ uvx matchify path/to/project/
189
+ ```
190
+
191
+ ## Key Features
192
+
193
+ - **Automatic conversion** of if/elif/else chains to Python 3.10+ match statements
194
+ - **Preserves formatting** and code structure using LibCST
195
+ - **Supports multiple pattern types**:
196
+ - Literal comparisons (`x == 1`, `x == "value"`)
197
+ - Identity checks (`x is None`, `x is True`)
198
+ - isinstance checks (`isinstance(x, MyClass)`)
199
+ - Class patterns with attributes (`isinstance(p, Point) and p.x == 5`)
200
+ - Sequence patterns (`len(x) == 2 and x[0] == 0 and x[1] == 1`)
201
+ - Nested sequences (`[[1, 2], 3]`)
202
+ - Sequence attributes in class patterns (`Data(value=[1, 2, 3])`)
203
+ - Or patterns for isinstance tuples (`isinstance(x, (int, float))`)
204
+ - **Parallel processing** for fast conversion of large codebases
205
+ - **Safe transformations** - only converts when semantics are preserved
206
+
207
+ ## Usage
208
+
209
+ ```bash
210
+ # Convert a single file
211
+ matchify path/to/file.py
212
+
213
+ # Convert all Python files in a directory
214
+ matchify path/to/project/
215
+
216
+ # Convert with verbose output
217
+ matchify path/to/project/ -v
218
+
219
+ # Check whether files would be converted without writing changes
220
+ matchify path/to/project/ --check
221
+
222
+ # Use parallel processing (default: number of CPUs)
223
+ matchify path/to/project/ -j 8
224
+
225
+ # Enable one risky assumption explicitly
226
+ matchify path/to/project/ --assume pure-subjects
227
+
228
+ # Disable all risky assumptions
229
+ matchify path/to/project/ --safe
230
+
231
+ # Enable all risky assumptions
232
+ matchify path/to/project/ --risky
233
+ ```
234
+
235
+ ## pre-commit
236
+
237
+ Matchify provides two pre-commit hooks.
238
+
239
+ Use `matchify` to automatically rewrite files, similar to the default Black
240
+ hook:
241
+
242
+ ```yaml
243
+ repos:
244
+ - repo: https://github.com/15r10nk/matchify
245
+ rev: v0.1.0
246
+ hooks:
247
+ - id: matchify
248
+ ```
249
+
250
+ Use `matchify-check` to only report files that would be converted without
251
+ modifying them:
252
+
253
+ ```yaml
254
+ repos:
255
+ - repo: https://github.com/15r10nk/matchify
256
+ rev: v0.1.0
257
+ hooks:
258
+ - id: matchify-check
259
+ ```
260
+
261
+ ## Risky assumptions
262
+
263
+ By default, Matchify enables no risky assumptions. `--safe` makes that explicit.
264
+ `--risky` enables all available risky assumptions.
265
+ When a skipped `if`/`elif` chain would require a risky assumption, the CLI
266
+ prints the file location and the required `--assume` value instead of converting
267
+ that chain.
268
+
269
+ ### `--assume=pure-subjects`
270
+
271
+ Permits transformations such as `a.x == 1 and b.y == 2` into a match on
272
+ `(a.x, b.y)`. This evaluates every subject eagerly, so enable it only when those
273
+ name, attribute, and subscript reads cannot raise exceptions or produce
274
+ observable side effects. Without the option, later `and` operands remain guards
275
+ and preserve short-circuiting.
276
+
277
+ ```python
278
+ # Before
279
+ if a.x == 1 and b.y == 2:
280
+ handle_first()
281
+ elif a.x == 3 and b.y == 4:
282
+ handle_second()
283
+
284
+ # After
285
+ match (a.x, b.y):
286
+ case 1, 2:
287
+ handle_first()
288
+ case 3, 4:
289
+ handle_second()
290
+ ```
291
+
292
+ ### `--assume=use-object`
293
+
294
+ Permits generic attribute patterns such as `object(x=1)` when different
295
+ branches inspect attributes of a common object without an explicit `isinstance`
296
+ check. This performs pattern-time attribute lookups, so enable it only when
297
+ those lookups cannot raise exceptions or produce observable side effects.
298
+
299
+ ```python
300
+ # Before
301
+ if value.x == 1:
302
+ handle_x()
303
+ elif value.y == 2:
304
+ handle_y()
305
+
306
+ # After
307
+ match value:
308
+ case object(x=1):
309
+ handle_x()
310
+ case object(y=2):
311
+ handle_y()
312
+ ```
313
+
314
+ ### `--assume=identity-equality`
315
+
316
+ Permits conversions from qualified identity comparisons such as
317
+ `op is Op.ADD` to value patterns such as `case Op.ADD`. Match value patterns
318
+ compare with equality, not identity, so enable it only when identity and
319
+ equality are equivalent for those values.
320
+
321
+ ```python
322
+ # Before
323
+ if op is Op.ADD:
324
+ add()
325
+ elif op is Op.SUB:
326
+ subtract()
327
+
328
+ # After
329
+ match op:
330
+ case Op.ADD:
331
+ add()
332
+ case Op.SUB:
333
+ subtract()
334
+ ```
335
+
336
+ ### `--assume=hashable-subjects`
337
+
338
+ Permits membership tests against literal sets to become OR patterns. Set
339
+ membership hashes the subject and can raise `TypeError` for an unhashable value,
340
+ while a pattern only performs equality comparisons. Enable it only when match
341
+ subjects are hashable. Custom `__hash__` and `__eq__` implementations may still
342
+ make lookup behavior or side effects differ from pattern matching.
343
+
344
+ ```python
345
+ # Before
346
+ if value in {1, 2}:
347
+ handle_small()
348
+ elif value == 3:
349
+ handle_three()
350
+
351
+ # After
352
+ match value:
353
+ case 1 | 2:
354
+ handle_small()
355
+ case 3:
356
+ handle_three()
357
+ ```
358
+
359
+ ### `--assume=list-sequence-pattern`
360
+
361
+ Permits a sequence pattern to imply an explicit `isinstance(value, list)`
362
+ check. Python sequence patterns can also match other sequence types, so enable
363
+ it only when that broader match is acceptable.
364
+
365
+ ```python
366
+ # Before
367
+ if isinstance(value, list) and len(value) == 1 and value[0] == 1:
368
+ handle_one()
369
+ elif value is None:
370
+ handle_none()
371
+
372
+ # After
373
+ match value:
374
+ case 1,:
375
+ handle_one()
376
+ case None:
377
+ handle_none()
378
+ ```
379
+
380
+ ### `--assume=tuple-sequence-pattern`
381
+
382
+ Permits a sequence pattern to imply an explicit `isinstance(value, tuple)`
383
+ check. Python sequence patterns can also match other sequence types, so enable
384
+ it only when that broader match is acceptable. Checks against `(list, tuple)`
385
+ require both sequence assumptions.
386
+
387
+ ```python
388
+ # Before
389
+ if isinstance(value, tuple) and len(value) == 1 and value[0] == 1:
390
+ handle_one()
391
+ elif value is None:
392
+ handle_none()
393
+
394
+ # After
395
+ match value:
396
+ case 1,:
397
+ handle_one()
398
+ case None:
399
+ handle_none()
400
+ ```
401
+
402
+ ### `--assume=lookup-equality`
403
+
404
+ Permits dictionary lookup tables embedded in statements to become `match`
405
+ statements. Dictionary lookup uses hashing while patterns use equality, and
406
+ dictionary values are evaluated only in the selected case instead of eagerly
407
+ when constructing the dictionary. Enable it only when those equality and
408
+ evaluation-order differences are acceptable. Tuple keys, including nested
409
+ tuples, become sequence patterns and can therefore also match equivalent
410
+ non-tuple sequences.
411
+
412
+ ```python
413
+ # Before
414
+ result = {"create": "POST", "read": "GET"}[operation]
415
+
416
+ # After
417
+ match operation:
418
+ case "create":
419
+ result = "POST"
420
+ case "read":
421
+ result = "GET"
422
+ case _matchify_key:
423
+ raise KeyError(_matchify_key)
424
+ ```
425
+
426
+ ## Development
427
+
428
+ Development and repository-testing notes are in
429
+ [CONTRIBUTING.md](CONTRIBUTING.md).
430
+
290
431
  ## Issues
291
432
 
292
433
  If you encounter any problems, please [report an issue](https://github.com/15r10nk/matchify/issues) along with a detailed description.