Skip to content

Comments

add handling of unknown types to JSON stream#398

Closed
peendebak wants to merge 5 commits intomicrosoft:masterfrom
VandersypenQutech:fix/json
Closed

add handling of unknown types to JSON stream#398
peendebak wants to merge 5 commits intomicrosoft:masterfrom
VandersypenQutech:fix/json

Conversation

@peendebak
Copy link
Contributor

# we cannot convert the object to JSON, just take a string
s = str(obj)
finally:
return s
Copy link
Contributor

Choose a reason for hiding this comment

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

You've mixed control patterns... either use:

try:
    return super(...)
except TypeError:
    return str(...)

or:

try:
    s = super(...)
except TypeError:
    s = str(...)
# shouldn't go in a finally because you're avoiding the error
# ie if there were any *other* error, then the finally itself would
# error out because s would not be defined!
return s

Copy link
Contributor

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

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

I'm OK with this (after addressing my comment), does seem like it's better not to fail in this case, though I'd still much prefer that people convert their data structures to JSON when they enter them into the metadata, rather than asking our encoder to do it - that way metadata is consistent when first acquired and when read back in from disk.

@peendebak
Copy link
Contributor Author

@alexcjohnson Added the fix as suggested.

@alexcjohnson
Copy link
Contributor

Removing the finally was part of the suggested change (and the most important part, as it could lead to confusing errors). The other part is handling the two branches symmetrically. Right now try returns, but except sets s, which gets returned in the outer scope. Not a huge deal (so 💃 ), but really they should either both return or both set s for a later return.

Incidentally, we don't seem to have any tests for NumpyJSONEncoder do we? It's probably largely covered by way of metadata tests, but if you wanted to write a short test for it in test_helpers.py you would have my undying gratitude 🍻

Copy link
Contributor

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

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

Remaining comments are nonblocking.

@peendebak
Copy link
Contributor Author

@alexcjohnson @giulioungaretti Added a basic test

@peendebak
Copy link
Contributor Author

@giulioungaretti @alexcjohnson Even tough the 💃 is there, I cannot merge myself any more

Copy link
Contributor

@giulioungaretti giulioungaretti left a comment

Choose a reason for hiding this comment

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

@eendebakpt yes, only core can merge.

Also auto pep8 introduced a lot of wrong empty lines.
In helpers.py L18 and 22, 262 and in basically every function in validators.py

@giulioungaretti
Copy link
Contributor

@eendebakpt thanks for the PR, will prettyify the tests and merge!

@giulioungaretti giulioungaretti mentioned this pull request Nov 22, 2016
@peendebak peendebak deleted the fix/json branch October 19, 2017 13:21
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.

4 participants