-
-
Notifications
You must be signed in to change notification settings - Fork 709
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
Refactor/pretty format json #254
Conversation
There was a problem hiding this 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', |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah intentional :)
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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)', |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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).
btw I thought I had run the tests before creating the PR, sorry about that! |
6e4d4c6
to
e472253
Compare
* 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.
e472253
to
5b6ddaf
Compare
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... |
ah yeah, I've hit that json bug before, fun fun! |
Thanks again! |
Two commits to fix pretty_format_json:
Remove pretty_format_json simplejson dependency
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.