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
20 changes: 18 additions & 2 deletions dataclass_csv/dataclass_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import csv

from datetime import date, datetime
from distutils.util import strtobool
from typing import Union, Type, Optional, Sequence, Dict, Any, List

import typing
Expand Down Expand Up @@ -31,6 +30,23 @@ def _verify_duplicate_header_items(header):
)


# this is a port from distutils.util.strtobool
def _strtobool(val: str) -> bool:
"""Convert a string representation of truth to true (1) or false (0).

True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError(f"invalid truth value {val!r}")


def is_union_type(t):
if hasattr(t, "__origin__") and t.__origin__ is Union:
return True
Expand Down Expand Up @@ -220,7 +236,7 @@ def _process_row(self, row):
transformed_value = (
value
if isinstance(value, bool)
else strtobool(str(value).strip()) == 1
else _strtobool(str(value).strip()) == 1
)
except ValueError as ex:
raise CsvValueError(ex, line_number=self._reader.line_num) from None
Expand Down