matchify 0.0.1__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 (41) hide show
  1. matchify-0.0.1/.copier-answers.yml +20 -0
  2. matchify-0.0.1/.github/FUNDING.yml +1 -0
  3. matchify-0.0.1/.github/actions/setup/action.yml +23 -0
  4. matchify-0.0.1/.github/pull_request_template.md +21 -0
  5. matchify-0.0.1/.github/workflows/ci.yml +156 -0
  6. matchify-0.0.1/.gitignore +124 -0
  7. matchify-0.0.1/.pre-commit-config.yaml +89 -0
  8. matchify-0.0.1/AGENTS.md +75 -0
  9. matchify-0.0.1/CONTRIBUTING.md +44 -0
  10. matchify-0.0.1/PKG-INFO +247 -0
  11. matchify-0.0.1/README.md +225 -0
  12. matchify-0.0.1/design.md +341 -0
  13. matchify-0.0.1/find_new_issue.py +2286 -0
  14. matchify-0.0.1/find_new_issue_2.py +995 -0
  15. matchify-0.0.1/limitations.md +88 -0
  16. matchify-0.0.1/pyproject.toml +94 -0
  17. matchify-0.0.1/src/matchify/__init__.py +11 -0
  18. matchify-0.0.1/src/matchify/__main__.py +6 -0
  19. matchify-0.0.1/src/matchify/access_path.py +285 -0
  20. matchify-0.0.1/src/matchify/assumptions.py +58 -0
  21. matchify-0.0.1/src/matchify/capture_patterns.py +98 -0
  22. matchify-0.0.1/src/matchify/cli.py +193 -0
  23. matchify-0.0.1/src/matchify/compiler.py +253 -0
  24. matchify-0.0.1/src/matchify/conditions.py +492 -0
  25. matchify-0.0.1/src/matchify/facts.py +356 -0
  26. matchify-0.0.1/src/matchify/pattern_builder.py +381 -0
  27. matchify-0.0.1/src/matchify/patterns.py +164 -0
  28. matchify-0.0.1/src/matchify/safety.py +69 -0
  29. matchify-0.0.1/src/matchify/transform.py +138 -0
  30. matchify-0.0.1/test-repos.json +72 -0
  31. matchify-0.0.1/test-repos.py +424 -0
  32. matchify-0.0.1/tests/helpers.py +64 -0
  33. matchify-0.0.1/tests/samples/.gitkeep +0 -0
  34. matchify-0.0.1/tests/test_access_path.py +178 -0
  35. matchify-0.0.1/tests/test_cli.py +678 -0
  36. matchify-0.0.1/tests/test_edge_cases.py +1892 -0
  37. matchify-0.0.1/tests/test_find_new_issue_2.py +41 -0
  38. matchify-0.0.1/tests/test_generated_roundtrip.py +88 -0
  39. matchify-0.0.1/tests/test_guard_rendering.py +33 -0
  40. matchify-0.0.1/tests/test_readme.py +60 -0
  41. matchify-0.0.1/tests/test_transform.py +5223 -0
