Skip to content

Commit bacf9ac

Browse files
authored
Merge pull request #12 from albertas/feature/pre-commit-hook
Feature/pre commit hook
2 parents 49f5286 + 8352fe4 commit bacf9ac

9 files changed

+56
-42
lines changed

Diff for: .github/workflows/check-deadcode-on-python38.yml renamed to .github/workflows/check-deadcode-on-python310.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will install Python dependencies, run tests and lint with a single version of Python
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
33

4-
name: Checks deadcode package with Python3.8
4+
name: Checks deadcode package with Python3.10
55

66
on:
77
push:
@@ -19,10 +19,10 @@ jobs:
1919

2020
steps:
2121
- uses: actions/checkout@v3
22-
- name: Set up Python 3.8
22+
- name: Set up Python 3.10
2323
uses: actions/setup-python@v3
2424
with:
25-
python-version: "3.8"
25+
python-version: "3.10"
2626
- name: Install dependencies
2727
run: |
2828
make .venv

Diff for: .pre-commit-hooks.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Config options described at: https://pre-commit.com/#creating-new-hooks
2+
- id: deadcode
3+
name: deadcode
4+
description: "Deadcode: find and fix unused Python code"
5+
entry: deadcode
6+
language: python
7+
minimum_pre_commit_version: 2.9.2
8+
require_serial: true
9+
types: [python]

Diff for: Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fix: format fixlint
55

66
.venv:
77
pip install uv
8-
uv venv -p 3.8 .venv
8+
uv venv -p 3.10 .venv
99
uv pip sync requirements-dev.txt
1010
uv pip install -e .[test]
1111

@@ -22,7 +22,7 @@ lint: .venv
2222
.venv/bin/ruff check deadcode tests
2323

2424
fix: .venv
25-
.venv/bin/ruff deadcode tests --fix
25+
.venv/bin/ruff check deadcode tests --fix
2626

2727
mypy: .venv
2828
.venv/bin/mypy deadcode
@@ -38,7 +38,7 @@ format: .venv
3838
.venv/bin/ruff format deadcode tests
3939

4040
audit: .venv
41-
.venv/bin/pip-audit
41+
.venv/bin/pip-audit --skip-editable
4242

4343
sync: .venv
4444
uv pip sync requirements-dev.txt

Diff for: README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
<h2 align="center">Find and Fix Unused Python Code</h2>
44

55
<p align="center">
6-
<a href="https://github.com/albertas/deadcode/blob/main/LICENSE"><img alt="License: AGPLv3" src="https://raw.githubusercontent.com/albertas/deadcode/main/docs/_static/AGPLv3-license.svg"></a>
6+
<a href="https://github.com/pre-commit/pre-commit"><img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit" alt="pre-commit" style="max-width:100%;"></a>
77
<a href="https://pypi.org/project/deadcode/"><img alt="PyPI" src="https://img.shields.io/pypi/v/deadcode"></a>
88
<a href="https://pepy.tech/project/deadcode"><img alt="Downloads" src="https://static.pepy.tech/badge/deadcode"></a>
9+
<a href="https://github.com/albertas/deadcode/blob/main/LICENSE"><img alt="License: AGPLv3" src="https://raw.githubusercontent.com/albertas/deadcode/main/docs/_static/AGPLv3-license.svg"></a>
910
</p>
1011

1112

@@ -71,9 +72,9 @@ ignore-names-in-files = ["migrations"]
7172

7273

7374
##### Glossory
74-
name - variable, function or class name.
75-
body - code block which follows after `:` in function or class definition.
76-
definition - whole class or function definition expression including its name and body.
75+
- `name` - variable, function or class name.
76+
- `body` - code block which follows after `:` in function or class definition.
77+
- `definition` - whole class or function definition expression including its name and body.
7778

7879

7980
## Rules
@@ -173,6 +174,9 @@ code base is implemented in.
173174
- [ ] Check if file is still valid/parsable after automatic fixing, if not: halt the change and report error.
174175

175176
## Release notes
177+
- v2.3.2:
178+
- Add `pre-commit` hook support.
179+
- Drop support for Python 3.8 and 3.9 versions, since their ast implementation is lacking features.
176180
- v2.3.1:
177181
- Started analysing files in bytes instead of trying to convert them into UTF-8 encoded strings.
178182
- Improved automatic removal of unused imports.

Diff for: deadcode/actions/fix_or_show_unused_code.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ def fix_or_show_unused_code(unused_items: Iterable[CodeItem], args: Args) -> str
4141
if args.dry and ('__all_files__' in args.dry or _match(filename, args.dry)):
4242
with open(filename, 'rb') as f:
4343
filename_bytes = filename.encode()
44-
diff = diff_bytes(unified_diff, f.readlines(), updated_file_content_lines,
45-
fromfile=filename_bytes, tofile=filename_bytes)
44+
diff = diff_bytes(
45+
unified_diff,
46+
f.readlines(),
47+
updated_file_content_lines,
48+
fromfile=filename_bytes,
49+
tofile=filename_bytes,
50+
)
4651
# TODO: consider printing result instantly to save memory
4752
result_chunk = b''.join(diff)
4853
if args.no_color:

Diff for: deadcode/utils/fix_indent.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
T = TypeVar('T')
55

