Skip to content

Commit

Permalink
Update ruff linter rules according to documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Jul 5, 2024
1 parent c16f3f4 commit ec06bb0
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def try_load_file(self, path: str) -> bytes:
return response["Body"].read()
except botocore.exceptions.ClientError as e:
if e.response["Error"]["Code"] in {"NoSuchKey", "404"}:
raise FileNotFoundError
raise FileNotFoundError from e
else:
raise e

Expand Down
2 changes: 1 addition & 1 deletion griptape/drivers/file_manager/base_file_manager_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def save_file(self, path: str, value: bytes | str) -> InfoArtifact | ErrorArtifa
value = value.encode()
else:
value = value.encode(encoding=encoding)
elif isinstance(value, bytearray) or isinstance(value, memoryview):
elif isinstance(value, (bytearray, memoryview)):
raise ValueError(f"Unsupported type: {type(value)}")

self.try_save_file(path, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ def _make_request(self, request: dict) -> bytes:
try:
image_bytes = self.image_generation_model_driver.get_generated_image(response_body)
except Exception as e:
raise ValueError(f"Inpainting generation failed: {e}")
raise ValueError(f"Inpainting generation failed: {e}") from e

return image_bytes
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def try_query(self, query: str, images: list[ImageArtifact]) -> TextArtifact:
try:
return self.image_query_model_driver.process_output(response_body)
except Exception as e:
raise ValueError(f"Output is unable to be processed as returned {e}")
raise ValueError(f"Output is unable to be processed as returned {e}") from e
67 changes: 33 additions & 34 deletions griptape/drivers/web_scraper/markdownify_web_scraper_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,51 +48,50 @@ def convert_a(self, el, text, convert_as_inline):
return super().convert_a(el, text, convert_as_inline)
return text

with sync_playwright() as p:
with p.chromium.launch(headless=True) as browser:
page = browser.new_page()
with sync_playwright() as p, p.chromium.launch(headless=True) as browser:
page = browser.new_page()

def skip_loading_images(route):
if route.request.resource_type == "image":
return route.abort()
route.continue_()
def skip_loading_images(route):
if route.request.resource_type == "image":
return route.abort()
route.continue_()

page.route("**/*", skip_loading_images)
page.route("**/*", skip_loading_images)

page.goto(url)
page.goto(url)

# Some websites require a delay before the content is fully loaded
# even after the browser has emitted "load" event.
if self.timeout:
page.wait_for_timeout(self.timeout)
# Some websites require a delay before the content is fully loaded
# even after the browser has emitted "load" event.
if self.timeout:
page.wait_for_timeout(self.timeout)

content = page.content()
content = page.content()

if not content:
raise Exception("can't access URL")
if not content:
raise Exception("can't access URL")

soup = BeautifulSoup(content, "html.parser")
soup = BeautifulSoup(content, "html.parser")

# Remove unwanted elements
exclude_selector = ",".join(
self.exclude_tags + [f".{c}" for c in self.exclude_classes] + [f"#{i}" for i in self.exclude_ids]
)
if exclude_selector:
for s in soup.select(exclude_selector):
s.extract()
# Remove unwanted elements
exclude_selector = ",".join(
self.exclude_tags + [f".{c}" for c in self.exclude_classes] + [f"#{i}" for i in self.exclude_ids]
)
if exclude_selector:
for s in soup.select(exclude_selector):
s.extract()

text = OptionalLinksMarkdownConverter().convert_soup(soup)
text = OptionalLinksMarkdownConverter().convert_soup(soup)

# Remove leading and trailing whitespace from the entire text
text = text.strip()
# Remove leading and trailing whitespace from the entire text
text = text.strip()

# Remove trailing whitespace from each line
text = re.sub(r"[ \t]+$", "", text, flags=re.MULTILINE)
# Remove trailing whitespace from each line
text = re.sub(r"[ \t]+$", "", text, flags=re.MULTILINE)

# Indent using 2 spaces instead of tabs
text = re.sub(r"(\n?\s*?)\t", r"\1 ", text)
# Indent using 2 spaces instead of tabs
text = re.sub(r"(\n?\s*?)\t", r"\1 ", text)

# Remove triple+ newlines (keep double newlines for paragraphs)
text = re.sub(r"\n\n+", "\n\n", text)
# Remove triple+ newlines (keep double newlines for paragraphs)
text = re.sub(r"\n\n+", "\n\n", text)

return TextArtifact(text)
return TextArtifact(text)
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def search(self, query: str, **kwargs) -> ListArtifact:
]
)
except Exception as e:
raise Exception(f"Error searching '{query}' with DuckDuckGo: {e}")
raise Exception(f"Error searching '{query}' with DuckDuckGo: {e}") from e
2 changes: 1 addition & 1 deletion griptape/mixins/serializable_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_schema(cls: type[T], subclass_name: Optional[str] = None) -> Schema:

@classmethod
def from_dict(cls: type[T], data: dict) -> T:
return cast(T, cls.get_schema(subclass_name=data["type"] if "type" in data else None).load(data))
return cast(T, cls.get_schema(subclass_name=data.get("type")).load(data))

@classmethod
def from_json(cls: type[T], data: str) -> T:
Expand Down
8 changes: 4 additions & 4 deletions griptape/schemas/polymorphic_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def _dump(self, obj, *, update_fields=True, **kwargs):
obj_type = self.get_obj_type(obj)

if not obj_type:
return (None, {"_schema": "Unknown object class: %s" % obj.__class__.__name__})
return (None, {"_schema": f"Unknown object class: {obj.__class__.__name__}"})

type_schema = BaseSchema.from_attrs_cls(obj.__class__)

if not type_schema:
return None, {"_schema": "Unsupported object type: %s" % obj_type}
return None, {"_schema": f"Unsupported object type: {obj_type}"}

schema = type_schema if isinstance(type_schema, Schema) else type_schema()

Expand Down Expand Up @@ -110,7 +110,7 @@ def load(self, data, *, many=None, partial=None, unknown=None, **kwargs):

def _load(self, data, *, partial=None, unknown=None, **kwargs):
if not isinstance(data, dict):
raise ValidationError({"_schema": "Invalid data type: %s" % data})
raise ValidationError({"_schema": f"Invalid data type: {data}"})

data = dict(data)
unknown = unknown or self.unknown
Expand All @@ -121,7 +121,7 @@ def _load(self, data, *, partial=None, unknown=None, **kwargs):

type_schema = self.inner_class.get_schema(data_type)
if not type_schema:
raise ValidationError({self.type_field: ["Unsupported value: %s" % data_type]})
raise ValidationError({self.type_field: [f"Unsupported value: {data_type}"]})

schema = type_schema if isinstance(type_schema, Schema) else type_schema()

Expand Down
4 changes: 2 additions & 2 deletions griptape/structures/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def insert_task(

try:
parent_index = self.tasks.index(parent_task)
except ValueError:
raise ValueError(f"Parent task {parent_task.id} not found in workflow.")
except ValueError as exc:
raise ValueError(f"Parent task {parent_task.id} not found in workflow.") from exc
else:
if parent_index > last_parent_index:
last_parent_index = parent_index
Expand Down
14 changes: 6 additions & 8 deletions griptape/tasks/actions_subtask.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ def run(self) -> BaseArtifact:
self.structure.logger.error(f"Subtask {self.id}\n{e}", exc_info=True)

self.output = ErrorArtifact(str(e), exception=e)
finally:
if self.output is not None:
return self.output
else:
return ErrorArtifact("no tool output")
if self.output is not None:
return self.output
else:
return ErrorArtifact("no tool output")

def execute_actions(self, actions: list[Action]) -> list[tuple[str, BaseArtifact]]:
with self.futures_executor_fn() as executor:
Expand Down Expand Up @@ -234,9 +233,8 @@ def __parse_actions(self, actions_matches: list[str]) -> None:
tag=action_tag, name=action_name, path=action_path, input=action_input, tool=tool
)

if new_action.tool:
if new_action.input:
self.__validate_action(new_action)
if new_action.tool and new_action.input:
self.__validate_action(new_action)

