Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
pip install deadcode
To see unused code findings:
deadcode .
To see suggested fixes for all files:
deadcode . --fix --dry
To see suggested fixes only for foo.py
file:
deadcode . --fix --dry --only foo.py
To fix:
deadcode . --fix
Tune out some of the false positives, e.g.:
deadcode . --exclude=venv,tests --ignore-names=BaseTestCase,*Mixin --ignore-names-in-files=migrations
The same options can be provided in pyproject.toml
settings file:
[tool.deadcode]
exclude = ["venv", "tests"]
ignore-names = ["BaseTestCase", "*Mixin"]
ignore-names-in-files = ["migrations"]
Option | Type | Meaning |
---|---|---|
--fix | - | Automatically remove detected unused code expressions from the code base. |
--dry | - | Show changes which would be made in files. |
--only | list | Filenames (or path expressions), that will be reflected in the output (and modified if needed). |
--exclude | list | Filenames (or path expressions), which will be completely skipped without being analysed. |
--ignore-names | list | Removes provided list of names from the output. Regexp expressions to match multiple names can also be provided, e.g. *Mixin will match all classes ending with Mixin . |
--ignore-names-in-files | list | Ignores unused names in files, which filenames match provided path expressions. |
--ignore-names-if-inherits-from | list | Ignores names of classes, which inherit from provided class names. |
--ignore-names-if-decorated-with | list | Ignores names of an expression, which is decorated with one of the provided decorator names. |
--ignore-bodies-of | list | Ignores body of an expression if its name matches any of the provided names. |
--ignore-bodies-if-decorated-with | list | Ignores body of an expression if its decorated with one of the provided decorator names. |
--ignore-bodies-if-inherits-from | list | Ignores body of a class if it inherits from any of the provided class names. |
--ignore-definitions | list | Ignores definition (including name and body) if a name of an expression matches any of the provided ones. |
--ignore-definitions-if-inherits-from | list | Ignores definition (including name and body) of a class if it inherits from any of the provided class names. |
--ignore-definitions-if-decorated-with | list | Ignores definition (including name and body) of an expression, which is decorated with any of the provided decorator names. |
--no-color | - | Removes colors from the output. |
--count | - | Provides the count of the detected unused names instead of printing them all out. |
--quiet | - | Does not output anything. Makefile still fails with exit code 1 if unused names are found. |
name
- variable, function or class name.body
- code block which follows after :
in function or class definition.definition
- whole class or function definition expression including its name and body.Code | Name | Message |
---|---|---|
DC01 | unused-variable | Variable {name} is never used |
DC02 | unused-function | Function {name} is never used |
DC03 | unused-class | Class {name} is never used |
DC04 | unused-method | Method {name} is never used |
DC05 | unused-attribute | Attribute {name} is never used |
DC06 | unused-name | Name {name} is never used |
DC07 | unused-import | Import {name} is never used |
DC08 | unused-property | Property {name} is never used |
DC09 | unreachable-if-block | Unreachable conditional statement block |
DC11 | empty-file | Empty Python file |
DC12 | commented-out-code | Commented out code |
DC13 | unreachable-code | Code after terminal statement, e.g. return , raise , continue , break |
DC | ignore-expression | Do not show any findings for an expression, which starts on current line (this code can only be used in # noqa: DC comments) |
Inline # noqa
comments can be used to ignore deadcode
checks.
E.g. unused Foo
class wont be detected/fixed because # noqa: DC03
comment is used:
class Foo: # noqa: DC03
pass
make check
- runs unit tests and other checks using virtual environment.ruff and
flake8 - don't have rules for unused global
code detection, only for local ones F823
, F841
, F842
. deadcode
package
tries to add new DCXXX
checks for detecting variables/functions/classes/files
which are not used in a whole code base.
deadcode
- is supposed to be used inline with other static code checkers like ruff
.
There is an alternative vulture package.
In case there are several definitions using the same name - they all wont be reported if at least one usage of that name is being detected.
Files with syntax errors will be ignored, because deadcode
uses ast
to
build abstract syntax tree for name usage detection.
It is assumed that deadcode
will be run using the same or higher Python version as the
code base is implemented in.
.*
with only *
in regexp matching.--fix
option to automatically remove detected dead code occourencies--depth
parameter to ignore nested code.. (To only check global scope use 0).deadcode
till the output stops changing.--fix
could accept a list of filenames as well (only those files would be changed, but the summary could would be full).
(This might be confusing, because filenames, which have to be considered are provided without any flag, --fix is expected to not accept arguments)raise
, return
, break
, continue
and comes in the same scope.ignore
and per-file-ignores
command line and pyproject.toml options, which allows to skip some rules.noqa
comment and all rules react to noqa: rule_id
comments.ast
implementation to lower Python versions.--version
option to show deadcode
version.deadcode
output.--only
option that accepts filenames only which will be reflected in the output and modified.
This option can be used with --fix
and --fix --dry
options as well as for simple unused code detection without fixing.pre-commit
hook support.ast
implementation is lacking features.--dry
option.FAQs
Find and remove dead code.
We found that deadcode demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.