-
Notifications
You must be signed in to change notification settings - Fork 8
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
Fix type hints #340
Fix type hints #340
Conversation
for more information, see https://pre-commit.ci
WalkthroughThe changes involve modifications to function signatures and documentation across several files in the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant QueueSystem
User->>QueueSystem: Submit Job
QueueSystem->>User: Return Job ID or None
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (6)
pysqa/wrapper/gent.py (1)
Line range hint
41-50
: Update docstring to reflect new return type.The change in the return type hint from
pandas.DataFrame
toUnion[pandas.DataFrame, None]
is appropriate and aligns with the method's implementation. This change improves type accuracy and flexibility.However, the method's docstring should be updated to reflect this change in the return type.
Please update the docstring to include information about the possibility of returning
None
. Here's a suggested change:def convert_queue_status(queue_status_output: str) -> Union[pandas.DataFrame, None]: """ Converts the queue status output into a pandas DataFrame. Args: queue_status_output (str): The output of the queue status command. Returns: - pandas.DataFrame: The converted queue status. + Union[pandas.DataFrame, None]: The converted queue status, or None if there are no jobs in the queue. """pysqa/utils/validate.py (1)
80-82
: Consider renaming the functionThe function name
_memory_spec_string_to_value
now seems inconsistent with its new capability to handle non-string inputs.Consider renaming the function to better reflect its new capabilities. For example:
def _memory_spec_to_value( value: Union[str, int, float], default_magnitude: str = "m", target_magnitude: str = "b", ) -> Union[int, float]:This name more accurately describes the function's ability to handle various input types.
pysqa/ext/modular.py (1)
58-58
: Update the docstring to reflect the new return type.The change in the return type annotation to
Union[int, None]
is correct and more accurately represents the method's behavior. However, the docstring for this method needs to be updated to reflect this change.Please update the Returns section in the docstring to:
Returns: Union[int, None]: The cluster queue ID if successful, None otherwise.pysqa/utils/core.py (3)
75-82
: Enhance the docstring with type information and accuracy.The added docstring is a good start, but it can be improved for clarity and accuracy:
- Add type information to the parameters.
- Correct the description of
execute_command
to indicate it's a callable rather than a function.- Consider adding a return section to describe what the class returns or creates.
Here's a suggested improvement:
""" The goal of the QueueAdapter class is to make submitting to a queue system as easy as starting another sub process locally. Args: queue_type (str): Type of the queuing system in capital letters. execute_command (callable): Callable to execute commands. Returns: CoreQueueAdapter: An instance of the CoreQueueAdapter class. """
Line range hint
1-465
: Consider further type hint improvements throughout the file.While the PR objective is to "Fix type hints", there are opportunities to enhance the existing type hints:
Replace
Union[type, None]
withOptional[type]
for better readability. For example:def submit_job( self, queue: Optional[str] = None, job_name: Optional[str] = None, # ... other parameters ... ) -> Optional[int]:Use more specific callable types where applicable. For example, in the
__init__
method:from typing import Callable def __init__( self, queue_type: str, execute_command: Callable[[Union[str, List[str]], Optional[str], bool, bool, str], str] = execute_command, ):Consider using TypedDict for dictionary return types where appropriate, especially if the structure of the returned dictionary is known and consistent.
Import and use
TypeAlias
for complex types that are used multiple times:from typing import TypeAlias CommandList: TypeAlias = Union[str, List[str]]These improvements will enhance code readability and provide more precise type information.
Line range hint
1-465
: Summary of review and alignment with PR objectiveThe PR objective was to "Fix type hints", but the actual change was limited to adding a docstring to the
CoreQueueAdapter
class. While this addition is valuable, it doesn't directly address the stated objective.To better align with the PR's intent, consider:
- Implementing the suggested improvements to the added docstring, including type information for parameters.
- Applying the proposed type hint enhancements throughout the file, such as using
Optional
instead ofUnion[type, None]
and more specific callable types.These changes would more fully address the PR's objective of fixing and improving type hints in the code.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (5)
- pysqa/ext/modular.py (2 hunks)
- pysqa/utils/basic.py (0 hunks)
- pysqa/utils/core.py (1 hunks)
- pysqa/utils/validate.py (1 hunks)
- pysqa/wrapper/gent.py (2 hunks)
💤 Files not reviewed due to no reviewable changes (1)
- pysqa/utils/basic.py
🔇 Additional comments (5)
pysqa/wrapper/gent.py (2)
1-1
: LGTM: Import statement added for Union type.The addition of
from typing import Union
is appropriate and necessary for the updated return type hint in theconvert_queue_status
method. This change aligns with the PR objective of fixing type hints.
Line range hint
1-41
: Overall assessment: Changes improve type accuracy.The modifications in this file, including the addition of the
Union
import and the update to the return type hint ofconvert_queue_status
, successfully improve type accuracy and flexibility. These changes align well with the PR objective of fixing type hints.A minor suggestion was made to update the docstring of
convert_queue_status
to reflect the new return type. Once this is addressed, the changes in this file will be complete and satisfactory.pysqa/utils/validate.py (1)
80-82
: Verify impact onvalue_in_range
functionThe
value_in_range
function uses_memory_spec_string_to_value
. Please ensure that this change doesn't negatively impact its behavior.Let's verify the usage of
_memory_spec_string_to_value
in thevalue_in_range
function:This will help us understand if any adjustments are needed in
value_in_range
to accommodate the new input types of_memory_spec_string_to_value
.pysqa/ext/modular.py (2)
1-1
: LGTM: Import statement updated correctly.The addition of
Union
to the import statement is appropriate for the new return type annotation in thesubmit_job
method.
58-58
: Verify callers of submit_job method.The new return type
Union[int, None]
accurately reflects the method's behavior. To ensure this change doesn't introduce issues elsewhere in the codebase, please verify that all code calling this method is prepared to handle both int and None return values.✅ Verification successful
submit_job method return type verified.
All callers of
submit_job
have been reviewed and appropriately handle bothint
andNone
return values.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Find all calls to submit_job method in the codebase # Test: Search for calls to submit_job. Expect: List of all method calls with context. rg --type python -A 5 $'submit_job\('Length of output: 69
Script:
#!/bin/bash # Description: Find all calls to submit_job method in the codebase # Test: Search for calls to submit_job. Expect: List of all method calls with context. rg --glob '*.py' -A 5 'submit_job\('Length of output: 5104
value: Union[str, int, float], | ||
default_magnitude: str = "m", | ||
target_magnitude: str = "b", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approve type hint change, but function body needs updates
The change to accept Union[str, int, float]
as input is good for flexibility, but the function body needs to be updated to handle these new input types. Also, please update the docstring to reflect this change.
Please update the function body to handle int and float inputs. Here's a suggestion for the beginning of the function:
def _memory_spec_string_to_value(
value: Union[str, int, float],
default_magnitude: str = "m",
target_magnitude: str = "b",
) -> Union[int, float]:
"""
Converts a valid memory string, integer, or float into a value of desired magnitude.
Args:
value (Union[str, int, float]): The value to convert. If string, it should be a valid memory string.
default_magnitude (str): The magnitude for interpreting plain integer/float values [b, B, k, K, m, M, g, G, t, T]. Defaults to "m".
target_magnitude (str): The magnitude to which the output value should be converted [b, B, k, K, m, M, g, G, t, T]. Defaults to "b".
Returns:
Union[int, float]: The value in `target_magnitude` units.
"""
if isinstance(value, (int, float)):
# Treat int/float as if they were strings with default_magnitude
return (value * 1024 ** magnitude_mapping[default_magnitude.lower()]) / (
1024 ** magnitude_mapping[target_magnitude.lower()]
)
# Existing code for string handling...
This change ensures that int and float inputs are handled correctly, interpreting them with the default_magnitude.
Summary by CodeRabbit
New Features
submit_job
andconvert_queue_status
can now returnNone
).CoreQueueAdapter
class.Bug Fixes
Documentation
BasisQueueAdapter
class to streamline information.