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

Do not break loading incompatible cache (#875) #1034

Merged
merged 1 commit into from
Oct 21, 2019
Merged

Do not break loading incompatible cache (#875) #1034

merged 1 commit into from
Oct 21, 2019

Conversation

vaneseltine
Copy link
Contributor

A black cache created in Python 3.8 throws a ValueError in earlier versions;
this adds a test for the issue and adjusts read_cache to handle it.

Copy link

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I suggest to remove also protocol=pickle.HIGHEST_PROTOCOL in write_cache().

@vaneseltine
Copy link
Contributor Author

I suggest to remove also protocol=pickle.HIGHEST_PROTOCOL in write_cache().

OK, good. This will prevent the incompatibility to begin with.

Copy link

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

DEFAULT_PROTOCOL is default, so the protocol argument can be omitted.

@vaneseltine
Copy link
Contributor Author

True, but leaving an explicit argument indicates that using the protocol has been considered and choosing the default is deliberate.

But if that doesn't sway you, of course I can drop it. 😄

@JelleZijlstra
Copy link
Collaborator

Would it be better if we include the pickle protocol number in the cache file name? That way it's safe to switch back and forth between different protocols.

@vaneseltine
Copy link
Contributor Author

vaneseltine commented Oct 2, 2019

After poking through pickle documentation and PEP 3154, my conclusion is that black should pick its own default.

The entire purpose of having numbered protocols is to provide for backwards compatibility. Protocol 4 was added in 3.4, and so it is included in all versions that Black supports. Therefore, Black should use protocol=4 (until it is superseded in all supported versions by 5; i.e. until Black supports no version lower than 3.8).

mode = black.FileMode()
from shutil import copyfile

src = THIS_DIR / "data" / "cache-38.pickle"
Copy link

Choose a reason for hiding this comment

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

Let me guess this based on file name:

  • this is legit py3.8 pickle file
  • test is assumed to be ran on py3.7

Would it be better to cook up a pickle file with some crazy protocol number instead?

Copy link
Contributor Author

@vaneseltine vaneseltine Oct 6, 2019

Choose a reason for hiding this comment

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

Correct. And yes; well; it's currently run on 3.6 and 3.7 during CI checks.

But you're right that an improbable pickle protocol number would be better, since this test could become obsolete. I didn't get at all into pickle internals to set this up (just pickled a tiny black cache in 3.8).

@@ -64,6 +64,7 @@ if sys.version_info >= (3, 5):
AWAIT: int
ASYNC: int
ERRORTOKEN: int
COLONEQUAL: int
Copy link

Choose a reason for hiding this comment

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

Is this perhaps an unrelated change? If so it could be submitted separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hrm, that was a mistake. Must have snuck in from my attempt to update the branch to current.

@ambv
Copy link
Collaborator

ambv commented Oct 20, 2019

Drop the test (it's rather brittle) and the unrelated file change so we can merge this. Thank you for working on this!

Copy link
Collaborator

@ambv ambv left a comment

Choose a reason for hiding this comment

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

Marking as "request changes" so I can filter through the PRs easier.

A black cache created in Python 3.8 throws an unhandled
ValueError in earlier versions. This is because 3.6 does
not recognize the pickle protocol used as default in 3.8.
Accordingly, this commit:

  - Fixes read_cache to return an empty cache instead.

  - Changes the pickle protocol to 4 as the highest protocol
    fully supported by black's supported Python versions.
@vaneseltine
Copy link
Contributor Author

Drop the test (it's rather brittle) and the unrelated file change so we can merge this. Thank you for working on this!

Okay, I've updated to drop the test and properly update my pull request handling -- this should be good to go now.

@ambv ambv merged commit 03766f5 into psf:master Oct 21, 2019
@ambv
Copy link
Collaborator

ambv commented Oct 21, 2019

Thank you, Matt! This makes Black better for everybody.

@gaborbernat
Copy link
Contributor

@ambv thanks!!! is there a release planned with this? 🙇

@ambv
Copy link
Collaborator

ambv commented Oct 21, 2019

No, we'll keep it on master forever. 🤓

Yes, we are planning a release relatively soon now. Please test master at Bloomberg and report any deal breakers before we cut the release this week.

@gaborbernat
Copy link
Contributor

Did a quick rundown of 8-10 projects of the master... Seems to be running mostly just fine. It does generate some changes though, mostly related to either tuple unwrapping (which has now always parenthesis now on the left side) and sometimes remove parenthesis when comments fit in.

-        expected_txt = (
-            "def fun(a1: str, *args: str, kwonly1: int = None, **kwargs) -> None: ...\n"
-        )  # noqa
+        expected_txt = "def fun(a1: str, *args: str, kwonly1: int = None, **kwargs) -> None: ...\n"  # noqa
-        msg, = excinfo.value.args
+        (msg,) = excinfo.value.arg

I get these are expected though.

@serhiy-storchaka
Copy link

Argh, it ruined the ,= operator!

@hugovk
Copy link
Contributor

hugovk commented Oct 21, 2019

-        msg, = excinfo.value.args
+        (msg,) = excinfo.value.arg

I get these are expected though.

That one looks like #1041.

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.

7 participants