Skip to content

Bug: Mutable default arguments in function signatures #573

Description

@gkorland

Description

Several functions use mutable default arguments, a classic Python bug where mutations to the default value are shared across all calls:

  1. api/project.py:96process_git_history:

    def process_git_history(self, ignore: Optional[List[str]] = []) -> GitGraph:
  2. api/analyzers/source_analyzer.py:184analyze_local_folder:

    def analyze_local_folder(self, path: str, g: Graph, ignore: Optional[list[str]] = []) -> None:
  3. api/graph.py:482connect_entities:

    def connect_entities(self, relation: str, src_id: int, dest_id: int, properties: dict = {}) -> None:

Impact

If any of these functions mutate their default argument (e.g., ignore.append(...) or properties['key'] = ...), the mutation persists across subsequent calls, causing hard-to-debug state leakage.

Suggested Fix

Use None as the default and create a new list/dict inside the function:

def process_git_history(self, ignore: Optional[List[str]] = None) -> GitGraph:
    if ignore is None:
        ignore = []

Note: analyze_sources in project.py already uses this pattern correctly.

Context

Found during code review of PR #522.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions