Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flake8-pytest-style
Browse files Browse the repository at this point in the history
collindutter committed Jul 16, 2024
1 parent 50603cf commit 2383542
Showing 166 changed files with 340 additions and 347 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -217,6 +217,7 @@ select = [
"C4", # flake8-comprehensions
"ANN", # flake8-annotations
"FBT", # flake8-boolean-trap
"PT", # flake8-pytest-style
]
ignore = [
"UP007", # non-pep604-annotation
@@ -238,12 +239,15 @@ ignore = [
"ANN101", # missing-type-self
"ANN102", # missing-type-cls
"ANN401", # any-type
"PT011", # pytest-raises-too-broad
]
[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["I"]
"__init__.py" = [
"I" # isort
]
"tests/*" = [
"ANN001", # missing-type-function-argument
"ANN201", # missing-return-type-undocumented-public-function
Original file line number Diff line number Diff line change
@@ -15,11 +15,11 @@ class TestPgVectorVectorStoreDriver:
vec1 = [0.1, 0.2, 0.3]
vec2 = [0.4, 0.5, 0.6]

@pytest.fixture
@pytest.fixture()
def embedding_driver(self):
return MockEmbeddingDriver()

@pytest.fixture
@pytest.fixture()
def vector_store_driver(self, embedding_driver):
driver = PgVectorVectorStoreDriver(
connection_string=self.connection_string, embedding_driver=embedding_driver, table_name=self.table_name
@@ -30,8 +30,8 @@ def vector_store_driver(self, embedding_driver):
return driver

def test_initialize_requires_engine_or_connection_string(self, embedding_driver):
driver = PgVectorVectorStoreDriver(embedding_driver=embedding_driver, table_name=self.table_name)
with pytest.raises(ValueError):
driver = PgVectorVectorStoreDriver(embedding_driver=embedding_driver, table_name=self.table_name)
driver.setup()

def test_initialize_accepts_engine(self, embedding_driver):
2 changes: 1 addition & 1 deletion tests/mocks/docker/fake_api.py
Original file line number Diff line number Diff line change
@@ -150,7 +150,7 @@ def post_fake_create_container():
return status_code, response


def get_fake_inspect_container(tty=False):
def get_fake_inspect_container(*, tty=False):
status_code = 200
response = {
"Id": FAKE_CONTAINER_ID,
2 changes: 1 addition & 1 deletion tests/unit/artifacts/test_audio_artifact.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@


class TestAudioArtifact:
@pytest.fixture
@pytest.fixture()
def audio_artifact(self):
return AudioArtifact(value=b"some binary audio data", format="pcm", model="provider/model", prompt="two words")

2 changes: 1 addition & 1 deletion tests/unit/artifacts/test_base_media_artifact.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ class TestMediaArtifact:
class ImaginaryMediaArtifact(MediaArtifact):
media_type: str = "imagination"

@pytest.fixture
@pytest.fixture()
def media_artifact(self):
return self.ImaginaryMediaArtifact(value=b"some binary dream data", format="dream")

5 changes: 3 additions & 2 deletions tests/unit/artifacts/test_boolean_artifact.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: FBT003
import pytest

from griptape.artifacts import BooleanArtifact
@@ -14,14 +15,14 @@ def test_parse_bool(self):
BooleanArtifact.parse_bool("foo")

with pytest.raises(ValueError):
BooleanArtifact.parse_bool(None)
BooleanArtifact.parse_bool(None) # pyright: ignore[reportArgumentType]

assert BooleanArtifact.parse_bool(True).value is True
assert BooleanArtifact.parse_bool(False).value is False

def test_add(self):
with pytest.raises(ValueError):
BooleanArtifact(True) + BooleanArtifact(True)
BooleanArtifact(True) + BooleanArtifact(True) # pyright: ignore[reportUnusedExpression]

def test_value_type_conversion(self):
assert BooleanArtifact(1).value is True
2 changes: 1 addition & 1 deletion tests/unit/artifacts/test_image_artifact.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@


class TestImageArtifact:
@pytest.fixture
@pytest.fixture()
def image_artifact(self):
return ImageArtifact(
value=b"some binary png image data",
2 changes: 1 addition & 1 deletion tests/unit/chunkers/test_markdown_chunker.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@


class TestTextChunker:
@pytest.fixture
@pytest.fixture()
def chunker(self):
return MarkdownChunker(max_tokens=MAX_TOKENS)

2 changes: 1 addition & 1 deletion tests/unit/chunkers/test_pdf_chunker.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@


class TestPdfChunker:
@pytest.fixture
@pytest.fixture()
def chunker(self):
return PdfChunker(max_tokens=MAX_TOKENS)

2 changes: 1 addition & 1 deletion tests/unit/chunkers/test_text_chunker.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@


class TestTextChunker:
@pytest.fixture
@pytest.fixture()
def chunker(self):
return TextChunker(max_tokens=MAX_TOKENS)

2 changes: 1 addition & 1 deletion tests/unit/common/test_prompt_stack.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@


class TestPromptStack:
@pytest.fixture
@pytest.fixture()
def prompt_stack(self):
return PromptStack()

10 changes: 5 additions & 5 deletions tests/unit/config/test_amazon_bedrock_structure_config.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import boto3
from pytest import fixture
import pytest

from griptape.config import AmazonBedrockStructureConfig
from tests.utils.aws import mock_aws_credentials


class TestAmazonBedrockStructureConfig:
@fixture(autouse=True)
def run_before_and_after_tests(self):
@pytest.fixture(autouse=True)
def _run_before_and_after_tests(self):
mock_aws_credentials()

@fixture
@pytest.fixture()
def config(self):
mock_aws_credentials()
return AmazonBedrockStructureConfig()

@fixture
@pytest.fixture()
def config_with_values(self):
return AmazonBedrockStructureConfig(
session=boto3.Session(
8 changes: 4 additions & 4 deletions tests/unit/config/test_anthropic_structure_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from pytest import fixture
import pytest

from griptape.config import AnthropicStructureConfig


class TestAnthropicStructureConfig:
@fixture(autouse=True)
def mock_anthropic(self, mocker):
@pytest.fixture(autouse=True)
def _mock_anthropic(self, mocker):
mocker.patch("anthropic.Anthropic")
mocker.patch("voyageai.Client")

@fixture
@pytest.fixture()
def config(self):
return AnthropicStructureConfig()

6 changes: 3 additions & 3 deletions tests/unit/config/test_azure_openai_structure_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from pytest import fixture
import pytest

from griptape.config import AzureOpenAiStructureConfig


class TestAzureOpenAiStructureConfig:
@fixture(autouse=True)
@pytest.fixture(autouse=True)
def mock_openai(self, mocker):
return mocker.patch("openai.AzureOpenAI")

@fixture
@pytest.fixture()
def config(self):
return AzureOpenAiStructureConfig(
azure_endpoint="http://localhost:8080",
4 changes: 2 additions & 2 deletions tests/unit/config/test_cohere_structure_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from pytest import fixture
import pytest

from griptape.config import CohereStructureConfig


class TestCohereStructureConfig:
@fixture
@pytest.fixture()
def config(self):
return CohereStructureConfig(api_key="api_key")

6 changes: 3 additions & 3 deletions tests/unit/config/test_google_structure_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from pytest import fixture
import pytest

from griptape.config import GoogleStructureConfig


class TestGoogleStructureConfig:
@fixture(autouse=True)
@pytest.fixture(autouse=True)
def mock_openai(self, mocker):
return mocker.patch("google.generativeai.GenerativeModel")

@fixture
@pytest.fixture()
def config(self):
return GoogleStructureConfig()

6 changes: 3 additions & 3 deletions tests/unit/config/test_openai_structure_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from pytest import fixture
import pytest

from griptape.config import OpenAiStructureConfig


class TestOpenAiStructureConfig:
@fixture(autouse=True)
@pytest.fixture(autouse=True)
def mock_openai(self, mocker):
return mocker.patch("openai.OpenAI")

@fixture
@pytest.fixture()
def config(self):
return OpenAiStructureConfig()

4 changes: 2 additions & 2 deletions tests/unit/config/test_structure_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from pytest import fixture
import pytest

from griptape.config import StructureConfig


class TestStructureConfig:
@fixture
@pytest.fixture()
def config(self):
return StructureConfig()

Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

class TestAmazonBedrockCohereEmbeddingDriver:
@pytest.fixture(autouse=True)
def mock_session(self, mocker):
def _mock_session(self, mocker):
fake_embeddings = '{"embeddings": [[0, 1, 0]] }'

mock_session_class = mocker.patch("boto3.Session")
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

class TestAmazonBedrockTitanEmbeddingDriver:
@pytest.fixture(autouse=True)
def mock_session(self, mocker):
def _mock_session(self, mocker):
fake_embeddings = '{"embedding": [0, 1, 0]}'

mock_session_class = mocker.patch("boto3.Session")
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ def mock_openai(self, mocker):

return mock_chat_create

@pytest.fixture
@pytest.fixture()
def driver(self):
return AzureOpenAiEmbeddingDriver(azure_endpoint="foobar", model="gpt-4", azure_deployment="foobar")

2 changes: 1 addition & 1 deletion tests/unit/drivers/embedding/test_base_embedding_driver.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@


class TestBaseEmbeddingDriver:
@pytest.fixture
@pytest.fixture()
def driver(self):
return MockEmbeddingDriver()

Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@


class TestDummyEmbeddingDriver:
@pytest.fixture
@pytest.fixture()
def embedding_driver(self):
return DummyEmbeddingDriver()

Original file line number Diff line number Diff line change
@@ -43,19 +43,17 @@ def test_try_embed_chunk(self, mock_client):
).try_embed_chunk("foobar") == [0, 2, 0]

mock_client.get().read.return_value = b'{"embedding": []}'
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="model response is empty"):
assert AmazonSageMakerJumpstartEmbeddingDriver(
endpoint="test-endpoint",
model="test-model",
tokenizer=OpenAiTokenizer(model=OpenAiTokenizer.DEFAULT_OPENAI_GPT_3_CHAT_MODEL),
).try_embed_chunk("foobar") == [0, 2, 0]
assert str(e) == "model response is empty"

mock_client.get().read.return_value = b"{}"
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="invalid response from model"):
assert AmazonSageMakerJumpstartEmbeddingDriver(
endpoint="test-endpoint",
model="test-model",
tokenizer=OpenAiTokenizer(model=OpenAiTokenizer.DEFAULT_OPENAI_GPT_3_CHAT_MODEL),
).try_embed_chunk("foobar") == [0, 2, 0]
assert str(e) == "invalid response from model"
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import boto3
import pytest
from moto import mock_sqs
from pytest import fixture

from griptape.drivers.event_listener.amazon_sqs_event_listener_driver import AmazonSqsEventListenerDriver
from tests.mocks.mock_event import MockEvent
from tests.utils.aws import mock_aws_credentials


class TestAmazonSqsEventListenerDriver:
@fixture()
def run_before_and_after_tests(self):
@pytest.fixture()
def _run_before_and_after_tests(self):
mock_aws_credentials()

@fixture()
@pytest.fixture()
def driver(self):
mock = mock_sqs()
mock.start()
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import boto3
import pytest
from moto import mock_iotdata
from pytest import fixture

from griptape.drivers.event_listener.aws_iot_core_event_listener_driver import AwsIotCoreEventListenerDriver
from tests.mocks.mock_event import MockEvent
@@ -9,11 +9,11 @@

@mock_iotdata
class TestAwsIotCoreEventListenerDriver:
@fixture(autouse=True)
def run_before_and_after_tests(self):
@pytest.fixture(autouse=True)
def _run_before_and_after_tests(self):
mock_aws_credentials()

@fixture()
@pytest.fixture()
def driver(self):
return AwsIotCoreEventListenerDriver(
iot_endpoint="foo bar", topic="fizz buzz", session=boto3.Session(region_name="us-east-1")
Loading

0 comments on commit 2383542

Please sign in to comment.