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

Refactor/pretty format json #254

Merged
merged 3 commits into from
Dec 11, 2017

Conversation

cas--
Copy link

@cas-- cas-- commented Dec 10, 2017

Two commits to fix pretty_format_json:

Remove pretty_format_json simplejson dependency

  • The simplejson module is only needed for <=py25 so replace with builtin json.
  • Replace six dependecy for simple Py2 check for convertion to unicode.
  • Cleanup quotes.

Fix pretty_format_json to use int indent

The indent parameter for json should be integer and under Python2 is
will raise an error if not. So switch from str to int and mention
default value in help text.

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

A few little things below, overall I'm + on removing dependence on simplejson

setup.py Outdated
@@ -28,8 +28,6 @@
'flake8!=2.5.3',
'autopep8>=1.3',
'pyyaml',
'simplejson',
'six',
Copy link
Member

Choose a reason for hiding this comment

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

six is used elsewhere so it can't be removed

Copy link
Author

Choose a reason for hiding this comment

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

I missed that other usage, that can be easily resolved too. :)

Copy link
Member

Choose a reason for hiding this comment

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

tbh, until we're comfortable dropping python 2 entirely, keeping six around seems ok to me -- I'd certainly rather see from six.moves import ... than try: except: for imports

Copy link
Author

@cas-- cas-- Dec 10, 2017

Choose a reason for hiding this comment

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

For a single import to require six, I'd disagree as it creates an extra level of abstraction to a 3rd party module and a docs lookup for usage. With a try..except you know exactly what is going on. Both will have to be modified in future regardless though I suppose it'd then be easier to find the six module imports...

Anyway, it's not a big deal :)


if args.autofix:
_autofix(json_file, pretty_contents)

status = 1

except simplejson.JSONDecodeError:
except json.JSONDecodeError:
Copy link
Member

Choose a reason for hiding this comment

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

there's no json.JSONDecodeError, I believe stdlib throws ValueError?

Copy link
Author

Choose a reason for hiding this comment

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

Ah it does exist on Py3 but not Py2... I'll sort this 👍

# Ensure unicode under Py2
if sys.version_info.major == 2:
json_pretty = json_pretty.decode()
json_pretty += '\n' # dumps do not end with a newline
Copy link
Member

Choose a reason for hiding this comment

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

the comment was changed here, though I think the lhs has better grammar

Copy link
Author

Choose a reason for hiding this comment

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

Yeah intentional :)

Copy link
Member

Choose a reason for hiding this comment

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

I guess it makes more sense to me that dumps is a singular noun ("dump string")

)) + "\n" # dumps does not end with a newline
)
# Ensure unicode under Py2
if sys.version_info.major == 2:
Copy link
Member

Choose a reason for hiding this comment

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

will need a no cover comment to satisfy coverage.

Since six is still used, probably just use six.PY2 here

default=' ',
help='String used as delimiter for one indentation level',
default=2,
help='String used as delimiter for one indentation level (Default: 2)',
Copy link
Member

Choose a reason for hiding this comment

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

this shouldn't be refactored -- it allows for the following inputs:

  • '\t'
  • ' '
  • '4'

See #120 which is essentially the opposite of this patch :)

Copy link
Author

Choose a reason for hiding this comment

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

Hmm ok, strings break Py2 json. Suggestions? Keep simplejson and have json as fallback?

There is no need for that function tbh since both simplejson and py3 json accept both str and int and do the conversion...

Copy link
Author

@cas-- cas-- Dec 10, 2017

Choose a reason for hiding this comment

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

'' and '4' are still value and coverted to 0 and 4 ints.

Copy link
Author

@cas-- cas-- Dec 10, 2017

Choose a reason for hiding this comment

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

Ok what I suggest is to leave the default as an int (because that is the original implementation of indent), pass the values str or int to json and catch any exceptions.

Copy link
Member

Choose a reason for hiding this comment

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

>>> '\t'.strip()
''

hmmm seems to do what I expect :)

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see you edited, disregard :D

Copy link
Author

Choose a reason for hiding this comment

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

Yeah I was testing with strip(' ') not strip() 😱

I think I have the solution for all this and will update...

Copy link
Member

Choose a reason for hiding this comment

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

hmmm yes, a bit of a predicament then...

I guess we could just document that \t is invalid for python2 and if that feature is desired use python 3? The tests'll need some adjustment to show this behaviour (and the README will need an update).

@cas--
Copy link
Author

cas-- commented Dec 10, 2017

btw I thought I had run the tests before creating the PR, sorry about that!

@cas-- cas-- force-pushed the refactor/pretty-format-json branch 2 times, most recently from 6e4d4c6 to e472253 Compare December 10, 2017 22:19
Calum Lind added 2 commits December 10, 2017 22:33
 * The simplejson module is only needed for <=py25 so replace with builtin json.
 * Replace six dependecy for simple Py2 check for convertion to unicode.
 * Cleanup quotes.
The indent parameter for json should be integer and under Python2 is
will raise an error if not. So switch from str to int and mention
default value in help text.
@cas-- cas-- force-pushed the refactor/pretty-format-json branch from e472253 to 5b6ddaf Compare December 10, 2017 22:33
@cas--
Copy link
Author

cas-- commented Dec 10, 2017

This was a fun project... much head scratching with failing tests, found a bug in object_pairs_hook for py2.7 and 3.4: https://bugs.python.org/issue16333

Anyway all sorted, I think...

@asottile
Copy link
Member

ah yeah, I've hit that json bug before, fun fun!

@asottile asottile merged commit cf04ab0 into pre-commit:master Dec 11, 2017
@asottile
Copy link
Member

Thanks again!

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

Successfully merging this pull request may close these issues.

2 participants