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

Add support to attributes without default values #13

Closed
miikargh opened this issue Aug 11, 2020 · 2 comments · Fixed by #14
Closed

Add support to attributes without default values #13

miikargh opened this issue Aug 11, 2020 · 2 comments · Fixed by #14
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@miikargh
Copy link

miikargh commented Aug 11, 2020

Problem

yaml-data-class-config doesn't currently support attributes without default values.

Cause

The attribute FILE_PATH in config.py that does not have a default value causes the error.

Possible solution

Remove the FILE_PATH attribute or add it after the config.yml is loaded so that it doesn't cause problems with mypy.

Example

config.yml:

property_a: 1
property_b: '2'

example.py:

from dataclasses import dataclass, field
from yamldataclassconfig.config import YamlDataClassConfig


@dataclass
class Config(YamlDataClassConfig):
    property_a: int
    property_b: str

Running mypy example.py throws the following errors:

problem.py:7: error: Attributes without a default cannot follow attributes with one
problem.py:8: error: Attributes without a default cannot follow attributes with one
Found 2 errors in 1 file (checked 1 source file)

One can go around this problem by adding Union[None, <type>] to every attribute but this is not ideal since one might want to have attributes that throw error if no value is provided.

Environment

  • Ubuntu 20.04.1 LTS x86_64
  • mypy==0.782
  • python==3.8.5

Thanks for the great library by the way!

yukihiko-shinoda added a commit that referenced this issue Aug 11, 2020
Mypy doesn't check function return value even if it is field which has
init=False .

@see #13
@yukihiko-shinoda
Copy link
Owner

yukihiko-shinoda commented Aug 11, 2020

Hi, thanks for your feedback!

I deployed new version 1.4.0, would you check it ?
FILE_PATH was already specified as field(init=False).

cf. dataclasses.field | dataclasses — Data Classes — Python 3.8.5 documentation

However, mypy seems to not check it if its definition is return value of method.
I checked it recovered by this update.
163532d

config.yml:

property_a: 1
property_b: '2'

test.py:

from dataclasses import dataclass
from yamldataclassconfig.config import YamlDataClassConfig

@dataclass
class Config(YamlDataClassConfig):
    property_a: int
    property_b: str


CONFIG: Config = Config(0, "1")
CONFIG.load()
print(CONFIG.property_a)
print(CONFIG.property_b)

Result:

$ pipenv run python test.py
1
2
$ pipenv run mypy test.py
Success: no issues found in 1 source file

@yukihiko-shinoda yukihiko-shinoda added enhancement New feature or request good first issue Good for newcomers labels Aug 11, 2020
@yukihiko-shinoda yukihiko-shinoda linked a pull request Aug 11, 2020 that will close this issue
@miikargh
Copy link
Author

Seems to work well after updating to yamldataclassconfig==1.4.0. Thanks a bunch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants