Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: fix Ruff 0.8.0 UP031 errors #510

Merged
merged 3 commits into from
Nov 27, 2024
Merged

Conversation

njzjz-bot
Copy link
Contributor

@njzjz-bot njzjz-bot commented Nov 27, 2024

Generated by the task: njzjz-bot/njzjz-bot#12.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Enhanced error message formatting across various components, improving readability and clarity.
    • Updated error handling in job submission and status checking methods for better debugging.
  • Chores

    • Cleaned up code by removing unnecessary comments and using modern f-string formatting in multiple files.
  • Documentation

    • Updated copyright formatting in documentation configuration for improved readability.

Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request includes updates across various files, primarily focusing on modernizing string formatting from older methods to f-strings for improved readability. Changes are made to error message formatting in multiple classes and methods, including BaseContext, SSHSession, JH_UniScheduler, DistributedShell, LSF, PBS, Torque, SGE, Shell, Slurm, and SlurmJobArray. Additionally, some methods have undergone minor adjustments to improve clarity and maintainability, such as the removal of commented-out code and updates to resource specifications.

Changes

File Path Change Summary
doc/conf.py Updated copyright variable from old-style string formatting to f-string format.
dpdispatcher/base_context.py Modified error message formatting in block_checkcall method from % to str.format().
dpdispatcher/contexts/ssh_context.py Updated error message formatting in ensure_alive method from % to f-string; removed commented-out code.
dpdispatcher/machines/JH_UniScheduler.py Changed error message formatting in check_status method from % to f-string.
dpdispatcher/machines/distributed_shell.py Updated error message formatting in do_submit and check_status methods from % to f-string.
dpdispatcher/machines/lsf.py Enhanced error message formatting in check_status method from % to f-string; modified GPU resource handling in gen_script_header.
dpdispatcher/machines/pbs.py Updated error message formatting in check_status methods of PBS and Torque classes from % to f-string; adjusted resource specifications in gen_script_header for Torque and SGE.
dpdispatcher/machines/shell.py Updated error message formatting in do_submit and check_status methods from % to f-string.
dpdispatcher/machines/slurm.py Changed error message formatting in do_submit and check_status methods from % to f-string; modified gen_script_header in SlurmJobArray.
dpdispatcher/utils/utils.py Updated error message in retry decorator from old-style formatting to f-string.

Possibly related PRs

  • handle more job status on SGE system #491: The changes in the check_status method of the pbs.py file involve updating string formatting from the old-style % to f-strings, similar to the modernization of string formatting in the conf.py file of the main PR.

Suggested reviewers

  • njzjz

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between deeef8f and 5c980e5.

📒 Files selected for processing (1)
  • dpdispatcher/contexts/ssh_context.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • dpdispatcher/contexts/ssh_context.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

dpdispatcher/contexts/ssh_context.py Outdated Show resolved Hide resolved
Copy link
Contributor

@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: 0

🧹 Outside diff range and nitpick comments (14)
dpdispatcher/machines/shell.py (2)

Line range hint 99-116: Consider removing commented-out code

There are large blocks of commented-out code that appear to be old implementations. These should be removed as they can be retrieved from version control if needed.


45-48: Consider extracting common error handling logic

The error handling pattern is duplicated in both do_submit and check_status methods. Consider extracting this into a helper method to improve maintainability.

+ def _handle_command_error(self, cmd: str, ret: int, stderr) -> None:
+     err_str = stderr.read().decode("utf-8")
+     raise RuntimeError(
+         f"status command {cmd} fails to execute\nerror message:{err_str}\nreturn code {ret}\n"
+     )

  def do_submit(self, job):
      # ...
      if ret != 0:
-         err_str = stderr.read().decode("utf-8")
-         raise RuntimeError(
-             f"status command {cmd} fails to execute\nerror message:{err_str}\nreturn code {ret}\n"
-         )
+         self._handle_command_error(cmd, ret, stderr)
      # ...

  def check_status(self, job):
      # ...
      if ret != 0:
