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

ruff prep #5764

Closed
wants to merge 9 commits into from
Closed

ruff prep #5764

wants to merge 9 commits into from

Conversation

kflynn
Copy link
Member

@kflynn kflynn commented Aug 10, 2024

I've been messing with uv and ruff (thanks, @the-wondersmith!) and wow, they are fast enough to legit be game-changers. This PR cleans up the Python code in line with what ruff wants.

Reviewers: going commit by commit will probably be simpler but it's not that big a deal.

  • Clean up imports and init.py's
  • Clear up typing for some booleans
  • Only have one copy of is_ip_address
  • Shut up regex warning (even though irerrorresponse.py needs to go away)
  • Clean up some things ruff doesn't like
  • Ditch dead code
  • Clean up is, is not, not in
  • Drop unneeded f-strings
  • Clean up bare excepts

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. python Pull requests that update Python code t:debt Piece of accumulated technical debt labels Aug 10, 2024
@@ -241,7 +241,7 @@ def good_ambassador_id(self, resource: dict):
# Make sure it's a list. Yes, this is Draconian,
# but the jsonschema will allow only a string or a list,
# and guess what? Strings are Iterables.
if type(allowed_ids) != list:
if type(allowed_ids) is not list:

Choose a reason for hiding this comment

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

Suggested change
if type(allowed_ids) is not list:
if not isinstance(allowed_ids, list):

@@ -31,6 +31,10 @@ class _CompileResult(TypedDict):
xds: NotRequired[EnvoyConfig]


def null_file_checker(path: str) -> bool:

Choose a reason for hiding this comment

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

Suggested change
def null_file_checker(path: str) -> bool:
def null_file_checker(_: str) -> bool:

Tiny nit - unused variables shouldn't be bound to names

@@ -430,7 +430,7 @@ def process(self, resource: ACResource) -> RichStatus:

try:
handler(resource)
except Exception as e:
except Exception:

Choose a reason for hiding this comment

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

If nothing is being done here, why is the exception being caught? Usually, you'd expect something like:

try:
    handler(resource)
except Exception as error:
    raise SomeCustomError from error

So that the traceback and context are preserved.

Haven't had time to dive into the full context here so it's a genuine question, not a criticism or anything 😅

@@ -24,7 +24,7 @@ def __init__(self, config: "V3Config") -> None:

self.update(
{
"listeners": [l.as_dict() for l in config.listeners],
"listeners": [x.as_dict() for x in config.listeners],

Choose a reason for hiding this comment

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

Suggested change
"listeners": [x.as_dict() for x in config.listeners],
"listeners": [listener.as_dict() for listener in config.listeners],

Absolutely a tiny nit, please feel 100% free to ignore it, just a personal preference for short-but-descriptive variable names over generic or shortest-possible ones.

@@ -406,13 +406,13 @@ def __init__(

request_headers_to_remove = group.get("remove_request_headers", None)
if request_headers_to_remove:
if type(request_headers_to_remove) != list:
if type(request_headers_to_remove) is not list:

Choose a reason for hiding this comment

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

Suggested change
if type(request_headers_to_remove) is not list:
if not isinstance(request_headers_to_remove, list):

request_headers_to_remove = [request_headers_to_remove]
self["request_headers_to_remove"] = request_headers_to_remove

response_headers_to_remove = group.get("remove_response_headers", None)
if response_headers_to_remove:
if type(response_headers_to_remove) != list:
if type(response_headers_to_remove) is not list:

Choose a reason for hiding this comment

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

Suggested change
if type(response_headers_to_remove) is not list:
if not isinstance(response_headers_to_remove, list):

@@ -68,7 +68,7 @@ def _cors_normalize(value: Any) -> Any:
are returned unaltered.
"""

if type(value) == list:
if type(value) is list:

Choose a reason for hiding this comment

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

Suggested change
if type(value) is list:
if isinstance(value, list):

@kflynn kflynn closed this Aug 20, 2024
@kflynn kflynn deleted the flynn/dev/ruff-prep branch August 20, 2024 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Pull requests that update Python code size:L This PR changes 100-499 lines, ignoring generated files. t:debt Piece of accumulated technical debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants