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

Support fingerprinting of UnsetBool options. #4665

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/python/pants/option/options_fingerprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import inspect
import json
import os
from hashlib import sha1

from pants.base.build_environment import get_buildroot
from pants.option.custom_types import dict_with_files_option, file_option, target_option
from pants.option.custom_types import UnsetBool, dict_with_files_option, file_option, target_option


class Encoder(json.JSONEncoder):
_UNSET_BOOL_ENCODING = '__type::{}::{}'.format(inspect.getmodule(UnsetBool), UnsetBool.__name__)
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

Why is the inspect magic needed? Couldn't this just be UnsetBool.__module__, or even a hard-coded string? I mean, apart from readability concerns _UNSET_BOOL_ENCODING can be literally any unlikely-to-collide string, if I understand this correctly?

Copy link
Contributor Author

@jsirois jsirois Jun 9, 2017

Choose a reason for hiding this comment

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

It could be anything - eye of beholder I guess. I look at __module__, __package__ and throw up my hands as to which will be available when. I figure inspect gets this right robustly.

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

Well, as long as it's not a performance liability, which it probably isn't. My suspicion of introspection is a JVM holdover, in Python introspection is part of the bargain, I guess.

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

Do we actually care what module this is in though? Would we want to invalidate if we renamed the module?

Copy link
Contributor Author

@jsirois jsirois Jun 9, 2017

Choose a reason for hiding this comment

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

Well, as long as it's not a performance liability ....

Its a class constant, so no real concern

Do we actually care what module this is in though?

No, just that it doesn't collide with other strings. Open to a concrete suggestion on what value makes sense.

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

I think '_UNSET_BOOL_ENCODING' as the value makes sense?

Sorry to harp on this, I just imagine (or another casual reader) looking at this in the future and wondering why we want to invalidate options fingerprints if we change the name or location of an implementation detail. A constant literal string is fairly idiomatic for "symbolic constant value whose actual content isn't significant, only its uniqueness".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good - unlikely to collide.


def default(self, o):
if o is UnsetBool:
return self._UNSET_BOOL_ENCODING
return super(Encoder, self).default(o)


def stable_json_dumps(obj):
return json.dumps(obj, ensure_ascii=True, allow_nan=False, sort_keys=True)
return json.dumps(obj, ensure_ascii=True, allow_nan=False, sort_keys=True, cls=Encoder)


def stable_json_sha1(obj):
Expand Down
8 changes: 7 additions & 1 deletion tests/python/pants_test/option/test_options_fingerprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from pants.base.payload import Payload
from pants.base.payload_field import PrimitiveField
from pants.option.custom_types import dict_option, file_option, list_option, target_option
from pants.option.custom_types import (UnsetBool, dict_option, file_option, list_option,
target_option)
from pants.option.options_fingerprinter import OptionsFingerprinter
from pants.util.contextutil import temporary_dir
from pants_test.base_test import BaseTest
Expand Down Expand Up @@ -95,3 +96,8 @@ def test_fingerprint_file_list(self):
def test_fingerprint_primitive(self):
fp1, fp2 = (self.options_fingerprinter.fingerprint('', v) for v in ('foo', 5))
self.assertNotEquals(fp1, fp2)

def test_fingerprint_unset_bool(self):
fp1 = self.options_fingerprinter.fingerprint(UnsetBool, UnsetBool)
fp2 = self.options_fingerprinter.fingerprint(UnsetBool, UnsetBool)
self.assertEqual(fp1, fp2)