Giter Club home page Giter Club logo

uncalled's Introduction

uncalled

Find unused functions in Python projects.

This tool uses either regular expressions (the default) or AST traversal. The regular expressions are fast and has surprisingly few false-positives. To further reduce false positives, there is a combined mode both.

Usage

$ uncalled path/to/project

for more options, see uncalled --help

vulture is a similar package.

uncalled's People

Contributors

elazarg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

uncalled's Issues

Functions in docstrings are detected as unused

It is not that uncommon that people write example usage in docstrings, and this can include function definitions.

For example, running uncalled with this in the code:

def print_args(f):
    """Decorator that print the function arguments before executing it.

    Example:

    ```
    @print_args
    def example_add(x, y):
        return x + y

    if __name__ == "__main__":
        addition(x=1, y=2)
    ```

    => prints "() {'x': 1, 'y': 2}".
    """

    def decorated(*args, **kwargs):
        print(args, kwargs)
        return f(*args, **kwargs)

    return decorated

Will output Unused function example_add.


People can also use docstrings as 'block comments':

"""TODO: uncomment when feature X is merged.

def call_feature_X():
    pass
"""

In this case we probably don't want uncalled to output Unused function call_feature_X.

Pydantic Validators show up as unused functions.

It seems that all pydantic validators show up as false positives.

Given a module like this query.py

from pydantic import BaseModel, field_validator

class Query(BaseModel):

    limit: int | None = None  # number of rows to output

    @field_validator("limit", mode="before")
    @classmethod
    def limit_must_be_int_or_none(cls, item: int | None) -> int | None:
        """Validate limit."""
        match item:
            case None | int():
                return item
            case _:
                return None

Uncalled will flag the limit_must_be_int_or_none. I assume this is because my code doesn't call it. However, pydantic does call it whenever my Query gets instantiated. It is very much not dead code.

$ uncalled --how both query.py
query.py: Unused function limit_must_be_int_or_none

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.