Skip to content

CLN: use f-string for JSON related files #30430

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

Merged
merged 14 commits into from
Dec 24, 2019

Conversation

jyscao
Copy link
Contributor

@jyscao jyscao commented Dec 23, 2019

@alimcmaster1 alimcmaster1 added Code Style Code style, linting, code_checks Clean labels Dec 23, 2019
@jyscao jyscao changed the title Json f string CLN: use f-string for JSON related files Dec 23, 2019
Copy link
Member

@alimcmaster1 alimcmaster1 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! - few minor comments

"JSON data had unexpected key(s): {bad_keys}".format(
bad_keys=pprint_thing(bad_keys)
)
f"JSON data had unexpected key(s): {pprint_thing(bad_keys)}"
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if pprint_thing is still needed - can you check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm unfamiliar with the specifics of pprint_thing and where and when it should be used. Could you let me know why you suspect it might no longer be needed, and I'll investigate.

Copy link
Member

Choose a reason for hiding this comment

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

pprint_thing is partially left over from py2/py3 compat, but partially for pretty-pretting nested objects. In tthis case, bad_keys is defined a couple lines up and is a str, so pprint_thing is definitely not needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification. I removed both the import and the call to pprint_thing in that module

f"{spaces}}},\n"
f'{spaces}"b":{{\n'
f'{spaces}{spaces}"0":"bar",\n'
f'{spaces}{spaces}"1":"qux"\n'
Copy link
Member

Choose a reason for hiding this comment

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

Can we just use double quotes opposed to mixing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the outer quotes all use " now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I remember, the quotes actually became mixed as a result of running black pandas. I'd originally used ' for the outer quotes on each line. But black seems to prefer " as the outer when there are no quote literals inside the quote.

Changing all to outer quotes to " and using ' as the inner is even more problematic, as that causes the test to fail, thus I've changed it back to using ' as the outer quote for the time being.

Running black again as the CI is prompting me to do will once again result in mixed quotes. So please let me know what's the best practise here: consistent quoting or black-style?

Copy link
Member

Choose a reason for hiding this comment

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

Just keep the original triple quoted string as is and prefix with an f

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I changed it back

@@ -328,8 +328,7 @@ def _recursive_extract(data, path, seen_meta, level=0):

if k in result:
raise ValueError(
"Conflicting metadata name {name}, "
"need distinguishing prefix ".format(name=k)
f"Conflicting metadata name {k}, " "need distinguishing prefix "
Copy link
Member

Choose a reason for hiding this comment

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

@jyscao an extra pair of parentheses got in here and needs to be removed. this sometimes happens when running black

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By parentheses, I'm guessing you mean extra pair of "? If that's the case, they have been removed.

@@ -100,7 +100,7 @@ def assert_frame_equal(self, left, right, *args, **kwargs):
check_names=kwargs.get("check_names", True),
check_exact=kwargs.get("check_exact", False),
check_categorical=kwargs.get("check_categorical", True),
obj="{obj}.columns".format(obj=kwargs.get("obj", "DataFrame")),
obj=f"{kwargs.get('obj', 'DataFrame')}.columns",
Copy link
Member

Choose a reason for hiding this comment

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

can you take the kwargs.get out to a separate line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since keyword arguments are passed to tm.assert_index_equal, I didn't really know where best to put the new line for the kwargs.get call, so for the time being I put it before the call to tm.assert_index_equal. Also I'm not too sure if obj_type is a good binding name for this value. Please review this change.

default_handler=str
) == '{{"A":{{"0":"{hex}"}},"B":{{"0":1}}}}'.format(hex=hexed)
assert (
df_nonprintable.to_json(default_handler=str) == f'{{"A":{{"0":"{hexed}"}}}}'
Copy link
Member

Choose a reason for hiding this comment

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

pls split into lines:

result = df_nonprintable...
expected = f'...'
assert result == expected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I also split the test below this one into 3 lines.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

@WillAyd WillAyd added this to the 1.0 milestone Dec 24, 2019
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm.

@jbrockmendel jbrockmendel merged commit c74de79 into pandas-dev:master Dec 24, 2019
@jbrockmendel
Copy link
Member

Thanks @jyscao

AlexKirko pushed a commit to AlexKirko/pandas that referenced this pull request Dec 29, 2019
* CLN: use f-string for JSON related files

* Apply black style

* Missed one...

* Use double-quotes for expected in test_to_json_indent

* Add the f in f-string

* Use correct multiplier (100k not 10) for "bar"

* Add back missing closing paren

* Use single quote as the outer quotes

* Remove pprint_thing usage and import

* Keep use f""" for expected

* Split two tests into 3 lines each

* Remove paren around string

* Remove extra pair "" in string

* Move kwargs.get onto own line
@jyscao jyscao deleted the json_f-string branch June 23, 2020 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Code Style Code style, linting, code_checks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants