Skip to content

Issue 883: Use Literal type for strength parameter in java_class_decomposition#884

Open
MMenshikh wants to merge 3 commits intocqfn:masterfrom
MMenshikh:issue-883
Open

Issue 883: Use Literal type for strength parameter in java_class_decomposition#884
MMenshikh wants to merge 3 commits intocqfn:masterfrom
MMenshikh:issue-883

Conversation

@MMenshikh
Copy link

@MMenshikh MMenshikh commented Jul 20, 2025

Replaced str with Literal['strong', 'weak'] for the strength parameter in decompose_java_class (file: java_class_decomposition.py).

This improves type safety and makes the API more self-documenting.
Note: It does not prevent invalid values at runtime, but helps catch issues earlier with static analysis tools and IDEs.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling during concurrent file analysis to ensure that exceptions in individual files do not interrupt the overall process.
  • Refactor

    • Updated the concurrency result handling to return a complete list of results instead of yielding them one by one.
    • Enhanced type safety for Java class decomposition by restricting the accepted values for the strength parameter.

@coderabbitai
Copy link

coderabbitai bot commented Jul 20, 2025

Walkthrough

The updates modify concurrency result handling in the run_thread function, changing it from a generator yielding results to a function returning a list, and enhancing exception handling. Additionally, the decompose_java_class function now uses a more restrictive type annotation for its strength parameter, accepting only 'strong' or 'weak'.

Changes

File(s) Change Summary
aibolit/main.py Refactored run_thread to return a list of results, process futures as they complete, and add per-future exception handling.
aibolit/ast_framework/java_class_decomposition.py Updated decompose_java_class to use Literal['strong', 'weak'] for the strength parameter type annotation.

Poem

In threads we hop, results in a heap,
Catching exceptions so none make us weep.
With strength now defined, not just any old string—
Only 'strong' or 'weak' can the rabbit now bring.
Code grows precise as we bound and we leap!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0bdf156 and 34abf12.

📒 Files selected for processing (2)
  • aibolit/__main__.py (1 hunks)
  • aibolit/ast_framework/java_class_decomposition.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: latexmk
  • GitHub Check: make
🔇 Additional comments (2)
aibolit/ast_framework/java_class_decomposition.py (2)

4-4: LGTM: Clean import addition for enhanced typing.

The addition of Literal import enables more precise type annotations and follows Python typing best practices.


46-46: Excellent type safety improvement!

Changing the strength parameter from generic str to Literal['strong', 'weak'] provides several benefits:

  • Enhanced compile-time type checking
  • Better IDE autocompletion and validation
  • Clearer API contract for callers
  • Prevents invalid parameter values at development time

The existing runtime validation (lines 62-69) ensures backward compatibility while this change adds proactive type safety.

Comment on lines +881 to +889
results = []
with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
future_results = [executor.submit(run_recommend_for_file, file, args) for file in files]
concurrent.futures.wait(future_results)
for future in future_results:
yield future.result()
for future in concurrent.futures.as_completed(future_results):
try:
results.append(future.result())
except Exception as e:
print(f'Error analyzing file: {e}')
return results
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Improved concurrency with better error handling.

The changes enhance the robustness of concurrent file processing:

  • Better error isolation: Individual file analysis failures no longer stop the entire process
  • Explicit result collection: Clear accumulation of results using as_completed()
  • Enhanced debugging: Error messages provide context about which file failed

Note that this changes the return type from generator to list. Verify that all callers (like line 777) handle this correctly.

For large datasets, consider the memory implications of collecting all results vs the previous streaming approach. Run this script to verify the function usage pattern:


🏁 Script executed:

#!/bin/bash
# Description: Verify how run_thread function results are consumed
# Expected: Should find list() conversion or similar at call sites

rg -A 3 -B 3 "run_thread\(" --type py

Length of output: 729


Update call site for the new run_thread return type

Since run_thread now returns a List instead of a generator, remove the redundant list() wrapper at its call site:

  • In aibolit/main.py (around line 884):
    -    results = list(run_thread(files, args))
    +    results = run_thread(files, args)

This avoids an unnecessary copy. You may still review memory usage when processing very large file sets, but the change keeps the caller in sync with the new return type.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In aibolit/__main__.py around lines 881 to 889, the call site for run_thread
currently wraps its result in list(), which is redundant since run_thread now
returns a List instead of a generator. Remove the list() wrapper around the call
to run_thread to avoid unnecessary copying and keep the caller consistent with
the updated return type.

results.append(future.result())
except Exception as e:
print(f'Error analyzing file: {e}')
return results
Copy link
Contributor

@ivanovmg ivanovmg Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MMenshikh I guess you based this PR on top of the branch for PR #881
Could you branch this out of the master?

def decompose_java_class(
class_ast: AST,
strength: str,
strength: Literal['strong', 'weak'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MMenshikh while this improves the code from the typing standpoint, it does not help prevent invalid values at runtime. You mentioned that it does help, though, in the PR description. Can you fix the PR description?

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.

2 participants