Skip to content

Conversation

parth-deepsource
Copy link

This commit introduces a new Go-based checker for Python code that detects functions and methods exhibiting multiple distinct return types.

The checker, multiple-return-types, performs the following:

  • Traverses Python Abstract Syntax Trees (ASTs).
  • Identifies function and method definitions.
  • Analyzes explicit return statements within each function/method.
  • Infers a simplified type for each returned expression (e.g., "str", "int", "NoneType", "identifier", "call_result", "complex_type").
  • Considers functions that can implicitly return None (e.g., by falling off the end or having no return statements).
  • Reports an issue if a function/method is found to have more than one unique return type.

During integration, the following adjustments were made:

  • The initial strict type inference for identifiers (differentiating identifier:foo from identifier:bar) and call results (differentiating call_result:func1 from call_result:func2) was generalized to "identifier" and "call_result" respectively. This was done to prevent the new checker from flagging numerous valid cases in existing library tests where different variables or calls of the same broad type are returned.
  • The logic for detecting implicit None returns when mixed with explicit returns was simplified. The checker now primarily focuses on explicit return type variations and considers a function to return NoneType if it has no explicit returns or if all explicit returns are None.
  • An existing test file (checkers/python/testdata/path-traversal-open.test.py) was annotated with # <expect-error> as the new checker correctly identified a function with multiple return types.

A comprehensive test suite (checkers/python/multiple_return_types.test.py) has been added to validate the checker's functionality across various scenarios, including mixed types, implicit returns, nested structures, and class methods.

The checker is registered and built as part of the existing make generate-registry and make build process.

This commit introduces a new Go-based checker for Python code that detects functions and methods exhibiting multiple distinct return types.

The checker, `multiple-return-types`, performs the following:
- Traverses Python Abstract Syntax Trees (ASTs).
- Identifies function and method definitions.
- Analyzes explicit `return` statements within each function/method.
- Infers a simplified type for each returned expression (e.g., "str", "int", "NoneType", "identifier", "call_result", "complex_type").
- Considers functions that can implicitly return `None` (e.g., by falling off the end or having no return statements).
- Reports an issue if a function/method is found to have more than one unique return type.

During integration, the following adjustments were made:
- The initial strict type inference for identifiers (differentiating `identifier:foo` from `identifier:bar`) and call results (differentiating `call_result:func1` from `call_result:func2`) was generalized to "identifier" and "call_result" respectively. This was done to prevent the new checker from flagging numerous valid cases in existing library tests where different variables or calls of the same broad type are returned.
- The logic for detecting implicit `None` returns when mixed with explicit returns was simplified. The checker now primarily focuses on explicit return type variations and considers a function to return `NoneType` if it has no explicit returns or if all explicit returns are `None`.
- An existing test file (`checkers/python/testdata/path-traversal-open.test.py`) was annotated with `# <expect-error>` as the new checker correctly identified a function with multiple return types.

A comprehensive test suite (`checkers/python/multiple_return_types.test.py`) has been added to validate the checker's functionality across various scenarios, including mixed types, implicit returns, nested structures, and class methods.

The checker is registered and built as part of the existing `make generate-registry` and `make build` process.
Copy link

vercel bot commented May 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
globstar ⬜️ Ignored (Inspect) Visit Preview May 22, 2025 6:36am

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