Skip to content
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

Raise an error when using field(init=False) #9

Closed
dfurtado opened this issue Feb 9, 2019 · 5 comments
Closed

Raise an error when using field(init=False) #9

dfurtado opened this issue Feb 9, 2019 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@dfurtado
Copy link
Owner

dfurtado commented Feb 9, 2019

Description

The issue occurs when defining a field in the dataclass with the property init=False. When it init property is set to false it means that the auto-generated dataclass initializer won't add the field in the list of arguments.

How to reproduce

  1. Create a dataclass and set init=False in one of the fields, for example:
from dataclasses import dataclass, field

from dataclass_csv import DataclassReader


@dataclass
class User:
    firstname: str
    lastname: str
    age: int = field(init=False)
  1. Try reading a csv file containing the expected data
def main():

    with open('users.csv') as f:
        reader = DataclassReader(f, User)

        for row in reader:
            print(row)


if __name__ == '__main__':
    main()
  1. You will see the error on the terminal:
Traceback (most recent call last):
  File "app.py", line 25, in <module>
    main()
  File "app.py", line 20, in main
    for row in reader:
  File "/home/daniel/Envs/test-oGAe6Cfg/lib/python3.7/site-packages/dataclass_csv/dataclass_reader.py", line 161, in __next__
    return self._process_row(row)
  File "/home/daniel/Envs/test-oGAe6Cfg/lib/python3.7/site-packages/dataclass_csv/dataclass_reader.py", line 157, in _process_row
    return self.cls(*values)
TypeError: __init__() takes 3 positional arguments but 4 were given

Expected behavior

When setting init=False in a dataclass field, the field should not be passed to the dataclass initializer.

Other information

OS: Debian 4.9.130-2
Python version: 3.7.1
dataclass-csv version: 1.0.1

@dfurtado dfurtado added the bug Something isn't working label Feb 9, 2019
@schobster
Copy link
Contributor

Hi @dfurtado - feel free to assign this one (and/or #10) to me if you would like some assistance 😸

@dfurtado
Copy link
Owner Author

dfurtado commented Feb 13, 2019

Hi @schobster , thanks a lot, I actually started on this one, I forgot to assign it to me. I will assign the #10 to you.

@rtdean
Copy link

rtdean commented Jul 14, 2019

Just hit this one, but in a slightly different way. I have a field that's marked as init=False, and doesn't have a corresponding field to map into it, and dataclass_csv doesn't like that much:

KeyError: 'The value xyzzy is missing in the CSV file.'

@rtdean
Copy link

rtdean commented Jul 14, 2019

It looks like the fix in #15 should fix my side of the issue as well.

@dfurtado
Copy link
Owner Author

dfurtado commented Mar 1, 2020

Solved #15

@dfurtado dfurtado closed this as completed Mar 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants