Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_contained_json(self) -> YieldFlake8Error:
actual_fmt = JSONFormat(path=self.file_path)
expected = {}
# TODO: accept key as a jmespath expression, value is valid JSON
for key, json_string in (self.file_dict.get(KEY_CONTAINS_JSON) or {}).items():
try:
expected[key] = json.loads(json_string)
except json.JSONDecodeError as err:
# This should not happen, because the style was already validated before.
# Maybe the NIP??? code was disabled by the user?
LOGGER.error("%s on %s while checking %s", err, KEY_CONTAINS_JSON, self.file_path)
continue
yield from self.warn_missing_different(
JSONFormat(data=actual_fmt.as_data).compare_with_dictdiffer(expected, unflatten)
)
def get_suggested_json(self, raw_actual: JsonDict = None) -> JsonDict:
"""Return the suggested JSON based on actual values."""
actual = set(flatten(raw_actual).keys()) if raw_actual else set()
expected = set(self.file_dict.get(KEY_CONTAINS_KEYS) or [])
# TODO: include "contains_json" keys in the suggestion as well
missing = expected - actual
if not missing:
return {}
return SortedDict(unflatten({key: self.SOME_VALUE_PLACEHOLDER for key in missing}))
def compare_with_flatten(self, expected: Union[JsonDict, "BaseFormat"] = None) -> Comparison:
"""Compare two flattened dictionaries and compute missing and different items."""
comparison = self._create_comparison(expected)
if comparison.flat_expected.items() <= comparison.flat_actual.items():
return comparison
comparison.set_missing(
unflatten({k: v for k, v in comparison.flat_expected.items() if k not in comparison.flat_actual})
)
comparison.set_diff(
unflatten(
{
k: v
for k, v in comparison.flat_expected.items()
if k in comparison.flat_actual and comparison.flat_expected[k] != comparison.flat_actual[k]
}
)
)
return comparison
def update_pair(self, key, raw_expected):
"""Update a key on one of the comparison dicts, with its raw expected value."""
if isinstance(key, str):
self.diff_dict.update({key: raw_expected})
return
if isinstance(key, list):
if len(key) == 4 and isinstance(key[3], int):
_, _, new_key, index = key
if new_key not in self.diff_dict:
self.diff_dict[new_key] = []
self.diff_dict[new_key].insert(index, raw_expected)
return
if len(key) == 1:
self.diff_dict.update(unflatten({key[0]: raw_expected}))
return
LOGGER.warning("Err... this is unexpected, please open an issue: key=%s raw_expected=%s", key, raw_expected)
def compare_with_flatten(self, expected: Union[JsonDict, "BaseFormat"] = None) -> Comparison:
"""Compare two flattened dictionaries and compute missing and different items."""
comparison = self._create_comparison(expected)
if comparison.flat_expected.items() <= comparison.flat_actual.items():
return comparison
comparison.set_missing(
unflatten({k: v for k, v in comparison.flat_expected.items() if k not in comparison.flat_actual})
)
comparison.set_diff(
unflatten(
{
k: v
for k, v in comparison.flat_expected.items()
if k in comparison.flat_actual and comparison.flat_expected[k] != comparison.flat_actual[k]
}
)
)
return comparison