-         err_str = stderr.read().decode("utf-8")
-         raise RuntimeError(
-             f"status command {cmd} fails to execute\nerror message:{err_str}\nreturn code {ret}\n"
-         )
+         self._handle_command_error(cmd, ret, stderr)
      # ...

Also applies to: 81-84

dpdispatcher/base_context.py (1)

Line range hint 105-111: Convert to f-string for better readability

While the current change moves away from %-formatting, it doesn't fully address the Ruff UP031 rule which recommends using f-strings. Let's modernize this further.

-                "Get error code {} in calling {} with job: {} . message: {}".format(
-                    exit_status,
-                    cmd,
-                    self.submission.submission_hash,
-                    stderr.read().decode("utf-8"),
-                )
+                f"Get error code {exit_status} in calling {cmd} with job: {self.submission.submission_hash} . message: {stderr.read().decode('utf-8')}"
dpdispatcher/utils/utils.py (1)

194-194: LGTM! Consider adding retry count to the error message.

The conversion to f-string is correct and aligns with modern Python practices. To make debugging easier, consider including the max retry limit in the error message.

-                    f"Failed to run {func.__name__} for {current_retry} times"
+                    f"Failed to run {func.__name__} after {current_retry}/{max_retry} attempts"
dpdispatcher/machines/JH_UniScheduler.py (2)

Line range hint 40-44: Consider modernizing additional string formatting

There are more opportunities to modernize string formatting in this file. For example, the script header generation could be simplified using f-strings.

- script_header_dict = {
-     "JH_UniScheduler_nodes_line": f"#JSUB -n {resources.number_node * resources.cpu_per_node}",
-     "JH_UniScheduler_ptile_line": f"#JSUB -R 'span[ptile={resources.cpu_per_node}]'",
-     "JH_UniScheduler_partition_line": f"#JSUB -q {resources.queue_name}",
- }
+ JH_UniScheduler_script_header = f"""\
+ #!/bin/bash -l
+ #JSUB -e %J.err
+ #JSUB -o %J.out
+ #JSUB -n {resources.number_node * resources.cpu_per_node}
+ #JSUB -R 'span[ptile={resources.cpu_per_node}]'
+ #JSUB -q {resources.queue_name}
+ {custom_gpu_line if custom_gpu_line else f'#JSUB -gpgpu {resources.gpu_per_node}'}
+ """

Line range hint 86-90: Consider modernizing string concatenation

The command construction in do_submit could be simplified using f-strings.

- stdin, stdout, stderr = self.context.block_checkcall(
-     "cd {} && {} {}".format(
-         shlex.quote(self.context.remote_root),
-         "jsub < ",
-         shlex.quote(script_file_name),
-     )
- )
+ stdin, stdout, stderr = self.context.block_checkcall(
+     f"cd {shlex.quote(self.context.remote_root)} && jsub < {shlex.quote(script_file_name)}"
+ )
dpdispatcher/machines/distributed_shell.py (1)

Line range hint 184-202: Consider standardizing string formatting across the file

While the current changes correctly address the immediate UP031 violations, there are other instances in the file (e.g., submit_command construction) that still use the .format() method. Consider creating a follow-up PR to standardize all string formatting to f-strings for consistency.

dpdispatcher/machines/pbs.py (1)

140-142: LGTM: Improved string formatting with refactor opportunity

The conversion from %-formatting to f-string improves readability. However, there's an opportunity to reduce code duplication between PBS and Torque classes.

Consider extracting the common error handling logic to a shared method in the PBS base class, since both PBS and Torque classes use identical error messages and handling:

 class PBS(Machine):
