Skip to content

chore(ci): enabling A & B rules in Ruff #305

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

leandrodamascena
Copy link
Contributor

Issue Link (REQUIRED)

Fixes #304

Summary

Changes

This PR enables the following rule categories in our Ruff configuration:

  • A: Flake8-builtins - Checks for Python built-in names being used as variables/parameters
  • B: Flake8-bugbear - Detects common bugs and design problems

A: Flake8-builtins Rules

These rules prevent shadowing of built-in names like id, list, dict, input, etc. Shadowing built-ins can lead to:

  1. Confusing code that's difficult to understand and maintain
  2. Subtle bugs where the built-in function is unexpectedly unavailable
  3. Decreased code readability and maintainability

B: Flake8-bugbear Rules

Bugbear rules detect a wide range of common Python issues:

  1. Logical errors that may not cause syntax errors but lead to runtime bugs
  2. Anti-patterns that make code more error-prone or difficult to maintain
  3. Potentially dangerous code practices (like mutable default arguments)
  4. Loop control variable overwriting and other subtle logic issues

User experience

No changes in use experience

Checklist

If your change doesn't seem to apply, please leave them unchecked.

  • I have performed a self-review of this change
  • Changes have been tested
  • [] Changes are documented
  • I have linked this PR to an existing issue (required)
Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Copy link
Contributor Author

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Hey @brnaba-aws I just left some comments explaining what I'm changing.
Thanks

else:
if not isinstance(options.client, Anthropic):
raise ValueError("If streaming is disabled, the provided client must be an Anthropic client")
elif not isinstance(options.client, Anthropic):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplifying logic.

self.client = options.client
elif self.streaming:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplifying logic.

@@ -146,7 +144,7 @@ def _build_input(
system_prompt: str
) -> dict:
"""Build the conversation command with all necessary configurations."""
input = {
json_input = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Avoiding variables names that shadow built-in python functions.

@@ -169,7 +167,7 @@ def _get_max_recursions(self) -> int:

async def _handle_streaming(
self,
input: dict,
payload_input: dict,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Avoiding parameters names that shadow built-in python functions.

@@ -205,32 +203,30 @@ async def stream_generator():
async def _process_with_strategy(
self,
streaming: bool,
input: dict,
payload_input: dict,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Avoiding parameters names that shadow built-in python functions.

@@ -62,7 +62,7 @@ async def process_request(

except Exception as error:
Logger.error(f"Error processing request with agent {agent.name}:{str(error)}")
raise f"Error processing request with agent {agent.name}:{str(error)}"
raise f"Error processing request with agent {agent.name}:{str(error)}" from error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exceptions should propagate the original error for complete stack trace.

@@ -253,7 +253,7 @@ def _format_agents_memory(self, agents_history: list[ConversationMessage]) -> st
return ''.join(
f"{user_msg.role}:{user_msg.content[0].get('text','')}\n"
f"{asst_msg.role}:{asst_msg.content[0].get('text','')}\n"
for user_msg, asst_msg in zip(agents_history[::2], agents_history[1::2])
for user_msg, asst_msg in zip(agents_history[::2], agents_history[1::2], strict=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding strict parameter to make sure zip function will unpack expected values OR will raise ValueError if not.

@@ -137,10 +137,13 @@ async def agent_process_request(self,
user_id: str,
session_id: str,
classifier_result: ClassifierResult,
additional_params: dict[str, str] = {},
additional_params: dict[str, str] | None = None,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should not add mutable parameters in the method signature. Moving the check to inside the function.

session_id: str,
additional_params: dict[str, str] = {},
stream_response: bool | None = False) -> AgentResponse:
async def route_request(self, user_input: str, user_id: str, session_id: str, additional_params: dict[str, str] | None = None, stream_response: bool | None = False) -> AgentResponse:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should not add mutable parameters in the method signature. Moving the check to inside the function.

@@ -84,7 +84,7 @@ def _extract_properties(self, func: Callable) -> dict[str, dict[str, Any]]:
param_descriptions[param_name] = description

properties = {}
for param_name, param in sig.parameters.items():
for param_name, _param in sig.parameters.items():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Variables is not being used, so, add _ to make it clear.

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.

Enable rules A & B in Ruff
1 participant