seequent / properties

An organizational aid and wrapper for validation and tab completion of class properties/traits.
http://propertiespy.rtfd.org
MIT License
18 stars 9 forks source link

Allow Instance to initialize from dict with '__class__' specified #255

Closed fwkoch closed 6 years ago

fwkoch commented 6 years ago

This is only valid if __class__ is identical to the instance_class name. If more complicated behavior is required, deserialize should be used.

As an example, say you have:

import properties

class Blah(properties.HasProperties):
    a = properties.Integer('')

class HasBlah(properties.HasProperties):
    b = properties.Instance('', Blah)

previously this would be fine:

val = HasBlah(b={'a': 5})

but this would fail:

val = HasBlah(b={'a': 5, '__class__': 'Blah'})

instead requiring:

val = HasBlah.deserialize({'b': {'a': 5, '__class__': 'Blah'}})

With this PR, all of the above are valid.

codecov[bot] commented 6 years ago

Codecov Report

Merging #255 into dev will decrease coverage by 0.03%. The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #255      +/-   ##
==========================================
- Coverage   96.24%   96.21%   -0.04%     
==========================================
  Files          16       16              
  Lines        2319     2322       +3     
==========================================
+ Hits         2232     2234       +2     
- Misses         87       88       +1
Impacted Files Coverage Δ
properties/base/instance.py 95.6% <66.66%> (-0.99%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 0fec8ee...9d23620. Read the comment docs.

fwkoch commented 6 years ago

I'm rejecting - this conflates initialization and deserialization. On init, we shouldn't see __class__, if we have a dictionary with that present, we should be deserializing instead (since that key would be created with serialize).