+    def _handle_qstat_error(self, command: str, ret: int, err_str: str) -> None:
+        if "qstat: Unknown Job Id" in err_str or "Job has finished" in err_str:
+            if self.check_finish_tag(job=job):
+                return JobStatus.finished
+            else:
+                return JobStatus.terminated
+        else:
+            raise RuntimeError(
+                f"status command {command} fails to execute. erro info: {err_str} return code {ret}"
+            )

     def check_status(self, job):
         # ... existing code ...
         if ret != 0:
-            if "qstat: Unknown Job Id" in err_str or "Job has finished" in err_str:
-                if self.check_finish_tag(job=job):
-                    return JobStatus.finished
-                else:
-                    return JobStatus.terminated
-            else:
-                raise RuntimeError(
-                    f"status command {command} fails to execute. erro info: {err_str} return code {ret}"
-                )
+            return self._handle_qstat_error(command, ret, err_str)

This would eliminate the duplicated error handling code in the Torque class.

dpdispatcher/machines/slurm.py (2)

146-150: Consider improving multi-line f-string formatting

While the conversion to f-strings is correct, the multi-line error message could be more readable.

Consider this format:

-                f"status command {command} fails to execute."
-                f"job_id:{job_id} \n error message:{err_str}\n return code {ret}\n"
+                f"""status command {command} fails to execute.
+                   job_id: {job_id}
+                   error message: {err_str}
+                   return code: {ret}
+                """

254-256: Maintain consistent string formatting style

The code uses % formatting while the rest of the file is being modernized to use f-strings.

Consider using f-string for consistency:

-        return super().gen_script_header(job) + "\n#SBATCH --array=0-%s" % (
-            math.ceil(len(job.job_task_list) / slurm_job_size) - 1
-        )
+        return super().gen_script_header(job) + f"\n#SBATCH --array=0-{math.ceil(len(job.job_task_list) / slurm_job_size) - 1}"
dpdispatcher/contexts/ssh_context.py (4)

Line range hint 75-82: Remove commented-out code blocks

These commented code blocks should be removed as they add unnecessary noise to the codebase. The old implementations are preserved in version control if needed for reference.

Also applies to: 117-125, 402-411


Line range hint 271-279: Consider enforcing key-based authentication

While password authentication is marked as deprecated in the documentation, it's still fully implemented. Consider:

  1. Adding a deprecation warning when password authentication is used
  2. Setting a timeline for removing password authentication
  3. Enforcing key-based authentication as the only method

Line range hint 853-868: Enhance security of temporary file handling

The temporary file operations for tar commands could be more secure:

  1. Use mkstemp for creating temporary files with proper permissions
  2. Ensure cleanup in case of errors using try-finally blocks
-            file_list_file = pathlib.PurePath(
-                os.path.join(self.remote_root, f".tmp_tar_{uuid.uuid4()}")
-            ).as_posix()
-            self.write_file(file_list_file, "\n".join(files))
+            import tempfile
+            temp_fd, file_list_file = tempfile.mkstemp(prefix='.tmp_tar_', dir=self.remote_root)
+            try:
+                with os.fdopen(temp_fd, 'w') as temp_file:
+                    temp_file.write("\n".join(files))
+                # ... rest of the code ...
+            finally:
+                if self.check_file_exists(file_list_file):
+                    self.sftp.remove(file_list_file)

Line range hint 873-879: Improve error handling specificity

The error handling for file not found could be more specific and informative:

-            if "No such file or directory" in str(e):
-                raise FileNotFoundError(
-                    "Backward files do not exist in the remote directory."
-                ) from e
+            if "No such file or directory" in str(e):
+                missing_files = [f for f in files if not self.check_file_exists(f)]
+                raise FileNotFoundError(
+                    f"The following files do not exist in the remote directory: {', '.join(missing_files)}"
+                ) from e
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 177ff2e and deeef8f.

