Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions dataclass_csv/dataclass_writer.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import csv
import dataclasses
from typing import Type, Dict, Any, List
from typing import Type, Dict, Any, Iterable
from .header_mapper import HeaderMapper


class DataclassWriter:
def __init__(
self,
f: Any,
data: List[Any],
data: Iterable,
cls: Type[object],
dialect: str = "excel",
**fmtparams: Dict[str, Any],
):
if not f:
raise ValueError("The f argument is required")

if not isinstance(data, list):
raise ValueError("Invalid 'data' argument. It must be a list")
try:
iter(data)
except TypeError:
raise ValueError("Invalid 'data' argument. It must be an iterable")

if not dataclasses.is_dataclass(cls):
raise ValueError("Invalid 'cls' argument. It must be a dataclass")
Expand Down Expand Up @@ -54,7 +56,7 @@ def write(self, skip_header: bool = False):
raise TypeError(
(
f"The item [{item}] is not an instance of "
f"{self._cls.__name__}. All items on the list must be "
f"{self._cls.__name__}. All items in the iterable must be "
"instances of the same type"
)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dataclass_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_invalid_file_value(tmpdir_factory):
DataclassWriter(None, users, User)


def test_with_data_not_a_list(tmpdir_factory):
def test_with_data_not_an_iterable(tmpdir_factory):
tempfile = tmpdir_factory.mktemp("data").join("user_001.csv")

users = User(name="test", age=40)
Expand Down