Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions codeflash/cli_cmds/init_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,11 @@ def get_java_formatter_cmd(formatter: str, build_tool: JavaBuildTool) -> list[st
if formatter == "google-java-format":
return ["google-java-format --replace $file"]
if formatter == "spotless":
if build_tool == JavaBuildTool.MAVEN:
return ["mvn spotless:apply -DspotlessFiles=$file"]
if build_tool == JavaBuildTool.GRADLE:
return ["./gradlew spotlessApply"]
return ["spotless $file"]
return _SPOTLESS_COMMANDS.get(build_tool, ["spotless $file"])
if formatter == "other":
click.echo("In codeflash.toml, please replace 'your-formatter' with your formatter command.")
if not hasattr(get_java_formatter_cmd, "_warning_shown"):
click.echo("In codeflash.toml, please replace 'your-formatter' with your formatter command.")
get_java_formatter_cmd._warning_shown = True
Comment on lines +437 to +439
Copy link
Contributor

Choose a reason for hiding this comment

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

Lint failure (SLF001) & mypy error (attr-defined): The function-attribute pattern get_java_formatter_cmd._warning_shown triggers ruff SLF001 (private member access) and mypy attr-defined since functions don't declare this attribute. This will fail CI pre-commit checks.

Additionally, this changes user-visible behavior: the original code displayed the warning on every call, while this version only shows it once per process. In the current usage (called once in the CLI setup flow), this is harmless, but it's a semantic change that should be intentional.

Consider using a module-level flag instead:

Suggested change
if not hasattr(get_java_formatter_cmd, "_warning_shown"):
click.echo("In codeflash.toml, please replace 'your-formatter' with your formatter command.")
get_java_formatter_cmd._warning_shown = True
if not _OTHER_WARNING_SHOWN:
click.echo("In codeflash.toml, please replace 'your-formatter' with your formatter command.")
_mark_other_warning_shown()

Where _OTHER_WARNING_SHOWN and _mark_other_warning_shown() are defined at module scope (using global in the setter). This avoids both the SLF001 and mypy issues.

return ["your-formatter $file"]
return ["disabled"]

Expand Down Expand Up @@ -544,3 +542,9 @@ def get_java_test_command(build_tool: JavaBuildTool) -> str:
if build_tool == JavaBuildTool.GRADLE:
return "./gradlew test"
return "mvn test"


_SPOTLESS_COMMANDS = {
JavaBuildTool.MAVEN: ["mvn spotless:apply -DspotlessFiles=$file"],
JavaBuildTool.GRADLE: ["./gradlew spotlessApply"],
}
Comment on lines +547 to +550
Copy link
Contributor

Choose a reason for hiding this comment

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

Mutable shared state: The dictionary values are mutable lists shared across all callers. If any caller mutates the returned list (e.g., result.append(...)), the dictionary itself is permanently modified. The original code returned fresh list literals on each call.

Not a bug with current callers, but a latent risk. Consider using tuples as values and converting to list in the return:

_SPOTLESS_COMMANDS = {
    JavaBuildTool.MAVEN: ["mvn spotless:apply -DspotlessFiles=$file"],
    JavaBuildTool.GRADLE: ["./gradlew spotlessApply"],
}

Or return list(...) from the .get() call to ensure a fresh copy.

Loading