📒 Files selected for processing (10)
  • doc/conf.py (1 hunks)
  • dpdispatcher/base_context.py (1 hunks)
  • dpdispatcher/contexts/ssh_context.py (2 hunks)
  • dpdispatcher/machines/JH_UniScheduler.py (1 hunks)
  • dpdispatcher/machines/distributed_shell.py (2 hunks)
  • dpdispatcher/machines/lsf.py (1 hunks)
  • dpdispatcher/machines/pbs.py (2 hunks)
  • dpdispatcher/machines/shell.py (2 hunks)
  • dpdispatcher/machines/slurm.py (5 hunks)
  • dpdispatcher/utils/utils.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • doc/conf.py
🔇 Additional comments (11)
dpdispatcher/machines/shell.py (2)

46-46: LGTM! String formatting modernized correctly

The change from %-formatting to f-string improves readability while maintaining the same error information.


82-82: LGTM! String formatting modernized correctly

The change from %-formatting to f-string improves readability while maintaining the same error information.

dpdispatcher/machines/JH_UniScheduler.py (1)

108-108: LGTM! String formatting modernized correctly.

The error message has been properly converted to use f-string formatting, which is more readable and efficient than older string formatting methods.

dpdispatcher/machines/distributed_shell.py (2)

184-184: LGTM: Appropriate f-string conversion

The error message formatting has been correctly modernized to use an f-string, which aligns with the Ruff UP031 rule requirements.


202-202: LGTM: Appropriate f-string conversion

The error message formatting has been correctly modernized to use an f-string, which aligns with the Ruff UP031 rule requirements.

dpdispatcher/machines/lsf.py (2)

132-132: LGTM! Clean conversion to f-string format.

The conversion from %-formatting to f-string improves readability while maintaining the same error information. The error message properly includes all necessary debugging details (return code, job hash, and error message).


Line range hint 1-232: Verify remaining string formatting patterns.

Let's check if there are any remaining instances of old-style string formatting that should be updated for consistency.

✅ Verification successful

No string formatting inconsistencies found

The search results show that:

  • The %J occurrences are part of LSF directives in the script header template and are required by the LSF system
  • The only .format() calls use modern formatting with named parameters (**script_header_dict) or positional parameters ({})
  • No old-style % string formatting is used in the actual code
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for remaining %-formatting patterns in the file
# Excluding the lsf_script_header_template which uses % for LSF directives

rg '%(?!\{)[^%]*%[^{]' dpdispatcher/machines/lsf.py

Length of output: 275


Script:

#!/bin/bash
# Search for string formatting patterns in the file
# Using simpler patterns to catch different formatting styles

rg -U '%.+[^{]' dpdispatcher/machines/lsf.py

# Also search for format() method calls for completeness
rg '\.format\(' dpdispatcher/machines/lsf.py

Length of output: 254

dpdispatcher/machines/pbs.py (1)

90-92: LGTM: Improved string formatting

The conversion from %-formatting to f-string improves readability while maintaining the same error information.

dpdispatcher/machines/slurm.py (2)

Line range hint 100-111: LGTM: Improved error message formatting

The conversion to f-strings enhances readability while maintaining the same error information.


332-336: Similar multi-line f-string formatting can be improved

This section has the same formatting opportunity as discussed in the base class's check_status method.

dpdispatcher/contexts/ssh_context.py (1)

93-94: LGTM: Improved string formatting

The conversion to f-string improves readability and follows modern Python best practices.

Copy link

codecov bot commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 47.78%. Comparing base (177ff2e) to head (5c980e5).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
dpdispatcher/machines/slurm.py 0.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (177ff2e) and HEAD (5c980e5). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (177ff2e) HEAD (5c980e5)
15 14
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #510       +/-   ##
===========================================
- Coverage   60.19%   47.78%   -12.42%     
===========================================
  Files          39       39               
  Lines        3859     3859               
===========================================
- Hits         2323     1844      -479     
- Misses       1536     2015      +479     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@njzjz njzjz enabled auto-merge (squash) November 27, 2024 22:17
@njzjz njzjz merged commit 361d1fd into deepmodeling:master Nov 27, 2024
26 checks passed
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