Skip to content

Commit 872ce02

Browse files
authored
Fix issue when getting the field type (#44)
* Fix issue when getting the field type * Updated history log * Updated version
1 parent 0ba2809 commit 872ce02

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

HISTORY.md

+8
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@
6767
* Added type hinting to external API
6868
* Documentation updates
6969
* Bug fixes
70+
71+
## 1.3.0 (2021-04-10)
72+
73+
* Included stub files
74+
* check if the CSV file has duplicated header values
75+
* Fixed issues #22 and #33
76+
* code cleanup
77+

dataclass_csv/dataclass_reader.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from distutils.util import strtobool
66
from typing import Union, Type, Optional, Sequence, Dict, Any, List
77

8+
import typing
9+
810
from .field_mapper import FieldMapper
911
from .exceptions import CsvValueError
1012

@@ -29,6 +31,20 @@ def _verify_duplicate_header_items(header):
2931
)
3032

3133

34+
def is_union_type(t):
35+
if hasattr(t, "__origin__") and t.__origin__ is Union:
36+
return True
37+
38+
return False
39+
40+
41+
def get_args(t):
42+
if hasattr(t, "__args__"):
43+
return t.__args__
44+
45+
return tuple()
46+
47+
3248
class DataclassReader:
3349
def __init__(
3450
self,
@@ -61,6 +77,8 @@ def __init__(
6177
if validate_header:
6278
_verify_duplicate_header_items(self._reader.fieldnames)
6379

80+
self.type_hints = typing.get_type_hints(cls)
81+
6482
def _get_optional_fields(self):
6583
return [
6684
field.name
@@ -175,20 +193,12 @@ def _process_row(self, row):
175193
values.append(None)
176194
continue
177195

178-
field_type = field.type
179-
# Special handling for Optional (Union of a single real type and None)
180-
if (
181-
# The first part of the condition is for Python < 3.8
182-
type(field_type).__name__ == "_Union"
183-
# The second part of the condition is for Python >= 3.8
184-
or "__origin__" in field_type.__dict__
185-
and field_type.__origin__ is Union
186-
):
187-
real_types = [
188-
t for t in field_type.__args__ if t is not type(None) # noqa: E721
189-
]
190-
if len(real_types) == 1:
191-
field_type = real_types[0]
196+
field_type = self.type_hints[field.name]
197+
198+
if is_union_type(field_type):
199+
type_args = [x for x in get_args(field_type) if x is not type(None)]
200+
if len(type_args) == 1:
201+
field_type = type_args[0]
192202

193203
if field_type is datetime:
194204
try:

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.2.0
2+
current_version = 1.3.0
33
commit = True
44
tag = True
55

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@
5151
test_suite="tests",
5252
tests_require=test_requirements,
5353
url="https://github.com/dfurtado/dataclass-csv",
54-
version="1.2.0",
54+
version="1.3.0",
5555
zip_safe=False,
5656
)

0 commit comments

Comments
 (0)