# Don't forget to add it to the subtask actions list!
self.actions.append(new_action)
Expand Down
2 changes: 1 addition & 1 deletion griptape/tasks/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def execute(self) -> Optional[BaseArtifact]:
finally:
self.state = BaseTask.State.FINISHED

return self.output
return self.output

def can_execute(self) -> bool:
return self.state == BaseTask.State.PENDING and all(parent.is_finished() for parent in self.parents)
Expand Down
2 changes: 1 addition & 1 deletion griptape/tasks/prompt_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _process_task_input(
return self._process_task_input(task_input(self))
elif isinstance(task_input, BaseArtifact):
return task_input
elif isinstance(task_input, list) or isinstance(task_input, tuple):
elif isinstance(task_input, (list, tuple)):
return ListArtifact([self._process_task_input(elem) for elem in task_input])
else:
return self._process_task_input(TextArtifact(task_input))
3 changes: 2 additions & 1 deletion griptape/tokenizers/base_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
import logging
from abc import ABC
from abc import ABC, abstractmethod
from attrs import define, field, Factory


Expand Down Expand Up @@ -40,6 +40,7 @@ def count_output_tokens_left(self, text: str) -> int:
else:
return 0

@abstractmethod
def count_tokens(self, text: str) -> int: ...

def _default_max_input_tokens(self) -> int:
Expand Down
9 changes: 6 additions & 3 deletions griptape/utils/chat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Optional, Callable

from typing import TYPE_CHECKING, Callable, Optional

from attrs import Factory, define, field

from griptape.utils.stream import Stream

if TYPE_CHECKING:
Expand All @@ -22,9 +25,9 @@ class Chat:

def default_output_fn(self, text: str) -> None:
if self.structure.config.prompt_driver.stream:
print(text, end="", flush=True)
print(text, end="", flush=True) # noqa T201
else:
print(text)
print(text) # noqa T201

def start(self) -> None:
if self.intro_text:
Expand Down
2 changes: 1 addition & 1 deletion griptape/utils/dict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def dict_merge(dct: dict, merge_dct: dict, add_keys: bool = True) -> dict:
if not add_keys:
merge_dct = {k: merge_dct[k] for k in set(dct).intersection(set(merge_dct))}

for key in merge_dct.keys():
for key in merge_dct:
if key in dct and isinstance(dct[key], dict):
dct[key] = dict_merge(dct[key], merge_dct[key], add_keys=add_keys)
else:
Expand Down
4 changes: 2 additions & 2 deletions griptape/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def import_optional_dependency(name: str) -> Optional[ModuleType]:
)
try:
module = import_module(name)
except ImportError:
raise ImportError(msg)
except ImportError as exc:
raise ImportError(msg) from exc

return module

Expand Down
4 changes: 2 additions & 2 deletions griptape/utils/load_artifact_from_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def load_artifact_from_memory(

try:
artifact = [a for a in artifacts if a.name == artifact_name][0]
except IndexError:
raise ValueError(f"artifact {artifact_name} not found in namespace {artifact_namespace}")
except IndexError as exc:
raise ValueError(f"artifact {artifact_name} not found in namespace {artifact_namespace}") from exc

if not isinstance(artifact, artifact_type):
raise ValueError(f"{artifact.name} is not of type {artifact_type}")
Expand Down
18 changes: 17 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,23 @@ line-length = 120
skip-magic-trailing-comma = true

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "TID251"]
select = [
"E", # pycodestyle
"F", # Pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
"TID251", # banned-api
"T201", # print
]
ignore = [
"UP007", # non-pep604-annotation
"E501", # line-too-long
"B024", # abstract-base-class-without-abstract-method
"B009", # get-attr-with-constant
"B010", # set-attr-with-constant
"SIM108" # if-else-block-instead-of-if-exp
]

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"attr".msg = "The attr module is deprecated, use attrs instead."
Expand Down
3 changes: 2 additions & 1 deletion tests/utils/code_blocks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import pathlib
import textwrap

Expand All @@ -13,7 +14,7 @@ def check_py_string(source: str) -> None:
try:
exec(source, {"__MODULE__": "__main__"})
except Exception:
print(source)
logging.info(source)
raise


Expand Down

0 comments on commit ec06bb0

Please sign in to comment.