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

dictionary-named methods are raising a validation exception #489

Merged
merged 1 commit into from
May 24, 2017

Conversation

toumorokoshi
Copy link

There's currently a bug where naming fields of a model after the dictionary methods will raise an incorrect DataError during validation (see unit test or toumorokoshi/transmute-core#20)

Looking at the documentation, the atoms() method shouldn't support anything besides a mapping anyway. This builds off of that assumption, and removes the getattr call.

@lkraider
Copy link
Contributor

lkraider commented May 23, 2017

Indeed this will fix this test case, but users that override other mapping methods (ex: "keys") will still break (relevant: #375). Also, overriding "get" will break other things (ex: m.atoms() will not work anymore to retrieve data), so you might get Undefined values depending on the case, as it will break this current assumption:

convert(m._schema, m) == convert(m._schema, m._data)

I could go ahead and merge this, but not sure a new release is warranted until a more general fix is achieved.

@toumorokoshi
Copy link
Author

toumorokoshi commented May 23, 2017

@lkraider thanks, that ticket provided a lot of context. I see why this doesn't solve the larger collisions that occur between field names and the reserved method names of the Mapping type itself.

It sounds like there's a lot of work to get to the point of separating the two apart (and a significant part backwards-incompatible). I can avoid dealing with this head on by using serialize_name and deserialize_from for now.

I would still argue this patch should be considered. It's a minimal change that allows better compatibility with the set, at least as far as the core schematics serialize logic is concerned. I actually think embarking on the larger solution is a good long-term goal, but I personally don't have the bandwidth to tackle that right now.

I've made a change to rely on __getitem__ instead of get. This ensures that overriding any public method will not break schematics ability to serialize / deserialize.

Copy link
Contributor

@lkraider lkraider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 from me after these few changes

@@ -1,4 +1,3 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change whitespace formatting in this patch.

@@ -658,4 +684,3 @@ class M(Model):
m = ListType(ModelType('M', required=True), required=True)

M.get_mock_object()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See first comment.


m = M({"items": 1, "values": 1, "get": 1})
m.validate()

Copy link
Contributor

@lkraider lkraider May 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for nested ModelType that override mapping methods too:

def test_nested_model_override_mapping_methods():

    class Nested(Model):
        items, values, get, keys = IntType(), IntType(), IntType(), IntType()

    class Root(Model):
        keys = ModelType(Nested)

    root = Root({"keys": {"items": 1, "values": 1, "get": 1, "keys": 1}})
    root.validate()
    assert root.keys.items == 1

@lkraider
Copy link
Contributor

I put forward a patch on master that fixes the reliance on keys iteration. You can amend the test-cases to include a keys attribute.

@toumorokoshi
Copy link
Author

Sounds good! Addressed all feedback, I believe. Thanks for fixing up the keys behavior as well!

Also just rebased on top of your changes, hope that's ok. not sure how that affects the existing CR.

@lkraider lkraider merged commit e8352d8 into schematics:master May 24, 2017
@lkraider
Copy link
Contributor

Let me know if you have issues with the latest master. I will prepare a 2.0.1 release including this fixes later in the week.

@lkraider
Copy link
Contributor

lkraider commented May 31, 2017

FYI: v2.0.1 was just released to PyPI and contains these fixes.

@toumorokoshi
Copy link
Author

works for me, thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants