From f21974b4a17e9d8382bcf79e88deeab850eda4c6 Mon Sep 17 00:00:00 2001 From: Matt Stone Date: Sun, 24 Mar 2024 19:42:04 -0400 Subject: [PATCH] refactor: rename assertions --- dataclass_io/_lib/assertions.py | 8 ++++---- dataclass_io/reader.py | 10 ++++------ tests/_lib/test_assertions.py | 28 ++++++++++++++-------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/dataclass_io/_lib/assertions.py b/dataclass_io/_lib/assertions.py index 59e6c44..cdb67b7 100644 --- a/dataclass_io/_lib/assertions.py +++ b/dataclass_io/_lib/assertions.py @@ -6,7 +6,7 @@ from dataclass_io._lib.dataclass_extensions import DataclassInstance -def assert_readable_file(path: Path) -> None: +def assert_file_is_readable(path: Path) -> None: """ Check that the input file exists and is readable. @@ -26,7 +26,7 @@ def assert_readable_file(path: Path) -> None: raise PermissionError(f"The input file is not readable: {path}") -def assert_readable_dataclass(dc_type: type[DataclassInstance]) -> None: +def assert_dataclass_is_valid(dataclass_type: type[DataclassInstance]) -> None: """ Check that the input type is a parseable dataclass. @@ -34,5 +34,5 @@ def assert_readable_dataclass(dc_type: type[DataclassInstance]) -> None: TypeError: If the provided type is not a dataclass. """ - if not is_dataclass(dc_type): - raise TypeError(f"The provided type must be a dataclass: {dc_type.__name__}") + if not is_dataclass(dataclass_type): + raise TypeError(f"The provided type must be a dataclass: {dataclass_type.__name__}") diff --git a/dataclass_io/reader.py b/dataclass_io/reader.py index 1653f66..957adaf 100644 --- a/dataclass_io/reader.py +++ b/dataclass_io/reader.py @@ -11,10 +11,8 @@ from typing import Type from typing import TypeAlias -from dataclass_io._lib.assertions import assert_readable_dataclass -from dataclass_io._lib.assertions import assert_readable_file - -# from dataclass_io._lib.dataclass_extensions import DataclassType +from dataclass_io._lib.assertions import assert_dataclass_is_valid +from dataclass_io._lib.assertions import assert_file_is_readable from dataclass_io._lib.dataclass_extensions import DataclassInstance ReadableFileHandle: TypeAlias = TextIOWrapper | IO | TextIO @@ -58,8 +56,8 @@ def __init__( TypeError: If the provided type is not a dataclass. """ - assert_readable_file(path) - assert_readable_dataclass(dataclass_type) + assert_file_is_readable(path) + assert_dataclass_is_valid(dataclass_type) self.dataclass_type = dataclass_type self.delimiter = delimiter diff --git a/tests/_lib/test_assertions.py b/tests/_lib/test_assertions.py index a2fe3b1..339ee2c 100644 --- a/tests/_lib/test_assertions.py +++ b/tests/_lib/test_assertions.py @@ -3,11 +3,11 @@ import pytest -from dataclass_io._lib.assertions import assert_readable_dataclass -from dataclass_io._lib.assertions import assert_readable_file +from dataclass_io._lib.assertions import assert_dataclass_is_valid +from dataclass_io._lib.assertions import assert_file_is_readable -def test_assert_readable_dataclass() -> None: +def test_assert_dataclass_is_valid() -> None: """ Test that we can validate if a dataclass is valid for reading. """ @@ -18,12 +18,12 @@ class FakeDataclass: bar: int try: - assert_readable_dataclass(FakeDataclass) + assert_dataclass_is_valid(FakeDataclass) except TypeError: raise AssertionError("Failed to validate a valid dataclass") from None -def test_assert_readable_dataclass_raises_if_not_a_dataclass() -> None: +def test_assert_dataclass_is_valid_raises_if_not_a_dataclass() -> None: """ Test that we raise an error if the provided type is not a dataclass. """ @@ -35,10 +35,10 @@ class BadDataclass: with pytest.raises(TypeError, match="The provided type must be a dataclass: BadDataclass"): # mypy (correctly) flags that `BadDataclass` is not a dataclass. # We still want to test that we can enforce this at runtime, so here it's ok to ignore. - assert_readable_dataclass(BadDataclass) # type: ignore[arg-type] + assert_dataclass_is_valid(BadDataclass) # type: ignore[arg-type] -def test_assert_readable_file(tmp_path: Path) -> None: +def test_assert_file_is_readable(tmp_path: Path) -> None: """ Test that we can validate if a file is valid for reading. """ @@ -47,30 +47,30 @@ def test_assert_readable_file(tmp_path: Path) -> None: fpath.touch() try: - assert_readable_file(fpath) + assert_file_is_readable(fpath) except Exception: raise AssertionError("Failed to validate a valid file") from None -def test_assert_readable_file_raises_if_file_does_not_exist(tmp_path: Path) -> None: +def test_assert_file_is_readable_raises_if_file_does_not_exist(tmp_path: Path) -> None: """ Test that we can validate if a file does not exist. """ with pytest.raises(FileNotFoundError, match="The input file does not exist: "): - assert_readable_file(tmp_path / "does_not_exist.txt") + assert_file_is_readable(tmp_path / "does_not_exist.txt") -def test_assert_readable_file_raises_if_file_is_a_directory(tmp_path: Path) -> None: +def test_assert_file_is_readable_raises_if_file_is_a_directory(tmp_path: Path) -> None: """ Test that we can validate if a file does not exist. """ with pytest.raises(IsADirectoryError, match="The input file path is a directory: "): - assert_readable_file(tmp_path) + assert_file_is_readable(tmp_path) -def test_assert_readable_file_raises_if_file_is_unreadable(tmp_path: Path) -> None: +def test_assert_file_is_readable_raises_if_file_is_unreadable(tmp_path: Path) -> None: """ Test that we can validate if a file cannot be read. """ @@ -79,4 +79,4 @@ def test_assert_readable_file_raises_if_file_is_unreadable(tmp_path: Path) -> No fpath.touch(0) with pytest.raises(PermissionError, match="The input file is not readable: "): - assert_readable_file(fpath) + assert_file_is_readable(fpath)