jacebrowning / yorm

Automatic object-YAML mapping for Python.
https://yorm.readthedocs.io
MIT License
27 stars 6 forks source link

Dictionaries Defaulting? #144

Closed AstraLuma closed 7 years ago

AstraLuma commented 7 years ago

I'm tracking down an issue where my Dictionaries are not picking up the values given in __init__(), instead they use the default values. Problem is, the trivial version works just fine.

The trivial version is:

import yorm
from yorm.types import Dictionary, String, Integer
import types
from pprint import pprint

@yorm.attr(count=Integer)
class EggDict(Dictionary):
    pass

@yorm.attr(spam=String)
@yorm.attr(eggs=EggDict)
@yorm.sync('/tmp/spamspameggsspam')
class Breakfast(types.SimpleNamespace):
    pass

b = Breakfast(spam="yes", eggs={'count': 2})

pprint(b.eggs)

My full app would print {'count': 0} in this case, but this reduced version is printing (correctly) {'count': 2}.

Will dig in to more tomorrow.

jacebrowning commented 7 years ago

Are you calling super().__init__()?

AstraLuma commented 7 years ago

There is no __init__(), the class definition is an accurate representation. It's just smaller than the real one.

AstraLuma commented 7 years ago

types.SimpleNamespace

jacebrowning commented 7 years ago

Can you enumerate the differences between your reduced version and full app?

AstraLuma commented 7 years ago

Way more attributes, including more dicts and lists. Has methods, class methods, and a read-only property descriptor.

jacebrowning commented 7 years ago

The number of attributes shouldn't matter. Are any of the methods, class methods, or properties conflicting with the names of mapped attributes or any other potentially reserved words?

Without seeing the code, I'm not sure what else to say. Can you produce a minimum example by whittling down your code?

AstraLuma commented 7 years ago

headdesk

It's only occurring inside the real application. I pull the models out (hard code format_path and remove debugging print()) into a test script and it behaves just fine.

AstraLuma commented 7 years ago

Does yorm have opinions on threading? All of my work is done on background threads (concurrent.futures.ThreadPoolExecutor).

EDIT: Note that for debugging, I have the pool size set to 1.

jacebrowning commented 7 years ago

I'd image you'd end up with multiple threads trying to overwrite the mapped object's file with potentially different values.

Can you run a test with threading completely removed (not just setting the pool size to 1).

You could also try adding auto_save=False to the yorm.sync() decorator and manually calling yorm.save(obj) in your main thread after making changes.

AstraLuma commented 7 years ago

Oh, data loaded from the file overrides data set by __init__().