Skip to content

Conversation

@DaisukeYoda
Copy link
Member

@DaisukeYoda DaisukeYoda commented Feb 6, 2026

Summary

  • Add support for Python 3.10+ Union types (X | Y) in CBO analysis
  • Parse type annotations as AST nodes instead of strings for accurate analysis
  • Handle Union types in both function parameters and return types
  • Adjust CBO score threshold from 12% to 25% for more realistic scoring

Problem

Before this fix, Union type annotations like ctx: Context | None were not detected, resulting in artificially low CBO values. For example, a class with CBO=3 should actually have been CBO=7.

CBO scores were also too harsh (0 or 100 with nothing in between).

Changes

Parser (internal/parser/ast_builder.go)

  • Build type annotations as AST nodes (not just strings) for typed parameters and return types
  • Enables proper analysis of complex types like Union

CBO Analyzer (internal/analyzer/cbo.go)

  • Add NodeBinOp handling for Union types (X | Y)
  • Recursively extract dependencies from both sides of Union

Score Calibration (domain/analyze.go)

  • Change CBO penalty threshold from 12% to 25%
  • Results in more realistic score distribution

Test plan

  • All existing tests pass
  • New tests added for Union type detection
  • Verified with real-world Python libraries

- Add support for Python 3.10+ Union types (X | Y) in CBO analysis
- Parse type annotations as AST nodes instead of strings for accurate analysis
- Handle Union types in both function parameters and return types
- Adjust CBO score threshold from 12% to 25% for more realistic scoring

Before this fix, Union type annotations like `ctx: Context | None` were not
detected, resulting in artificially low CBO values. For example, click's
Command class had CBO=3 when it should have been CBO=7.
@DaisukeYoda DaisukeYoda self-assigned this Feb 6, 2026
…rence

Move return type annotation from Children to Right field in FunctionDef
nodes. This prevents DFA analysis from treating type names as variable
uses, which would distort data-flow metrics.

- FunctionDef.Right now holds the return type AST node
- CBO analyzer updated to read from Right instead of Children
- Arg.Children still holds parameter type annotations (not affected by DFA)
@DaisukeYoda DaisukeYoda merged commit 333c9ac into main Feb 6, 2026
3 of 4 checks passed
@DaisukeYoda DaisukeYoda deleted the fix/cbo-union-type-support branch February 6, 2026 13:56
@DaisukeYoda DaisukeYoda mentioned this pull request Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant