dlen 0.0.2__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.
- dlen-0.2.0/.gitignore +101 -0
- dlen-0.2.0/LICENSE +21 -0
- dlen-0.2.0/PKG-INFO +157 -0
- dlen-0.2.0/README.md +131 -0
- dlen-0.2.0/pyproject.toml +91 -0
- dlen-0.2.0/src/dlen/__init__.py +13 -0
- dlen-0.2.0/src/dlen/cli.py +89 -0
- dlen-0.2.0/src/dlen/core.py +140 -0
- dlen-0.2.0/src/dlen/py.typed +0 -0
- dlen-0.2.0/tests/test_cli.py +134 -0
- dlen-0.2.0/tests/test_core.py +189 -0
- dlen-0.0.2/PKG-INFO +0 -83
- dlen-0.0.2/dlen/__init__.py +0 -1
- dlen-0.0.2/dlen/__version__.py +0 -1
- dlen-0.0.2/dlen/dlen.py +0 -173
- dlen-0.0.2/dlen.egg-info/PKG-INFO +0 -83
- dlen-0.0.2/dlen.egg-info/SOURCES.txt +0 -9
- dlen-0.0.2/dlen.egg-info/dependency_links.txt +0 -1
- dlen-0.0.2/dlen.egg-info/entry_points.txt +0 -3
- dlen-0.0.2/dlen.egg-info/top_level.txt +0 -1
- dlen-0.0.2/setup.cfg +0 -4
- dlen-0.0.2/setup.py +0 -105
dlen-0.2.0/.gitignore
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
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
|
+
env/
|
|
12
|
+
build/
|
|
13
|
+
develop-eggs/
|
|
14
|
+
dist/
|
|
15
|
+
downloads/
|
|
16
|
+
eggs/
|
|
17
|
+
.eggs/
|
|
18
|
+
lib/
|
|
19
|
+
lib64/
|
|
20
|
+
parts/
|
|
21
|
+
sdist/
|
|
22
|
+
var/
|
|
23
|
+
wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
|
|
28
|
+
# PyInstaller
|
|
29
|
+
# Usually these files are written by a python script from a template
|
|
30
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
31
|
+
*.manifest
|
|
32
|
+
*.spec
|
|
33
|
+
|
|
34
|
+
# Installer logs
|
|
35
|
+
pip-log.txt
|
|
36
|
+
pip-delete-this-directory.txt
|
|
37
|
+
|
|
38
|
+
# Unit test / coverage reports
|
|
39
|
+
htmlcov/
|
|
40
|
+
.tox/
|
|
41
|
+
.coverage
|
|
42
|
+
.coverage.*
|
|
43
|
+
.cache
|
|
44
|
+
nosetests.xml
|
|
45
|
+
coverage.xml
|
|
46
|
+
*.cover
|
|
47
|
+
.hypothesis/
|
|
48
|
+
|
|
49
|
+
# Translations
|
|
50
|
+
*.mo
|
|
51
|
+
*.pot
|
|
52
|
+
|
|
53
|
+
# Django stuff:
|
|
54
|
+
*.log
|
|
55
|
+
local_settings.py
|
|
56
|
+
|
|
57
|
+
# Flask stuff:
|
|
58
|
+
instance/
|
|
59
|
+
.webassets-cache
|
|
60
|
+
|
|
61
|
+
# Scrapy stuff:
|
|
62
|
+
.scrapy
|
|
63
|
+
|
|
64
|
+
# Sphinx documentation
|
|
65
|
+
docs/_build/
|
|
66
|
+
|
|
67
|
+
# PyBuilder
|
|
68
|
+
target/
|
|
69
|
+
|
|
70
|
+
# Jupyter Notebook
|
|
71
|
+
.ipynb_checkpoints
|
|
72
|
+
|
|
73
|
+
# pyenv
|
|
74
|
+
.python-version
|
|
75
|
+
|
|
76
|
+
# celery beat schedule file
|
|
77
|
+
celerybeat-schedule
|
|
78
|
+
|
|
79
|
+
# SageMath parsed files
|
|
80
|
+
*.sage.py
|
|
81
|
+
|
|
82
|
+
# dotenv
|
|
83
|
+
.env
|
|
84
|
+
|
|
85
|
+
# virtualenv
|
|
86
|
+
.venv
|
|
87
|
+
venv/
|
|
88
|
+
ENV/
|
|
89
|
+
|
|
90
|
+
# Spyder project settings
|
|
91
|
+
.spyderproject
|
|
92
|
+
.spyproject
|
|
93
|
+
|
|
94
|
+
# Rope project settings
|
|
95
|
+
.ropeproject
|
|
96
|
+
|
|
97
|
+
# mkdocs documentation
|
|
98
|
+
/site
|
|
99
|
+
|
|
100
|
+
# mypy
|
|
101
|
+
.mypy_cache/
|
dlen-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Endika Iglesias
|
|
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.
|
dlen-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: dlen
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Check how long your functions and classes are
|
|
5
|
+
Project-URL: Homepage, https://github.com/Endika/dlen
|
|
6
|
+
Project-URL: Repository, https://github.com/Endika/dlen
|
|
7
|
+
Project-URL: Issues, https://github.com/Endika/dlen/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/Endika/dlen/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Endika Iglesias <endika2@gmail.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: code-quality,complexity,linter,refactoring,static-analysis
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# dlen
|
|
28
|
+
|
|
29
|
+
Check how long your functions and classes are.
|
|
30
|
+
|
|
31
|
+
A long function is the cheapest smell to detect and the most reliable one to act on.
|
|
32
|
+
`dlen` tells you which ones crossed the line, and where.
|
|
33
|
+
|
|
34
|
+
```console
|
|
35
|
+
$ dlen src/
|
|
36
|
+
src/importer.py:42:1: DL001 function 'process_batch' is 34 lines (max 20)
|
|
37
|
+
src/importer.py:88:5: DL001 function 'validate' is 14 lines (warn 12)
|
|
38
|
+
src/models.py:7:1: DL002 class 'LegacyRecord' is 812 lines (max 500)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```console
|
|
44
|
+
uv tool install dlen # or: pipx install dlen, pip install dlen
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Python 3.10 or newer. No dependencies.
|
|
48
|
+
|
|
49
|
+
## Use
|
|
50
|
+
|
|
51
|
+
```console
|
|
52
|
+
dlen src/ tests/ # files, directories, or both
|
|
53
|
+
dlen . --max-function 30 # your project, your thresholds
|
|
54
|
+
dlen . --output-format=json # for other tools
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
| Flag | Default | Meaning |
|
|
58
|
+
| --- | --- | --- |
|
|
59
|
+
| `--warn-function N` | 30 | report a function above N lines, without failing |
|
|
60
|
+
| `--max-function N` | 50 | fail on a function above N lines |
|
|
61
|
+
| `--max-class N` | 500 | fail on a class above N lines |
|
|
62
|
+
| `--output-format` | `full` | `full`, `github` or `json` |
|
|
63
|
+
|
|
64
|
+
A function's length includes its decorators — they are part of what you read before
|
|
65
|
+
you understand it.
|
|
66
|
+
|
|
67
|
+
### Where the defaults come from
|
|
68
|
+
|
|
69
|
+
They were measured, not chosen. Across 29,000 functions in the Python standard library,
|
|
70
|
+
numpy, Pillow, rich, pytest, httpx, mypy and coverage:
|
|
71
|
+
|
|
72
|
+
| | median | p75 | p90 | over 20 lines |
|
|
73
|
+
| --- | --- | --- | --- | --- |
|
|
74
|
+
| Python standard library | 7 | 14 | 30 | 16% |
|
|
75
|
+
| mypy | 7 | 17 | 37 | 21% |
|
|
76
|
+
| pytest | 9 | 18 | 34 | 22% |
|
|
77
|
+
| httpx | 9 | 19 | 34 | 23% |
|
|
78
|
+
| rich | 10 | 22 | 41 | 27% |
|
|
79
|
+
| Pillow | 10 | 23 | 51 | 27% |
|
|
80
|
+
| numpy | 14 | 46 | 82 | 43% |
|
|
81
|
+
|
|
82
|
+
The median function out there is 7 to 14 lines, which is reassuring and remarkably stable
|
|
83
|
+
across eight independent codebases. The tail is what a default has to answer to: a limit
|
|
84
|
+
of 20 flags a sixth of the standard library and a quarter of rich. That is not a warning,
|
|
85
|
+
it is a wall — and a tool that fires on a quarter of your code gets uninstalled on the
|
|
86
|
+
first day.
|
|
87
|
+
|
|
88
|
+
At 50 — the number `pylint` and `ruff` already use for statements — you flag around 5%.
|
|
89
|
+
That is a list you can work through on a Tuesday.
|
|
90
|
+
|
|
91
|
+
The class limit is a different story: 500 fires on about 1% of standard library classes,
|
|
92
|
+
and when it fires it is right. It stays where it was.
|
|
93
|
+
|
|
94
|
+
If you want the stricter Clean Code reading, it is one flag away:
|
|
95
|
+
|
|
96
|
+
```console
|
|
97
|
+
dlen src/ --warn-function 12 --max-function 20
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Versions before 0.2.0 shipped 12 and 20 as the defaults.
|
|
101
|
+
|
|
102
|
+
### Exit codes
|
|
103
|
+
|
|
104
|
+
| Code | When |
|
|
105
|
+
| --- | --- |
|
|
106
|
+
| `0` | nothing found, or only warnings |
|
|
107
|
+
| `1` | something was over a `max`, or a file could not be parsed |
|
|
108
|
+
| `2` | bad arguments |
|
|
109
|
+
|
|
110
|
+
Warnings never fail the build. That is the point of having two thresholds: one to
|
|
111
|
+
nudge, one to stop.
|
|
112
|
+
|
|
113
|
+
### Rules
|
|
114
|
+
|
|
115
|
+
| Code | Rule |
|
|
116
|
+
| --- | --- |
|
|
117
|
+
| `DL001` | function is too long |
|
|
118
|
+
| `DL002` | class is too long |
|
|
119
|
+
| `DL900` | file could not be read or parsed |
|
|
120
|
+
|
|
121
|
+
## In CI
|
|
122
|
+
|
|
123
|
+
`--output-format=github` puts each finding on the diff of the pull request instead
|
|
124
|
+
of burying it in a log:
|
|
125
|
+
|
|
126
|
+
```yaml
|
|
127
|
+
- run: uvx dlen src --output-format=github
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## As a library
|
|
131
|
+
|
|
132
|
+
The core returns data and prints nothing, so you can build your own reporting on it:
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from pathlib import Path
|
|
136
|
+
from dlen import Limits, check_paths
|
|
137
|
+
|
|
138
|
+
findings = check_paths([Path("src")], Limits(max_function=30))
|
|
139
|
+
for finding in findings:
|
|
140
|
+
print(finding.code, finding.path, finding.line, finding.message)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Every call returns a fresh list. Two runs never see each other's findings.
|
|
144
|
+
|
|
145
|
+
## How it measures
|
|
146
|
+
|
|
147
|
+
`dlen` parses your code with Python's own `ast` module, so it agrees with Python
|
|
148
|
+
about what a function is. It sees `async def`, decorators, nested functions and
|
|
149
|
+
methods, and it is not fooled by the word `def` inside a string or a comment.
|
|
150
|
+
|
|
151
|
+
> Versions before 0.1.0 matched text with regular expressions and had been broken on
|
|
152
|
+
> Python 3 since 2017. 0.1.0 is a rewrite: the counts are now correct rather than
|
|
153
|
+
> compatible, and the output carries file, line and column.
|
|
154
|
+
|
|
155
|
+
## Licence
|
|
156
|
+
|
|
157
|
+
MIT.
|
dlen-0.2.0/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# dlen
|
|
2
|
+
|
|
3
|
+
Check how long your functions and classes are.
|
|
4
|
+
|
|
5
|
+
A long function is the cheapest smell to detect and the most reliable one to act on.
|
|
6
|
+
`dlen` tells you which ones crossed the line, and where.
|
|
7
|
+
|
|
8
|
+
```console
|
|
9
|
+
$ dlen src/
|
|
10
|
+
src/importer.py:42:1: DL001 function 'process_batch' is 34 lines (max 20)
|
|
11
|
+
src/importer.py:88:5: DL001 function 'validate' is 14 lines (warn 12)
|
|
12
|
+
src/models.py:7:1: DL002 class 'LegacyRecord' is 812 lines (max 500)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```console
|
|
18
|
+
uv tool install dlen # or: pipx install dlen, pip install dlen
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Python 3.10 or newer. No dependencies.
|
|
22
|
+
|
|
23
|
+
## Use
|
|
24
|
+
|
|
25
|
+
```console
|
|
26
|
+
dlen src/ tests/ # files, directories, or both
|
|
27
|
+
dlen . --max-function 30 # your project, your thresholds
|
|
28
|
+
dlen . --output-format=json # for other tools
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
| Flag | Default | Meaning |
|
|
32
|
+
| --- | --- | --- |
|
|
33
|
+
| `--warn-function N` | 30 | report a function above N lines, without failing |
|
|
34
|
+
| `--max-function N` | 50 | fail on a function above N lines |
|
|
35
|
+
| `--max-class N` | 500 | fail on a class above N lines |
|
|
36
|
+
| `--output-format` | `full` | `full`, `github` or `json` |
|
|
37
|
+
|
|
38
|
+
A function's length includes its decorators — they are part of what you read before
|
|
39
|
+
you understand it.
|
|
40
|
+
|
|
41
|
+
### Where the defaults come from
|
|
42
|
+
|
|
43
|
+
They were measured, not chosen. Across 29,000 functions in the Python standard library,
|
|
44
|
+
numpy, Pillow, rich, pytest, httpx, mypy and coverage:
|
|
45
|
+
|
|
46
|
+
| | median | p75 | p90 | over 20 lines |
|
|
47
|
+
| --- | --- | --- | --- | --- |
|
|
48
|
+
| Python standard library | 7 | 14 | 30 | 16% |
|
|
49
|
+
| mypy | 7 | 17 | 37 | 21% |
|
|
50
|
+
| pytest | 9 | 18 | 34 | 22% |
|
|
51
|
+
| httpx | 9 | 19 | 34 | 23% |
|
|
52
|
+
| rich | 10 | 22 | 41 | 27% |
|
|
53
|
+
| Pillow | 10 | 23 | 51 | 27% |
|
|
54
|
+
| numpy | 14 | 46 | 82 | 43% |
|
|
55
|
+
|
|
56
|
+
The median function out there is 7 to 14 lines, which is reassuring and remarkably stable
|
|
57
|
+
across eight independent codebases. The tail is what a default has to answer to: a limit
|
|
58
|
+
of 20 flags a sixth of the standard library and a quarter of rich. That is not a warning,
|
|
59
|
+
it is a wall — and a tool that fires on a quarter of your code gets uninstalled on the
|
|
60
|
+
first day.
|
|
61
|
+
|
|
62
|
+
At 50 — the number `pylint` and `ruff` already use for statements — you flag around 5%.
|
|
63
|
+
That is a list you can work through on a Tuesday.
|
|
64
|
+
|
|
65
|
+
The class limit is a different story: 500 fires on about 1% of standard library classes,
|
|
66
|
+
and when it fires it is right. It stays where it was.
|
|
67
|
+
|
|
68
|
+
If you want the stricter Clean Code reading, it is one flag away:
|
|
69
|
+
|
|
70
|
+
```console
|
|
71
|
+
dlen src/ --warn-function 12 --max-function 20
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Versions before 0.2.0 shipped 12 and 20 as the defaults.
|
|
75
|
+
|
|
76
|
+
### Exit codes
|
|
77
|
+
|
|
78
|
+
| Code | When |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| `0` | nothing found, or only warnings |
|
|
81
|
+
| `1` | something was over a `max`, or a file could not be parsed |
|
|
82
|
+
| `2` | bad arguments |
|
|
83
|
+
|
|
84
|
+
Warnings never fail the build. That is the point of having two thresholds: one to
|
|
85
|
+
nudge, one to stop.
|
|
86
|
+
|
|
87
|
+
### Rules
|
|
88
|
+
|
|
89
|
+
| Code | Rule |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| `DL001` | function is too long |
|
|
92
|
+
| `DL002` | class is too long |
|
|
93
|
+
| `DL900` | file could not be read or parsed |
|
|
94
|
+
|
|
95
|
+
## In CI
|
|
96
|
+
|
|
97
|
+
`--output-format=github` puts each finding on the diff of the pull request instead
|
|
98
|
+
of burying it in a log:
|
|
99
|
+
|
|
100
|
+
```yaml
|
|
101
|
+
- run: uvx dlen src --output-format=github
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## As a library
|
|
105
|
+
|
|
106
|
+
The core returns data and prints nothing, so you can build your own reporting on it:
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from pathlib import Path
|
|
110
|
+
from dlen import Limits, check_paths
|
|
111
|
+
|
|
112
|
+
findings = check_paths([Path("src")], Limits(max_function=30))
|
|
113
|
+
for finding in findings:
|
|
114
|
+
print(finding.code, finding.path, finding.line, finding.message)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Every call returns a fresh list. Two runs never see each other's findings.
|
|
118
|
+
|
|
119
|
+
## How it measures
|
|
120
|
+
|
|
121
|
+
`dlen` parses your code with Python's own `ast` module, so it agrees with Python
|
|
122
|
+
about what a function is. It sees `async def`, decorators, nested functions and
|
|
123
|
+
methods, and it is not fooled by the word `def` inside a string or a comment.
|
|
124
|
+
|
|
125
|
+
> Versions before 0.1.0 matched text with regular expressions and had been broken on
|
|
126
|
+
> Python 3 since 2017. 0.1.0 is a rewrite: the counts are now correct rather than
|
|
127
|
+
> compatible, and the output carries file, line and column.
|
|
128
|
+
|
|
129
|
+
## Licence
|
|
130
|
+
|
|
131
|
+
MIT.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dlen"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Check how long your functions and classes are"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Endika Iglesias", email = "endika2@gmail.com" }]
|
|
14
|
+
keywords = ["linter", "refactoring", "code-quality", "static-analysis", "complexity"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Environment :: Console",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
]
|
|
28
|
+
dependencies = []
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/Endika/dlen"
|
|
32
|
+
Repository = "https://github.com/Endika/dlen"
|
|
33
|
+
Issues = "https://github.com/Endika/dlen/issues"
|
|
34
|
+
Changelog = "https://github.com/Endika/dlen/blob/main/CHANGELOG.md"
|
|
35
|
+
|
|
36
|
+
[project.scripts]
|
|
37
|
+
dlen = "dlen.cli:main"
|
|
38
|
+
|
|
39
|
+
[dependency-groups]
|
|
40
|
+
dev = [
|
|
41
|
+
"mypy>=1.11",
|
|
42
|
+
"pytest>=8",
|
|
43
|
+
"pytest-cov>=5",
|
|
44
|
+
"ruff>=0.6",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.hatch.build.targets.wheel]
|
|
48
|
+
packages = ["src/dlen"]
|
|
49
|
+
artifacts = ["src/dlen/py.typed"]
|
|
50
|
+
|
|
51
|
+
[tool.hatch.build.targets.sdist]
|
|
52
|
+
include = ["src/dlen", "tests", "README.md", "LICENSE"]
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
line-length = 100
|
|
56
|
+
target-version = "py310"
|
|
57
|
+
src = ["src", "tests"]
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint]
|
|
60
|
+
select = [
|
|
61
|
+
"E", "W", # pycodestyle
|
|
62
|
+
"F", # pyflakes
|
|
63
|
+
"I", # import sorting
|
|
64
|
+
"UP", # pyupgrade
|
|
65
|
+
"B", # bugbear
|
|
66
|
+
"SIM", # simplify
|
|
67
|
+
"C4", # comprehensions
|
|
68
|
+
"RET", # returns
|
|
69
|
+
"PTH", # use pathlib
|
|
70
|
+
"ARG", # unused arguments
|
|
71
|
+
"TID", # tidy imports
|
|
72
|
+
"RUF",
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
[tool.mypy]
|
|
76
|
+
# No python_version pin: it only ever makes mypy parse modern stubs with older rules.
|
|
77
|
+
# The 3.10 floor is proven by the CI test matrix instead.
|
|
78
|
+
strict = true
|
|
79
|
+
warn_unreachable = true
|
|
80
|
+
show_error_codes = true
|
|
81
|
+
files = ["src", "tests"]
|
|
82
|
+
|
|
83
|
+
[tool.pytest.ini_options]
|
|
84
|
+
testpaths = ["tests"]
|
|
85
|
+
addopts = "-q --strict-markers --strict-config"
|
|
86
|
+
|
|
87
|
+
[tool.coverage.run]
|
|
88
|
+
source = ["dlen"]
|
|
89
|
+
|
|
90
|
+
[tool.coverage.report]
|
|
91
|
+
exclude_also = ["if __name__ == .__main__.:"]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""dlen — check how long your functions and classes are."""
|
|
2
|
+
|
|
3
|
+
from .core import Code, Finding, Level, Limits, check_file, check_paths, check_source
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"Code",
|
|
7
|
+
"Finding",
|
|
8
|
+
"Level",
|
|
9
|
+
"Limits",
|
|
10
|
+
"check_file",
|
|
11
|
+
"check_paths",
|
|
12
|
+
"check_source",
|
|
13
|
+
]
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""The command line: parses arguments, renders findings, picks the exit code.
|
|
2
|
+
|
|
3
|
+
This is the only module that prints or reads argv. The core stays a pure function
|
|
4
|
+
of its inputs, which is what makes it usable from another program.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import json
|
|
11
|
+
import sys
|
|
12
|
+
from collections.abc import Sequence
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from .core import Finding, Level, Limits, check_paths
|
|
16
|
+
|
|
17
|
+
DEFAULTS = Limits()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def format_full(findings: Sequence[Finding]) -> str:
|
|
21
|
+
"""One line per finding, the shape editors and CI logs already know how to parse."""
|
|
22
|
+
return "\n".join(f"{f.path}:{f.line}:{f.column}: {f.code.value} {f.message}" for f in findings)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def format_github(findings: Sequence[Finding]) -> str:
|
|
26
|
+
"""Workflow commands, so the findings land on the PR diff instead of in a log."""
|
|
27
|
+
return "\n".join(
|
|
28
|
+
f"::{f.level.value} file={f.path},line={f.line},col={f.column},"
|
|
29
|
+
f"title={f.code.value}::{f.message}"
|
|
30
|
+
for f in findings
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _as_dict(finding: Finding) -> dict[str, str | int]:
|
|
35
|
+
return {
|
|
36
|
+
"path": str(finding.path),
|
|
37
|
+
"line": finding.line,
|
|
38
|
+
"column": finding.column,
|
|
39
|
+
"code": finding.code.value,
|
|
40
|
+
"level": finding.level.value,
|
|
41
|
+
"message": finding.message,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def format_json(findings: Sequence[Finding]) -> str:
|
|
46
|
+
return json.dumps([_as_dict(f) for f in findings], indent=2)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
FORMATTERS = {"full": format_full, "github": format_github, "json": format_json}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _add_limit(parser: argparse.ArgumentParser, flag: str, default: int, verb: str) -> None:
|
|
53
|
+
parser.add_argument(
|
|
54
|
+
flag,
|
|
55
|
+
type=int,
|
|
56
|
+
default=default,
|
|
57
|
+
metavar="N",
|
|
58
|
+
help=f"{verb} above N lines (default: {default})",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
63
|
+
parser = argparse.ArgumentParser(
|
|
64
|
+
prog="dlen", description="Check how long your functions and classes are."
|
|
65
|
+
)
|
|
66
|
+
parser.add_argument("paths", nargs="+", type=Path, metavar="PATH", help="files or directories")
|
|
67
|
+
_add_limit(parser, "--warn-function", DEFAULTS.warn_function, "warn")
|
|
68
|
+
_add_limit(parser, "--max-function", DEFAULTS.max_function, "fail")
|
|
69
|
+
_add_limit(parser, "--max-class", DEFAULTS.max_class, "fail")
|
|
70
|
+
parser.add_argument(
|
|
71
|
+
"--output-format", choices=sorted(FORMATTERS), default="full", metavar="FORMAT"
|
|
72
|
+
)
|
|
73
|
+
return parser
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
77
|
+
"""Returns the exit code: 1 when anything is over a max, 0 otherwise."""
|
|
78
|
+
args = build_parser().parse_args(argv)
|
|
79
|
+
limits = Limits(args.warn_function, args.max_function, args.max_class)
|
|
80
|
+
|
|
81
|
+
findings = check_paths(args.paths, limits)
|
|
82
|
+
if findings:
|
|
83
|
+
print(FORMATTERS[args.output_format](findings))
|
|
84
|
+
|
|
85
|
+
return 1 if any(f.level is Level.ERROR for f in findings) else 0
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if __name__ == "__main__": # pragma: no cover
|
|
89
|
+
sys.exit(main())
|