-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Empty string throws exception #59
Comments
@eric-j-ason A easy workaround is to specify an empty string as the default value: @dataclass
class Person:
firstname: str
lastname: str = ""
motto: str However, I do believe that dataclass-csv should interpret an empty string field as an empty string, which would be consistent with the behavior of Python's csv module. The following code example below is both a comparison between Python's csv module and dataclass-csv, and to demonstrate what works for dataclass-csv: import io
import csv
from dataclasses import dataclass
from typing import Optional
from dataclass_csv import DataclassReader, DataclassWriter
output = io.StringIO()
# csv module's default behavior is to output empty string and `None` both as empty string
# Using `csv.QUOTE_NOTNULL` in Python 3.12 can differentiate between `None` and not `None` though
writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
writer.writerow(["empty", "", "none", None])
print(output.getvalue())
# When reading an empty string, it would still be interpreted as an empty string
output.seek(0)
reader = csv.reader(output)
print(list(reader))
@dataclass
class Test:
a: str
# (*) This works, but not providing a default value would cause issues below
b: str = ""
# The line below also works, except that the default value is `None`
# b: Optional[str] = None
# This does not work
# b: str
# It's a bit counterintuitive, but this also does not work
# b: Optional[str]
output = io.StringIO()
writer = DataclassWriter(output, [Test("empty", "")], Test)
writer.write()
print(output.getvalue())
output.seek(0)
reader = DataclassReader(output, Test)
# Line below would raise exception if `Test.b` does not have a default value according to (*)
print(list(reader))
output = io.StringIO()
# There is a type mismatch here using `None` though, but this is just for demonstration
writer = DataclassWriter(output, [Test("none", None)], Test)
writer.write()
print(output.getvalue())
output.seek(0)
reader = DataclassReader(output, Test)
# Line below would raise exception if `Test.b` does not have a default value according to (*)
print(list(reader)) The output is:
Additionally, you can see that only using dataclass-csv/dataclass_csv/dataclass_reader.py Lines 82 to 88 in 2dc71be
|
Thank you, but that catches me out with another error:
|
@eric-j-ason Oops, forgot to test, just deal with the error accordingly, putting @dataclass
class Person:
firstname: str
motto: str
lastname: str = "" Or you can add a default value to @dataclass
class Person:
firstname: str
lastname: str = ""
motto: str = "" |
Empty strings in a CSV file, such as the middle field in
Greg,,The world's Greggest!
, causes an exception to be thrown when read.Example:
Result:
Using dataclass-csv 1.4.0.
The text was updated successfully, but these errors were encountered: