Package: deadcode
deadcodedeadcodeallows to detect and fix unused Python code. It implementsDCXXXstatic code linting rules for detecting unused code such as
variables, functions and classes.InstallationpipinstalldeadcodeUsagedeadcode.Or with command line options:deadcode . --exclude=venv,tests --ignore-names=BaseTestCase,*Mixin --ignore-names-in-files=migrationsThe same options can be provided inpyproject.tomlsettings file:[tool.deadcode]
exclude = ["venv", "tests"]
ignore-names = ["BaseTestCase", "*Mixin"]
ignore-names-in-files = ["migrations"]Command line optionsOptionTypeMeaning--fix-Automatically remove detected unused code expressions from the code base.--excludelistFilenames (or path expressions), which will be completely skipped without being analysed.--ignore-nameslistRemoves provided list of names from the output. Regexp expressions to match multiple names can also be provided, e.g.*Mixinwill match all classes ending withMixin.--ignore-names-in-fileslistIgnores unused names in files, which filenames match provided path expressions.--ignore-names-if-inherits-fromlistIgnores names of classes, which inherit from provided class names.--ignore-names-if-decorated-withlistIgnores names of an expression, which is decorated with one of the provided decorator names.--ignore-bodies-oflistIgnores body of an expression if its name matches any of the provided names.--ignore-bodies-if-decorated-withlistIgnores body of an expression if its decorated with one of the provided decorator names.--ignore-bodies-if-inherits-fromlistIgnores body of a class if it inherits from any of the provided class names.--ignore-definitionslistIgnores definition (including name and body) if a name of an expression matches any of the provided ones.--ignore-definitions-if-inherits-fromlistIgnores definition (including name and body) of a class if it inherits from any of the provided class names.--ignore-definitions-if-decorated-withlistIgnores 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.Glossoryname - 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.RulesCodeNameMessageDC001unused-variableVariable{name}is never usedDC002unused-functionFunction{name}is never usedDC003unused-classClass{name}is never usedDC004unused-methodMethod{name}is never usedDC005unused-attributeAttribute{name}is never usedDC006unused-nameName{name}is never usedDC007unused-importImport{name}is never usedDC008unused-propertyProperty{name}is never usedDC009*unreachable-codeUnreachableelseblockDC011empty-fileEmpty fileDC012*commented-out-codeCommented out codeDC013*ignore-expressionThis error code can ony be used in# noqa: DC013comments (no errors will be reported for expression which begins in current line)*- are not yet implemented rules.Ignoring checks with noqa commentsInline# noqacomments can be used to ignoredeadcodechecks.
E.g. unusedFooclass wont be detected/fixed because# noqa: DC003comment is used:classFoo:# noqa: DC003passContributingmake check- runs unit tests and other checks using virtual environment.Rationaleruffandflake8- don't have rules for unused global
code detection, only for local onesF823,F841,F842.deadcodepackage
tries to add newDCXXXchecks 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 likeruff.There is an alternativevulturepackage.Known limitationsIn 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, becausedeadcodeusesastto
build abstract syntax tree for name usage detection.It is assumed thatdeadcodewill be run using the same or higher Python version as the
code base is implemented in.Feature requestsReplace.*with only*in regexp matching.Add unused class method detection DC004 check.Add--fixoption to automatically remove detected dead code occourenciesAdd a check for empty python files.Split error codes into DC001, DC002, DC003 for variables, functions, class.Should have different codes for ignoring name and ignoring whole definition (reserved DCxx0 - ignore name, DCxx1 - ignore definition).Allow to disable each check separately using:inline comment.pyproject.toml fileAdd a check for code in comments.Add target python version option, if specified it will be used for code base check.Add a--depthparameter to ignore nested code.. (To only check global scope use 0).Add options:--ignore-definitions--ignore-definitions-if-inherits-from--ignore-definitions-if-decorated-with--ignore-names-if-inherits-from--ignore-names-if-decorated-withDistinguish between definitions with same name, but different files.Repeated application ofdeadcodetill the output stops changing.Unreachable code detection and fixing.--fix --dry [filenames]- only show whats about to change in the listed filenames.Benchmarking performance with larger projects (time, CPU and memory consumption) in order to optimize.