@@ -0,0 +1,20 @@
1
+ # Changes here will be overwritten by Copier
2
+ _commit: v0.0.6
3
+ _src_path: git@github.com:15r10nk/project-template.git
4
+ full_coverage: false
5
+ github_user: 15r10nk
6
+ insider_only: false
7
+ module_name: matchify
8
+ project_description: converts if-else code to match statements
9
+ project_main: true
10
+ project_name: matchify
11
+ pypy_versions:
12
+ - pypy3.10
13
+ python_versions:
14
+ - '3.10'
15
+ - '3.11'
16
+ - '3.12'
17
+ - '3.13'
18
+ - '3.14'
19
+ show_project_logo: false
20
+ use_mkdocs: false
@@ -0,0 +1 @@
1
+ github: 15r10nk
@@ -0,0 +1,23 @@
1
+ name: General Setup
2
+ description: setup Python and uv
3
+ inputs:
4
+ python-version: # id of input
5
+ description: the python version to use
6
+ required: false
7
+
8
+ default: '3.13'
9
+
10
+ runs:
11
+ using: composite
12
+ steps:
13
+ - name: Install uv
14
+ uses: astral-sh/setup-uv@v5
15
+ with:
16
+ python-version: ${{inputs.python-version}}
17
+
18
+ - name: Set up Python
19
+ uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
20
+ with:
21
+ python-version: ${{inputs.python-version}}
22
+ architecture: x64
23
+ allow-prereleases: true
@@ -0,0 +1,21 @@
1
+
2
+ ## Description
3
+ <!--
4
+ Please provide a clear and concise description of what this pull request does.
5
+ Create an issue first if you want to make bigger changes.
6
+ -->
7
+
8
+ ## Related Issue(s)
9
+ <!--
10
+ - Closes #123 (replace with relevant issue number(s), if applicable)
11
+ - Related to #456
12
+ -->
13
+
14
+ ## Checklist
15
+ - [ ] I have tested my changes thoroughly.
16
+ - [ ] I have added/updated relevant documentation.
17
+ - [ ] I have added tests for new functionality (if applicable).
18
+ - [ ] I have reviewed my own code for errors.
19
+ - I used:
20
+ - [ ] semantic commits (`feat:`, `fix:`, etc.)
21
+ - [ ] or my commits can be squashed during merge
@@ -0,0 +1,156 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches: [main]
7
+
8
+ jobs:
9
+
10
+ test:
11
+ runs-on: ${{matrix.os}}
12
+
13
+ strategy:
14
+ matrix:
15
+ python-version: ['3.10', '3.11', '3.12', '3.13', '3.14', pypy3.10]
16
+
17
+ os: [ubuntu-latest, windows-latest, macos-13]
18
+ is_insider:
19
+ - ${{contains(github.repository, '-insiders/')}}
20
+
21
+ exclude:
22
+ - is_insider: true
23
+ os: windows-latest
24
+ - is_insider: true
25
+ os: macos-13
26
+
27
+ - os: macos-13
28
+ python-version: '3.11'
29
+ - os: windows-latest
30
+ python-version: '3.11'
31
+
32
+ - os: macos-13
33
+ python-version: '3.12'
34
+ - os: windows-latest
35
+ python-version: '3.12'
36
+
37
+ - os: macos-13
38
+ python-version: '3.13'
39
+ - os: windows-latest
40
+ python-version: '3.13'
41
+
42
+ - os: macos-13
43
+ python-version: pypy3.10
44
+ - os: windows-latest
45
+ python-version: pypy3.10
46
+
47
+
48
+
49
+ env:
50
+ TOP: ${{github.workspace}}
51
+ steps:
52
+ - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
53
+ - uses: ./.github/actions/setup
54
+ with:
55
+ python-version: ${{matrix.python-version}}
56
+
57
+ - run: |
58
+ uv run -m ${{ matrix.os == 'ubuntu-latest' && 'coverage run -m' || '' }} pytest -q
59
+ - run: |
60
+ uv run -m coverage combine
61
+ mv .coverage .coverage.${{ matrix.python-version }}-${{matrix.os}}-${{strategy.job-index}}
62
+ if: matrix.os == 'ubuntu-latest'
63
+
64
+ - name: Upload coverage data
65
+ uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
66
+ if: matrix.os == 'ubuntu-latest'
67
+ with:
68
+ name: coverage-data-${{github.run_id}}-${{ matrix.python-version }}-${{matrix.os}}-${{strategy.job-index}}
69
+ path: .coverage.*
70
+ include-hidden-files: true
71
+ if-no-files-found: ignore
72
+
73
+ coverage:
74
+ name: Combine & check coverage
75
+ env:
76
+ TOP: ${{github.workspace}}
77
+ if: always()
78
+ needs: test
79
+ runs-on: ubuntu-latest
80
+
81
+ steps:
82
+ - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
83
+ - uses: ./.github/actions/setup
84
+
85
+ - uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4.1.9
86
+ with:
87
+ pattern: coverage-data-${{github.run_id}}-*
88
+ merge-multiple: true
89
+
90
+ - name: Combine coverage & fail if it's <100%
91
+ run: |
92
+ uv pip install --upgrade coverage[toml]
93
+
94
+ coverage combine
95
+ coverage html --skip-covered --skip-empty
96
+
97
+ # Report and write to summary.
98
+ coverage report --format=markdown >> $GITHUB_STEP_SUMMARY
99
+
100
+ # Report again and fail if under 100%.
101
+ coverage report --fail-under=100
102
+
103
+ - name: Upload HTML report if check failed
104
+ uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
105
+ with:
106
+ name: html-report
107
+ path: htmlcov
108
+ if: ${{ failure() }}
109
+
110
+
111
+ publish:
112
+ name: Publish new release
113
+ runs-on: ubuntu-latest
114
+ needs: [test, coverage]
115
+ if: github.event_name == 'push' && !contains(github.repository, '-insiders/')
116
+ environment: pypi
117
+ permissions:
118
+ # IMPORTANT: this permission is mandatory for Trusted Publishing
119
+ id-token: write
120
+ # this permission is mandatory to create github releases
121
+ contents: write
122
+
123
+ steps:
124
+ - name: Checkout main
125
+ uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
126
+ with:
127
+ fetch-depth: 0
128
+ - uses: ./.github/actions/setup
129
+
130
+ - name: Check if the commit has a vx.y.z tag
131
+ id: check-version
132
+ run: |
133
+ if git tag --list --points-at ${{ github.sha }} | grep -q -E '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
134
+ echo "is new version"
135
+ echo "should_continue=true" >> "$GITHUB_OUTPUT"
136
+ else
137
+ echo "is not a new version"
138
+ echo "should_continue=false" >> "$GITHUB_OUTPUT"
139
+ fi
140
+
141
+ - run: uv pip install hatch scriv
142
+ if: ${{ steps.check-version.outputs.should_continue == 'true' }}
143
+
144
+ - name: build package
145
+ if: ${{ steps.check-version.outputs.should_continue == 'true' }}
146
+ run: hatch build
147
+
148
+ - name: Publish package distributions to PyPI
149
+ if: ${{ steps.check-version.outputs.should_continue == 'true' }}
150
+ uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
151
+
152
+ - name: create github release
153
+ if: ${{ steps.check-version.outputs.should_continue == 'true' }}
154
+ env:
155
+ GITHUB_TOKEN: ${{ github.token }}
156
+ run: scriv github-release
@@ -0,0 +1,124 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ PKGINFO
29
+
30
+ # PyInstaller
31
+ *.manifest
32
+ *.spec
33
+
34
+ # Unit test / coverage reports
35
+ htmlcov/
36
+ .tox/
37
+ .nox/
38
+ .coverage
39
+ .coverage.*
40
+ .cache
41
+ nosetests.xml
42
+ coverage.xml
43
+ *.cover
44
+ *.py,cover
45
+ .hypothesis/
46
+ .pytest_cache/
47
+
48
+ # Translations
49
+ *.mo
50
+ *.pot
51
+
52
+ # Django stuff:
53
+ *.log
54
+ local_settings.py
55
+ db.sqlite3
56
+ db.sqlite3-journal
57
+
58
+ # Flask stuff:
59
+ instance/
60
+ .webassets-cache
61
+
62
+ # Scrapy stuff:
63
+ .scrapy
64
+
65
+ # Sphinx documentation
66
+ docs/_build/
67
+
68
+ # PyBuilder
69
+ target/
70
+
71
+ # Jupyter Notebook
72
+ .ipynb_checkpoints
73
+
74
+ # IPython
75
+ profile_default/
76
+ ipython_config.py
77
+
78
+ # pyenv
79
+ .python-version
80
+
81
+ # pipenv
82
+ Pipfile.lock
83
+
84
+ # PEP 582
85
+ __pypackages__/
86
+
87
+ # Celery stuff
88
+ celerybeat-schedule
89
+ celerybeat.pid
90
+
91
+ # SageMath parsed files
92
+ *.sage.py
93
+
94
+ # Environments
95
+ .env
96
+ .venv
97
+ env/
98
+ venv/
99
+ ENV/
100
+ env.bak/
101
+ venv.bak/
102
+
103
+ # Spyder project settings
104
+ .spyderproject
105
+ .spyproject
106
+
107
+ # Rope project settings
108
+ .ropeproject
109
+
110
+ # mkdocs documentation
111
+ /site
112
+
113
+ # mypy
114
+ .mypy_cache/
115
+ .dmypy.json
116
+ dmypy.json
117
+
118
+ # Pyre type checker
119
+ .pyre/
120
+
121
+ # Test files
122
+ test_*.py
123
+ !tests/test_*.py
124
+ uv.lock
@@ -0,0 +1,89 @@
1
+ ci:
2
+ autofix_pr: false
3
+ autoupdate_commit_msg: 'style: pre-commit autoupdate'
4
+ autofix_commit_msg: 'style: pre-commit fixed styling'
5
+ autoupdate_schedule: monthly
6
+
7
+ repos:
8
+ - hooks:
9
+ - id: check-ast
10
+ - id: check-merge-conflict
11
+ - id: trailing-whitespace
12
+ - id: mixed-line-ending
13
+ - id: fix-byte-order-marker
14
+ - id: check-case-conflict
15
+ - id: check-json
16
+ - id: end-of-file-fixer
17
+ repo: https://github.com/pre-commit/pre-commit-hooks
18
+ rev: v5.0.0
19
+ - hooks:
20
+ - args:
21
+ - --in-place
22
+ - --expand-star-imports
23
+ - --remove-all-unused-imports
24
+ - --ignore-init-module-imports
25
+ id: autoflake
26
+ repo: https://github.com/myint/autoflake
27
+ rev: v2.3.1
28
+
29
+ - repo: https://github.com/asottile/setup-cfg-fmt
30
+ rev: v2.8.0
31
+ hooks:
32
+ - id: setup-cfg-fmt
33
+
34
+ - repo: https://github.com/pycqa/isort
35
+ rev: 6.0.1
36
+ hooks:
37
+ - id: isort
38
+ name: isort (python)
39
+
40
+ - hooks:
41
+ - args:
42
+ - --py310-plus
43
+ id: pyupgrade
44
+ repo: https://github.com/asottile/pyupgrade
45
+ rev: v3.20.0
46
+ - hooks:
47
+ - id: black
48
+ repo: https://github.com/psf/black
49
+ rev: 25.1.0
50
+ - hooks:
51
+ - id: blacken-docs
52
+ args: [-l80]
53
+ repo: https://github.com/adamchainz/blacken-docs
54
+ rev: 1.19.1
55
+
56
+
57
+ - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
58
+ rev: v2.15.0
59
+ hooks:
60
+ - id: pretty-format-yaml
61
+ args: [--autofix, --indent, '2']
62
+
63
+ - hooks:
64
+ - id: commitizen
65
+ stages:
66
+ - commit-msg
67
+ repo: https://github.com/commitizen-tools/commitizen
68
+ rev: v4.8.3
69
+
70
+ - repo: https://github.com/abravalheri/validate-pyproject
71
+ rev: v0.24.1
72
+ hooks:
73
+ - id: validate-pyproject
74
+
75
+ - repo: https://github.com/henryiii/validate-pyproject-schema-store
76
+ rev: 2025.04.28
77
+ hooks:
78
+ - id: validate-pyproject
79
+
80
+
81
+ - repo: https://github.com/rhysd/actionlint
82
+ rev: v1.7.7
83
+ hooks:
84
+ - id: actionlint
85
+
86
+ - repo: https://github.com/crate-ci/typos
87
+ rev: v1
88
+ hooks:
89
+ - id: typos
@@ -0,0 +1,75 @@
1
+ # Agent Instructions for Matchify
2
+
3
+ Matchify converts eligible `if`/`elif`/`else` chains into Python 3.10+
4
+ `match` statements. It uses LibCST so transformations preserve source formatting.
5
+
6
+ ## Code map
7
+
8
+ - `src/matchify/transform.py` orchestrates the LibCST transformation and exposes
9
+ `transform_code()`.
10
+ - `src/matchify/compiler.py` normalizes complete chains and compiles them into
11
+ `match` statements.
12
+ - `src/matchify/conditions.py` parses conditions into typed predicates.
13
+ - `src/matchify/access_path.py` models subjects and nested attribute/index paths.
14
+ - `src/matchify/pattern_builder.py` lowers predicates into pattern facts and
15
+ residual guards.
16
+ - `src/matchify/facts.py` and `src/matchify/patterns.py` define and render the
17
+ pattern IR.
18
+ - `src/matchify/safety.py` checks whether transformations preserve behavior.
19
+ - `src/matchify/capture_patterns.py` turns eligible assignments into capture
20
+ patterns.
21
+ - `src/matchify/cli.py` implements file processing and the CLI;
22
+ `src/matchify/__main__.py` is the `python -m matchify` entry point.
23
+
24
+ ## Transformation principles
25
+
26
+ - Preserve runtime semantics. Be conservative when evaluation count/order,
27
+ side effects, bindings, or pattern behavior are uncertain.
28
+ - Convert only complete chains with at least one `elif`; leave unsupported
29
+ conditions unchanged.
30
+ - Conditions in a converted chain must resolve to a compatible match subject.
31
+ - Literal, singleton identity, class, sequence, OR, guard, nested, and capture
32
+ patterns are supported where the builder and safety checks can prove them
33
+ valid. Treat the behavior-focused tests as the authoritative specification.
34
+ - Non-literal expressions must not accidentally become capture patterns.
35
+ - A walrus expression that must execute as part of a condition belongs in a
36
+ guard, not in the match subject or structural pattern.
37
+
38
+ ## LibCST notes
39
+
40
+ - For `elif`, `cst.If.orelse` is another `cst.If`; for `else`, it is a
41
+ `cst.Else` whose `body` contains the suite.
42
+ - Use `.deep_equals()` for structural CST comparisons.
43
+ - Build a wildcard case with `cst.MatchAs(pattern=None, name=None)`.
44
+ - Pass concrete pattern nodes such as `cst.MatchValue` directly to
45
+ `cst.MatchCase.pattern`; `cst.MatchPattern` is abstract.
46
+
47
+ ## Development workflow
48
+
49
+ 1. Add a minimal behavior-focused regression test before or alongside a fix.
50
+ 2. Make changes in the appropriate parsing, lowering, rendering, or safety
51
+ layer rather than adding syntax-specific exceptions to orchestration code.
52
+ 3. Exercise transformations through `transform_code()`, `convert_file()`, or
53
+ the CLI when practical.
54
+ 4. Run the focused test, then the full suite:
55
+
56
+ ```bash
57
+ uv run pytest tests/test_transform.py -k <case>
58
+ uv run pytest
59
+ ```
60
+
61
+ The test suite is organized as follows:
62
+
63
+ - `tests/test_transform.py` and `tests/test_edge_cases.py`: source-to-source
64
+ behavior and rejection cases.
65
+ - `tests/test_cli.py`: file processing and CLI behavior.
66
+ - `tests/test_generated_roundtrip.py`: generated programs whose runtime traces
67
+ are compared before and after transformation.
68
+
69
+ Use `textwrap.dedent()` for multiline source fixtures and temporary directories
70
+ for filesystem tests. Test both successful conversions and nearby cases that
71
+ must remain unchanged.
72
+
73
+ For a complex generated-roundtrip failure, `pysource-minimize` can reduce a
74
+ reproducer. Use a precise checker that transforms the program and detects the
75
+ specific trace mismatch; broad checks can minimize to an unrelated failure.
@@ -0,0 +1,44 @@
1
+ # Contributing
2
+ Contributions are welcome.
3
+ Please create an issue before writing a pull request so we can discuss what needs to be changed.
4
+
5
+ # Testing
6
+ The code can be tested with [hatch](https://hatch.pypa.io/latest/)
7
+
8
+ * `hatch run cov:test` can be used to test all supported python versions and to check for coverage.
9
+ * `hatch run +py=3.10 all:test -- --sw` runs pytest for python 3.10 with the `--sw` argument.
10
+
11
+ ## Testing against external repositories
12
+
13
+ `test-repos.py` clones the projects configured in `test-repos.json`, prepares
14
+ their test environments, runs a baseline test suite, applies the local Matchify
15
+ checkout, validates changed files with the project's Python interpreter, and
16
+ reruns the tests.
17
+
18
+ Run one configured repository:
19
+
20
+ ```bash
21
+ uv run python test-repos.py django-rest-framework \
22
+ --workspace /tmp/matchify-drf
23
+ ```
24
+
25
+ Run every configured repository or list the available names:
26
+
27
+ ```bash
28
+ uv run python test-repos.py --workspace /tmp/matchify-repos
29
+ uv run python test-repos.py --list
30
+ ```
31
+
32
+ The workspace must be new. Each repository gets isolated clone, setup, test,
33
+ Matchify, and syntax logs. A combined `summary.json` is written at the
34
+ workspace root. Use `--skip-baseline` to omit the initial test run or
35
+ `--skip-tests` to perform setup, conversion, and syntax validation only.
36
+
37
+ Repository entries may configure `url`, `ref`, clone arguments, source and
38
+ excluded paths, extra Matchify arguments, setup commands, the target Python
39
+ command, the test command, environment variables, batch size, and timeout. The
40
+ `{root}`, `{repo}`, and `{workspace}` placeholders are available in commands and
41
+ environment values.
42
+
43
+ # Commits
44
+ Please use [pre-commit](https://pre-commit.com/) for your commits.