albertas / deadcode Goto Github PK
View Code? Open in Web Editor NEWFind and fix unused Python code using command line.
License: GNU Affero General Public License v3.0
Find and fix unused Python code using command line.
License: GNU Affero General Public License v3.0
Hi, nice project, I really like it!
A few things that should be added:
.pre-commit-config.yaml
expects a rev
which is either
- repo: https://github.com/albertas/deadcode
rev: 2706a6c1a4f3fd2d0c40daf066c681858cacd181
hooks:
- id: deadcode
As mentioned above, the rev should ideally be the tag of the released version. At the time of opening this issue that would be 2.4.0
--version
or -V
argumentAfter installing its hard to figure out which version is installed.
Finally some questions ๐
I run deadcode.exe .
locally and get: Well done! โจ ๐ โจ
pip freeze | grep deadcode
deadcode==2.4.0
If I add deadcode to my pre-commit-config.yaml
like this
- repo: https://github.com/albertas/deadcode
rev: 2706a6c1a4f3fd2d0c40daf066c681858cacd181
hooks:
- id: deadcode
and run it manually with pre-commit
, I get this:
deadcode.................................................................Failed
- hook id: deadcode
- exit code: 1
datamodel.py:11:0: DC03 Class `PartListDataModel` is never used
datamodel.py:50:8: DC05 Attribute `logger` is never used
datamodel.py:108:4: DC04 Method `SetValue` is never used
datamodel.py:148:4: DC04 Method `AddEntry` is never used
datamodel.py:162:4: DC04 Method `RemoveAll` is never used
datamodel.py:167:4: DC04 Method `get_all` is never used
datamodel.py:171:4: DC04 Method `get_reference` is never used
datamodel.py:175:4: DC04 Method `get_value` is never used
datamodel.py:179:4: DC04 Method `get_lcsc` is never used
datamodel.py:183:4: DC04 Method `get_footprint` is never used
datamodel.py:187:4: DC04 Method `select_alike` is never used
datamodel.py:196:4: DC04 Method `set_lcsc` is never used
datamodel.py:206:4: DC04 Method `remove_lcsc_number` is never used
datamodel.py:232:4: DC04 Method `toggle_bom_pos` is never used
The sha sum 2706a6c1a4f3fd2d0c40daf066c681858cacd181
is the one with the commit message "Bump version to 2.4.0".
So I guess that should be the code released, right!?
Why do they behave differently?
There was an issue reported that UTF-8 encoded files are being corrupted in Windows.
Windows uses a different encoding format by default. It might be that deadcode reads a file and writes modified version using the same encoding on top, instead of preserving the original file encoding.
Hello,
I'm trying to use the --ignore-bodies-if-inherits-from
option to drop false positives for class attributes in my GraphQL schema, but it doesn't to work.
This is easily reproducible:
# mymodule.py
import graphene
class SomeQuery(graphene.ObjectType):
this_field_is_used = graphene.String()
$ deadcode ./mymodule.py --ignore-bodies-if-inherits-from graphene.ObjectType
mymodule.py:4:0: DC03 Class `SomeQuery` is never used
mymodule.py:6:4: DC01 Variable `this_field_is_used` is never used
$ deadcode ./mymodule.py --ignore-bodies-if-inherits-from ObjectType
mymodule.py:4:0: DC03 Class `SomeQuery` is never used
mymodule.py:6:4: DC01 Variable `this_field_is_used` is never used
I'm using deadcode==2.4.1
P.S. --ignore-definitions-if-inherits-from
doesn't work either.
deadcode version:
[[package]]
name = "deadcode"
version = "2.2.2"
description = "Find and remove dead code."
category = "dev"
optional = false
python-versions = ">=3.8"
files = [
{file = "deadcode-2.2.2-py3-none-any.whl", hash = "sha256:d8280cf44e224e47e061f9b9963b902c009dc1ba1e43a42623a43ab88e1afa36"},
{file = "deadcode-2.2.2.tar.gz", hash = "sha256:b63e207999667e731212e04f6d48888a2e3f7256e5ee808ba9082af20d99dca0"},
]
Python version:
Python 3.11.7
To reproduce, create the following two files in the same directory:
file1.py:
foo = 1
bar = 2
xyz = 3
file2.py:
from file1 import (
foo,
bar,
xyz
)
from file1 import foo
def fn():
pass
fn()
Run deadcode with --fix
๐ v3.11.7 (myapp-py3.11)
โ deadcode ./file2.py --fix
file2.py:1:0: DC007 Import `bar` is never used
file2.py:1:0: DC007 Import `foo` is never used
file2.py:1:0: DC007 Import `xyz` is never used
file2.py:7:0: DC007 Import `foo` is never used
Removed 4 unused code items!
I expect all of the imports to be removed, but this what file2.py
looks like afterwards instead:
rom file1 import (
foo,
bar,
xyz
)
def fn():
pass
fn()
It removes the f
in from
, and doesn't get rid of those imports.
I am getting this error when I try to run this command on a directory within Django with just a bunch of Python files with subdirectories.
>>> deadcode .
Traceback (most recent call last):
File "/Users/myuser/env3/bin/deadcode", line 8, in <module>
sys.exit(main())
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/cli.py", line 20, in main
unused_names = find_unused_names(filenames=filenames, args=args)
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/actions/find_unused_names.py", line 13, in find_unused_names
dead_code_visitor.visit_abstract_syntax_trees()
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/visitor/dead_code_visitor.py", line 106, in visit_abstract_syntax_trees
self.visit(node)
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/visitor/dead_code_visitor.py", line 503, in visit
self.visit(item)
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/visitor/dead_code_visitor.py", line 469, in visit
visitor(node)
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/visitor/dead_code_visitor.py", line 418, in visit_ImportFrom
self._add_aliases(node)
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/visitor/dead_code_visitor.py", line 187, in _add_aliases
self._define(
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/visitor/dead_code_visitor.py", line 259, in _define
first_lineno = lines.get_first_line_number(first_node)
File "/Users/myuser/env3/lib/python3.9/site-packages/deadcode/visitor/lines.py", line 82, in get_first_line_number
return node.lineno
AttributeError: 'alias' object has no attribute 'lineno'
After installing your library I needed to manually install "tomli" library
Traceback (most recent call last):
File "/Users/luis/.pyenv/versions/lambda_poc_ai/bin/deadcode", line 5, in <module>
from deadcode.cli import main
File "/Users/luis/.pyenv/versions/3.10.12/envs/lambda_poc_ai/lib/python3.10/site-packages/deadcode/cli.py", line 5, in <module>
from deadcode.actions.parse_arguments import parse_arguments
File "/Users/luis/.pyenv/versions/3.10.12/envs/lambda_poc_ai/lib/python3.10/site-packages/deadcode/actions/parse_arguments.py", line 9, in <module>
import tomli as tomllib
ModuleNotFoundError: No module named 'tomli'
Hi, the deadcode
tool crashes when it encounters non utf-8 file.
TL;DR:
(.venv) root@pwndbg:~/pwndbg# deadcode .
Traceback (most recent call last):
File "/root/pwndbg/.venv/bin/deadcode", line 8, in <module>
sys.exit(main())
^^^^^^
File "/root/pwndbg/.venv/lib/python3.11/site-packages/deadcode/cli.py", line 20, in main
unused_names = find_unused_names(filenames=filenames, args=args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/pwndbg/.venv/lib/python3.11/site-packages/deadcode/actions/find_unused_names.py", line 13, in find_unused_names
dead_code_visitor.visit_abstract_syntax_trees()
File "/root/pwndbg/.venv/lib/python3.11/site-packages/deadcode/visitor/dead_code_visitor.py", line 101, in visit_abstract_syntax_trees
file_content = f.read()
^^^^^^^^
File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 81: invalid start byte
This occurred when it tried to parse the following file: .venv/lib64/python3.11/site-packages/IPython/core/tests/nonascii.py
which can be found here: https://github.com/ipython/ipython/blob/main/IPython/core/tests/nonascii.py
Lovely project!
Alas, https://deadcode.readthedocs.io does not resolve to docs.
pre-commit
hook implementation should be added, since it is a major way of using linters.
Nice package. I did run into the following problem. When I try to run deadcode --fix on the following
a=0
if True:
a=1
print(a)
print(a)
then it changes it to
a=0
print(a)
The expected behaviour would be
a=0
a=1
print(a)
print(a)
or just leaving the if
intact.
By default wildcard import imports all the names, which do not start with underscore.
However, it is possible to limit names, which might be imported by defining __all__
variable in the module. Only items listed in __all__
variable can be imported from this module.
This should be reflected in the behavior of deadcode.
Example:
foo.py
__all__ = ['foo', 'bar']
foo = 1
bar = 2
spam = 3
bar.py
from foo import *
print(foo)
print(bar)
# Should raise an error, because `spam` was not imported:
print(spam)
If I pipe to a file the file is empty. What I tried:
deadcode . --exclude=tests,tasks,src/tools --ignore-names-in-files=migrations > deadcode.txt
Perhaps because of the way we specify routes in FastAPI, when I run deadcode on a FastAPI project it identify nearly everything as dead code.
Is there a way to run deadcode for FastAPI projects?
@albertas I would like to know if it is possible or if there is a way for me to automatically delete this indicated code?
With an unused function, this works:
def check(arg): # noqa: DC002
pass
but this doesn't:
@cache
def check(arg): # noqa: DC002
pass
This works again, but I don't think it should be valid:
@cache # noqa: DC002
def check(arg):
pass
Adding more decorators yields an error again.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.