Skip to content
Open
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
11 changes: 7 additions & 4 deletions src/tablib/formats/_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import csv
from io import StringIO
from typing import Any, TextIO


class CSVFormat:
Expand All @@ -12,7 +13,7 @@ class CSVFormat:
DEFAULT_DELIMITER = ','

@classmethod
def export_stream_set(cls, dataset, **kwargs):
def export_stream_set(cls, dataset: Any, **kwargs: Any) -> StringIO:
"""Returns CSV representation of Dataset as file-like."""
stream = StringIO()

Expand All @@ -27,13 +28,15 @@ def export_stream_set(cls, dataset, **kwargs):
return stream

@classmethod
def export_set(cls, dataset, **kwargs):
def export_set(cls, dataset: Any, **kwargs: Any) -> str:
"""Returns CSV representation of Dataset."""
stream = cls.export_stream_set(dataset, **kwargs)
return stream.getvalue()

@classmethod
def import_set(cls, dset, in_stream, headers=True, skip_lines=0, **kwargs):
def import_set(
cls, dset: Any, in_stream: TextIO, headers: bool = True, skip_lines: int = 0, **kwargs: Any
) -> None:
"""Returns dataset from CSV stream."""

dset.wipe()
Expand All @@ -52,7 +55,7 @@ def import_set(cls, dset, in_stream, headers=True, skip_lines=0, **kwargs):
dset.append(row)

@classmethod
def detect(cls, stream, delimiter=None):
def detect(cls, stream: TextIO, delimiter: str | None = None) -> bool:
"""Returns True if given stream is valid CSV."""
try:
csv.Sniffer().sniff(stream.read(2048), delimiters=delimiter or cls.DEFAULT_DELIMITER)
Expand Down