undersort 0.1.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,86 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ tags:
7
+ - "v*"
8
+ pull_request:
9
+ branches: [main]
10
+
11
+ jobs:
12
+ lint:
13
+ name: Lint
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+
18
+ - name: Install uv
19
+ uses: astral-sh/setup-uv@v4
20
+ with:
21
+ enable-cache: true
22
+
23
+ - name: Set up Python
24
+ uses: actions/setup-python@v5
25
+ with:
26
+ python-version: "3.11"
27
+
28
+ - name: Install dependencies
29
+ run: uv sync
30
+
31
+ - name: Run ruff format check
32
+ run: uv run ruff format --check
33
+
34
+ - name: Run ruff linter
35
+ run: uv run ruff check
36
+
37
+ test:
38
+ name: Test
39
+ runs-on: ubuntu-latest
40
+ strategy:
41
+ matrix:
42
+ python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
43
+ steps:
44
+ - uses: actions/checkout@v4
45
+
46
+ - name: Install uv
47
+ uses: astral-sh/setup-uv@v4
48
+ with:
49
+ enable-cache: true
50
+
51
+ - name: Set up Python ${{ matrix.python-version }}
52
+ uses: actions/setup-python@v5
53
+ with:
54
+ python-version: ${{ matrix.python-version }}
55
+
56
+ - name: Install dependencies
57
+ run: uv sync
58
+
59
+ - name: Run tests
60
+ run: uv run pytest -v --cov=undersort --cov-report=term-missing
61
+
62
+ publish:
63
+ name: Publish to PyPI
64
+ runs-on: ubuntu-latest
65
+ needs: [lint, test]
66
+ if: startsWith(github.ref, 'refs/tags/v')
67
+ permissions:
68
+ id-token: write
69
+ steps:
70
+ - uses: actions/checkout@v4
71
+
72
+ - name: Install uv
73
+ uses: astral-sh/setup-uv@v4
74
+
75
+ - name: Set up Python
76
+ uses: actions/setup-python@v5
77
+ with:
78
+ python-version: "3.11"
79
+
80
+ - name: Build package
81
+ run: uv build
82
+
83
+ - name: Publish to PyPI
84
+ run: uv publish
85
+ env:
86
+ UV_PUBLISH_TOKEN: ${{ secrets.PYPI_TOKEN }}
@@ -0,0 +1,174 @@
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
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # uv
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://docs.astral.sh/uv/concepts/projects/#lockfiles
102
+ #uv.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
161
+
162
+ # Nuitka builds
163
+ *.onefile-build/
164
+ *.build/
165
+ *.dist/
166
+
167
+ # macOS
168
+ .DS_Store
169
+
170
+ # IDE history
171
+ .history
172
+
173
+ # Ruff cache
174
+ .ruff_cache/
@@ -0,0 +1,11 @@
1
+ # Sample pre-commit configuration
2
+ # To use: pip install pre-commit && pre-commit install
3
+
4
+ repos:
5
+ - repo: local
6
+ hooks:
7
+ - id: undersort
8
+ name: undersort
9
+ entry: uv run undersort
10
+ language: system
11
+ types: [python]
@@ -0,0 +1,7 @@
1
+ - id: undersort
2
+ name: undersort
3
+ description: Sort class methods by visibility (public, protected, private)
4
+ entry: undersort
5
+ language: python
6
+ types: [python]
7
+ require_serial: false
@@ -0,0 +1 @@
1
+ 3.11
@@ -0,0 +1,3 @@
1
+ {
2
+ "editor.formatOnSave": true,
3
+ }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Vladimir Kirilenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,29 @@
1
+ .PHONY: install lint test test-cov format check clean demo demo-check demo-diff
2
+
3
+ install:
4
+ uv sync
5
+
6
+ lint:
7
+ uv run ruff format
8
+ uv run ruff check --fix --unsafe-fixes --exit-zero
9
+
10
+ format:
11
+ uv run ruff format
12
+
13
+ check:
14
+ uv run ruff check
15
+
16
+ test:
17
+ uv run pytest -v
18
+
19
+ test-cov:
20
+ uv run pytest --cov=undersort --cov-report=term-missing --cov-report=html
21
+
22
+ clean:
23
+ rm -rf .venv __pycache__ .pytest_cache .mypy_cache .ruff_cache
24
+ find . -type d -name "__pycache__" -exec rm -rf {} +
25
+ find . -type f -name "*.pyc" -delete
26
+
27
+ update:
28
+ uv lock --upgrade
29
+ uv sync
@@ -0,0 +1,241 @@
1
+ Metadata-Version: 2.4
2
+ Name: undersort
3
+ Version: 0.1.0
4
+ Summary: A tool to sort class methods by visibility (public, protected, private)
5
+ Project-URL: Homepage, https://github.com/kivicode/undersort
6
+ Project-URL: Repository, https://github.com/kivicode/undersort
7
+ Project-URL: Issues, https://github.com/kivicode/undersort/issues
8
+ Author-email: KiviCode <kivicode.dev@gmail.com>
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: class-methods,code-formatter,linter,python,sorting
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Classifier: Topic :: Software Development :: Quality Assurance
23
+ Requires-Python: >=3.8
24
+ Requires-Dist: libcst>=1.1.0
25
+ Requires-Dist: rich>=13.0.0
26
+ Description-Content-Type: text/markdown
27
+
28
+ # undersort
29
+
30
+ A Python tool that automatically sorts class methods by visibility (public, protected, private) and type (class, static, instance).
31
+
32
+ ## Features
33
+
34
+ - Automatically reorders class methods based on visibility and method type
35
+ - Two-level sorting: primary by visibility, secondary by method type
36
+ - Fully configurable ordering via `pyproject.toml`
37
+ - Pre-commit hook integration
38
+ - Colored output for better readability
39
+ - Check mode for CI/CD validation
40
+ - Diff mode to preview changes
41
+
42
+ ## Installation
43
+
44
+ ```bash
45
+ # Using uv (recommended)
46
+ uv pip install -e .
47
+
48
+ # Using pip
49
+ pip install -e .
50
+ ```
51
+
52
+ ## Configuration
53
+
54
+ Configure the method ordering in your `pyproject.toml`:
55
+
56
+ ```toml
57
+ [tool.undersort]
58
+ # Method visibility ordering (primary sort)
59
+ # Options: "public", "protected", "private"
60
+ order = ["public", "protected", "private"]
61
+
62
+ # Method type ordering within each visibility level (secondary sort, optional)
63
+ # Options: "class" (classmethod), "static" (staticmethod), "instance" (regular methods)
64
+ # Default: ["class", "static", "instance"]
65
+ method_type_order = ["class", "static", "instance"]
66
+ ```
67
+
68
+ ### Method Visibility Rules
69
+
70
+ - **Public methods**: No underscore prefix (e.g., `def method()`) or magic methods (e.g., `__init__`, `__str__`)
71
+ - **Protected methods**: Single underscore prefix (e.g., `def _method()`)
72
+ - **Private methods**: Double underscore prefix, not magic (e.g., `def __method()`)
73
+
74
+ ### Method Type Rules
75
+
76
+ - **Class methods**: Decorated with `@classmethod`
77
+ - **Static methods**: Decorated with `@staticmethod`
78
+ - **Instance methods**: Regular methods (no special decorator)
79
+
80
+ ### Sorting Behavior
81
+
82
+ Methods are sorted in two levels:
83
+
84
+ 1. **Primary**: By visibility (public → protected → private)
85
+ 2. **Secondary**: Within each visibility level, by method type (class → static → instance by default)
86
+
87
+ Example: With default configuration, the order would be:
88
+
89
+ 1. Public class methods
90
+ 2. Public static methods
91
+ 3. Public instance methods
92
+ 4. Protected class methods
93
+ 5. Protected static methods
94
+ 6. Protected instance methods
95
+ 7. Private class methods
96
+ 8. Private static methods
97
+ 9. Private instance methods
98
+
99
+ ## Usage
100
+
101
+ ### Command Line
102
+
103
+ ```bash
104
+ # Sort a single file
105
+ undersort example.py
106
+
107
+ # Sort multiple files
108
+ undersort file1.py file2.py file3.py
109
+
110
+ # Sort all Python files in a directory (recursive by default)
111
+ undersort src/
112
+
113
+ # Sort all Python files in current directory and subdirectories
114
+ undersort .
115
+
116
+ # Non-recursive directory sorting (only files in the directory, not subdirectories)
117
+ undersort src/ --no-recursive
118
+
119
+ # Wildcards work too (expanded by shell)
120
+ undersort *.py
121
+ undersort src/**/*.py
122
+
123
+ # Check if files need sorting (useful for CI)
124
+ undersort --check example.py
125
+ undersort --check src/
126
+
127
+ # Show diff of changes
128
+ undersort --diff example.py
129
+
130
+ # Combine flags
131
+ undersort --check --diff src/
132
+ ```
133
+
134
+ **Note**: By default, undersort excludes common directories like `.venv`, `.git`, `__pycache__`, `.pytest_cache`, `.mypy_cache`, `.ruff_cache`, and `node_modules` when scanning directories.
135
+
136
+ ### Pre-commit Integration
137
+
138
+ Add to your `.pre-commit-config.yaml`:
139
+
140
+ ```yaml
141
+ repos:
142
+ - repo: local
143
+ hooks:
144
+ - id: undersort
145
+ name: undersort
146
+ entry: uv run undersort
147
+ language: system
148
+ types: [python]
149
+ ```
150
+
151
+ Then install the hook:
152
+
153
+ ```bash
154
+ pip install pre-commit
155
+ pre-commit install
156
+ ```
157
+
158
+ ## Example
159
+
160
+ ### Before
161
+
162
+ ```python
163
+ class Example:
164
+ def _protected_instance(self):
165
+ pass
166
+
167
+ @staticmethod
168
+ def public_static():
169
+ pass
170
+
171
+ def __init__(self):
172
+ pass
173
+
174
+ @classmethod
175
+ def _protected_class(cls):
176
+ pass
177
+
178
+ def public_instance(self):
179
+ pass
180
+
181
+ def __private_method(self):
182
+ pass
183
+
184
+ @classmethod
185
+ def public_class(cls):
186
+ pass
187
+ ```
188
+
189
+ ### After (with default config)
190
+
191
+ ```python
192
+ class Example:
193
+ @classmethod
194
+ def public_class(cls):
195
+ pass
196
+
197
+ @staticmethod
198
+ def public_static():
199
+ pass
200
+
201
+ def __init__(self):
202
+ pass
203
+
204
+ def public_instance(self):
205
+ pass
206
+
207
+ @classmethod
208
+ def _protected_class(cls):
209
+ pass
210
+
211
+ def _protected_instance(self):
212
+ pass
213
+
214
+ def __private_method(self):
215
+ pass
216
+ ```
217
+
218
+ The methods are now organized by:
219
+
220
+ 1. **Visibility**: public (including `__init__`) → protected → private
221
+ 2. **Type** (within each visibility): class → static → instance
222
+
223
+ ## Development
224
+
225
+ ```bash
226
+ # Install dependencies
227
+ uv sync
228
+
229
+ # Run on example file
230
+ uv run undersort example.py
231
+
232
+ # Test with check mode
233
+ uv run undersort --check example.py
234
+
235
+ # View diff
236
+ uv run undersort --diff example.py
237
+ ```
238
+
239
+ ## License
240
+
241
+ MIT