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

attr_getters attrs not available during __post_init__ #156

Open
paulmakepeace opened this issue Feb 28, 2022 · 0 comments
Open

attr_getters attrs not available during __post_init__ #156

paulmakepeace opened this issue Feb 28, 2022 · 0 comments

Comments

@paulmakepeace
Copy link

paulmakepeace commented Feb 28, 2022

I'm trying to pass in a config object that can be used to set defaults for attributes dynamically:

from dataclasses import dataclass
import jsons

@dataclass
class Car(jsons.JsonSerializable):
  engine: str = None

  def __post_init__(self):
    if self.engine is None
      self.engine = self.config["engine_default"]

car = Car.loads("{}", attr_getters={"config": lambda: {"engine_default": "mid"}})

fails with,

jsons.exceptions.DeserializationError:
  Could not deserialize value "{}" into "__main__.Car". 'Car' object has no attribute 'config'

(If there's no __post_init__, car.config works just fine here, but later in the cycle.)

If there's a better way of achieving this I'd be happy to hear it--I'd likely use the same idea to inject a logger dependency too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant