-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-30971: Improve code readability of json.tool #2720
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
Conversation
@dhimmel, thanks for your PR! By analyzing the history of the files in this pull request, we identified @benjaminp, @tiran and @berkerpeksag to be potential reviewers. |
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 left inline comments for the motivation behind each change.
Lib/json/tool.py
Outdated
@@ -25,22 +25,22 @@ def main(): | |||
help='a JSON file to be validated or pretty-printed') | |||
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), | |||
help='write the output of infile to outfile') | |||
parser.add_argument('--sort-keys', action='store_true', default=False, | |||
help='sort the output of dictionaries alphabetically by key') |
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 line exceeded the PEP8 line length, hence the rephrasing.
Lib/json/tool.py
Outdated
except ValueError as e: | ||
raise SystemExit(e) | ||
|
||
# Output JSON | ||
outfile = options.outfile or sys.stdout |
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.
Move variable definition close to where it's used.
Lib/json/tool.py
Outdated
sort_keys = options.sort_keys | ||
hook = collections.OrderedDict if sort_keys else None |
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.
Remove the heavily indented if-else for readability
Lib/json/tool.py
Outdated
@@ -25,22 +25,22 @@ def main(): | |||
help='a JSON file to be validated or pretty-printed') | |||
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), | |||
help='write the output of infile to outfile') | |||
parser.add_argument('--sort-keys', action='store_true', default=False, |
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.
Remove unnecessary argument.
Lib/json/tool.py
Outdated
options = parser.parse_args() | ||
|
||
# Read input JSON |
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.
Comment for readability.
We generally don't make changes like these unless we are modifying the code for other reasons. |
Lib/json/tool.py
Outdated
except ValueError as e: | ||
raise SystemExit(e) | ||
|
||
# Output JSON | ||
outfile = options.outfile or sys.stdout |
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.
Not sure this change helps, it is easier to read when we process all options in one place, and then use the process options.
It would be nice if we could integrate this in options:
outfile = options.outfile or sys.stdout
Does this work?
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
default=sys.stdout,
help='write the output of infile to outfile')
Lib/json/tool.py
Outdated
parser.add_argument('--sort-keys', action='store_true', default=False, | ||
help='sort the output of dictionaries alphabetically by key') | ||
parser.add_argument('--sort-keys', action='store_true', | ||
help='sort dictionary outputs alphabetically by key') |
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.
... dictionaries output ...?
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.
@nirs I changed it to:
sort dictionaries output alphabetically by key
as per your suggestion. But would it make more sense as possessive:
sort dictionaries' output alphabetically by key
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 would check other modules and use the same style.
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 would phrase this as "sort alphabetically by key when generating dictionary output".
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.
Nice!
Use --sort-keys help message based on the json.tool docs at https://docs.python.org/3.6/library/json.html#basic-usage: > If sort_keys is true, then the output of dictionaries > will be sorted by key.
|
This absolutely needs an issue to discuss whether or not to apply it. We probably won't, as we don't normally accept "prettify" pull requests. |
Thanks @bitdancer for the guidance. I opened |
Refs https://bugs.python.org/msg298692. Code was descriptive without the comments.
I'm closing the pull request in response to the advice received in Specifically from @bitdancer:
And from @berkerpeksag:
Will rebase my other PRs with json.tool enhancements on top of these changes. |
Originall from python#2720 Set default option for infile and outfile as per python#2720 (review) Use --sort-keys help message based on the json.tool docs at https://docs.python.org/3.6/library/json.html#basic-usage: Remove commens as per https://bugs.python.org/msg298692. Code was descriptive without the comments.
Several months ago I worked on pull requests to add new options to the
json.tool
command line utility. These pull requests are still open, pending further design discussion and consensus on whether to add them:bpo-29636
).bpo-27413
).Related, #346 fixed a small testing bug in
json.tool
and was merged.Since #345 and #201 have been inactive for some time, I wanted to take the non-controversial code changes from those PRs. This PR contains readability improvements and minor refactoring to
json.tool
. The goal is to get some of the previous work merged and make thejson.tool
source easier to maintain and enhance going forward. Admittedly, the changes are quite minor.