6+
67
def fix_indent(doc: T) -> Optional[T]:
78
"""Finds indentation of a first line and removes it from all following lines.
89

Diff for: pyproject.toml

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
[project]
22
name = "deadcode"
3-
version = "2.3.1"
3+
version = "2.3.2"
44
authors = [
55
{name = "Albertas Gimbutas", email = "[email protected]"},
66
]
77
description = "Find and remove dead code."
88
readme = "README.md"
9-
requires-python = ">= 3.8"
9+
requires-python = ">= 3.10"
1010
classifiers = [
1111
"Development Status :: 3 - Alpha",
1212
"License :: OSI Approved :: GNU Affero General Public License v3",
1313
"Natural Language :: English",
1414
"Operating System :: OS Independent",
1515
"Programming Language :: Python",
16-
"Programming Language :: Python :: 3.8",
17-
"Programming Language :: Python :: 3.9",
1816
"Programming Language :: Python :: 3.10",
1917
"Programming Language :: Python :: 3.11",
2018
"Programming Language :: Python :: 3.12",
@@ -156,23 +154,16 @@ ignore = [
156154
]
157155
"tests/fix/test_unused_imports.py" = ["F401", "W291"]
158156

159-
160157
[tool.mypy]
161158
exclude = ["build", "dist", ".venv"]
162-
python_version = "3.8"
159+
python_version = "3.10"
163160
strict = true
164161
pretty = true
165162
color_output = true
166163
show_error_codes = true
167164
warn_return_any = true
168165
warn_unused_configs = true
169166

170-
171-
[tool.isort]
172-
profile = "black"
173-
multi_line_output = 3
174-
known_third_party = ["deadcode"]
175-
176167
[tool.deadcode]
177168
ignore_names = [
178169
"ERROR_CODES",
@@ -195,14 +186,7 @@ ignore_names = [
195186
ignore_names_in_files = [
196187
"deadcode/utils/base_test_case.py"
197188
]
198-
199189
exclude = ["tests"]
200190

201-
[tool.black]
202-
max_line_length = 120
203-
line_length = 120
204-
target_version = ["py38"]
205-
206-
207191
[tool.pytest.ini_options]
208192
addopts = "--cov=. --no-cov-on-fail --cov-fail-under=88.0"

Diff for: requirements-dev.txt

+21-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ boolean-py==4.0
88
# via license-expression
99
cachecontrol==0.14.0
1010
# via pip-audit
11-
certifi==2024.6.2
11+
certifi==2024.7.4
1212
# via
1313
# httpcore
1414
# httpx
@@ -25,12 +25,16 @@ coverage==7.5.4
2525
# via pytest-cov
2626
cryptography==42.0.8
2727
# via secretstorage
28-
cyclonedx-python-lib==7.4.1
28+
cyclonedx-python-lib==7.5.1
2929
# via pip-audit
3030
defusedxml==0.7.1
3131
# via py-serializable
3232
distlib==0.3.8
3333
# via virtualenv
34+
exceptiongroup==1.2.1
35+
# via
36+
# anyio
37+
# pytest
3438
filelock==3.15.4
3539
# via
3640
# cachecontrol
@@ -85,7 +89,7 @@ msgpack==1.0.8
8589
mypy==1.10.1
8690
mypy-extensions==1.0.0
8791
# via mypy
88-
packageurl-python==0.15.1
92+
packageurl-python==0.15.3
8993
# via cyclonedx-python-lib
9094
packaging==24.1
9195
# via
@@ -98,9 +102,9 @@ pathspec==0.12.1
98102
# via hatchling
99103
pexpect==4.9.0
100104
# via hatch
101-
pip==24.1.1
105+
pip==24.1.2
102106
# via pip-api
103-
pip-api==0.0.33
107+
pip-api==0.0.34
104108
# via pip-audit
105109
pip-audit==2.7.3
106110
pip-requirements-parser==32.0.1
@@ -115,7 +119,7 @@ pluggy==1.5.0
115119
# pytest
116120
ptyprocess==0.7.0
117121
# via pexpect
118-
py-serializable==1.0.3
122+
py-serializable==1.1.0
119123
# via cyclonedx-python-lib
120124
pycparser==2.22
121125
# via cffi
@@ -134,7 +138,7 @@ rich==13.7.1
134138
# via
135139
# hatch
136140
# pip-audit
137-
ruff==0.5.0
141+
ruff==0.5.1
138142
secretstorage==3.3.3
139143
# via keyring
140144
shellingham==1.5.4
@@ -150,19 +154,26 @@ sortedcontainers==2.4.0
150154
toml==0.10.2
151155
# via pip-audit
152156
tomli==2.0.1
157+
# via
158+
# coverage
159+
# hatchling
160+
# mypy
161+
# pytest
153162
tomli-w==1.0.0
154163
# via hatch
155-
tomlkit==0.12.5
164+
tomlkit==0.13.0
156165
# via hatch
157166
trove-classifiers==2024.7.2
158167
# via hatchling
159168
typing-extensions==4.12.2
160-
# via mypy
169+
# via
170+
# anyio
171+
# mypy
161172
urllib3==2.2.2
162173
# via requests
163174
userpath==1.9.2
164175
# via hatch
165-
uv==0.2.21
176+
uv==0.2.24
166177
# via hatch
167178
virtualenv==20.26.3
168179
# via hatch

Diff for: tests/fix/test_unused_imports.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def fn():
3838
file2.py:4:4: DC07 Import `xyz` is never used
3939
file2.py:7:18: DC07 Import `foo` is never used
4040
41-
Removed 4 unused code items!""")
41+
Removed 4 unused code items!"""),
4242
)
4343

4444
# TODO: empty imports statements should be removed as well.

0 commit comments

Comments
 (0)