Home > OS >  TypeError: Params.my_params is expected to be <class 'params.MyParams'>, but value {
TypeError: Params.my_params is expected to be <class 'params.MyParams'>, but value {

Time:07-26

Goal: read in parameters from .yaml to pass to functions during runtime.

I've seen no reference to this error online; so decided to make a post.

I have a user-defined params.yaml:

my_params:
  host: txt
  project: txt
  roi_term_id: 123

# ...

That is read-in by params.py:

import os
from dataclasses import dataclass
from pathlib import Path
import yaml
from decouple import config
from typed_json_dataclass import TypedJsonMixin


@dataclass
class MyParams(TypedJsonMixin):
    host: str
    project: str
    roi_term: str

    def __post_init__(self):
        self.public_key = config('KEY')
        assert isinstance(self.public_key, str)
        self.private_key = config('SECRET')
        assert isinstance(self.private_key, str)
        super().__post_init__()

# ...
@dataclass
class Params(TypedJsonMixin):
    my_params: MyParams
    # ...


def load_params_dict():
    parameter_file = 'params.yaml'
    cwd = Path(os.getcwd())
    params_path = cwd / parameter_file
    if params_path.exists():
        params = yaml.safe_load(open(params_path))
    else:  # If this script is being called from the path directory
        params_path = cwd.parent / parameter_file
        params = yaml.safe_load(open(params_path))
    return params


params_dict = load_params_dict()
print(params_dict)
project_params = Params.from_dict(params_dict)

Traceback:

  File "/home/me/miniconda3/envs/myvenv/lib/python3.7/site-packages/typed_json_dataclass/typed_json_dataclass.py", line 152, in __post_init__
    expected_type(**field_value)
TypeError: __init__() got an unexpected keyword argument 'roi_term_id'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "path/main.py", line 7, in <module>
    from params import project_params
  File "/home/me/PycharmProjects/project/path/params.py", line 89, in <module>
    project_params = Params.from_dict(params_dict)
  File "/home/me/miniconda3/envs/myvenv/lib/python3.7/site-packages/typed_json_dataclass/typed_json_dataclass.py", line 248, in from_dict
    return cls(**raw_dict)
  File "<string>", line 9, in __init__
  File "/home/me/miniconda3/envs/myvenv/lib/python3.7/site-packages/typed_json_dataclass/typed_json_dataclass.py", line 155, in __post_init__
    raise TypeError(f'{class_name}.{field_name} '
TypeError: Params.my_params is expected to be <class 'params.MyParams'>, but value {'host': 'txt', 'project': 'txt', 'roi_term_id': 123} is a dict with unexpected keys

CodePudding user response:

2 things, name and dtype.

Subject:

roi_term: str

Name: Keys' names in params.yaml must be the exact same as the attributes' names in a class (and I assume order).

dtype: Attribute in class says str, but when the params.yaml file gets parsed it is considered int - since the value in the file is a whole number. I changed str to int in MyParams class.

Thus, roi_term_id from params.yaml conflicted with roi_term from MyParams class.

  • Related