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

Use dict instead of OrderedDict (Python >= 3.6) #61

Merged
merged 2 commits into from
Sep 14, 2021
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
68 changes: 31 additions & 37 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import numbers
import os
import re
from collections import OrderedDict
from tempfile import NamedTemporaryFile
from urllib.parse import urljoin, urlparse, urlsplit
from urllib.request import urlopen
Expand Down Expand Up @@ -331,7 +330,7 @@ def pkg_schema_str(self):

@property
def _schema_obj(self):
return json.loads(self.schema_str, object_pairs_hook=OrderedDict)
return json.loads(self.schema_str)

@property
def _pkg_schema_obj(self):
Expand Down Expand Up @@ -457,7 +456,7 @@ def load_core_codelists(codelist_url, unique_files, config=None):
@functools.lru_cache()
def _deref_schema(schema_str, schema_host, cache=None):
loader = CustomJsonrefLoader(schema_url=schema_host, cache=cache)
deref_obj = jsonref.loads(schema_str, loader=loader, object_pairs_hook=OrderedDict)
deref_obj = jsonref.loads(schema_str, loader=loader)
# Force evaluation of jsonref.loads here
repr(deref_obj)
return deref_obj
Expand Down Expand Up @@ -688,7 +687,7 @@ def get_additional_codelist_values(schema_obj, json_data):
def get_additional_fields_info(json_data, schema_fields, context, fields_regex=False):
fields_present = get_fields_present_with_examples(json_data)

additional_fields = collections.OrderedDict()
additional_fields = {}
root_additional_fields = set()

for field, field_info in fields_present.items():
Expand All @@ -707,7 +706,7 @@ def get_additional_fields_info(json_data, schema_fields, context, fields_regex=F
break
else:
field_info["root_additional_field"] = True
field_info["additional_field_descendance"] = collections.OrderedDict()
field_info["additional_field_descendance"] = {}
root_additional_fields.add(field)

field_info["path"] = "/".join(field.split("/")[:-1])
Expand Down Expand Up @@ -887,34 +886,29 @@ def get_schema_validation_errors(
if header_extra is None:
header_extra = header

unique_validator_key = OrderedDict(
[
("message", message),
("validator", e.validator),
("assumption", e.assumption if hasattr(e, "assumption") else None),
# Don't pass this value for 'enum' and 'required' validators,
# because it is not needed, and it will mean less grouping, which
# we don't want.
(
"validator_value",
e.validator_value
if e.validator not in ["enum", "required"]
else None,
),
("message_type", validator_type),
("path_no_number", path_no_number),
("header", header),
("header_extra", header_extra),
("null_clause", null_clause),
("error_id", e.error_id if hasattr(e, "error_id") else None),
("exclusiveMinimum", e.schema.get("exclusiveMinimum")),
("exclusiveMaximum", e.schema.get("exclusiveMaximum")),
("extras", getattr(e, "extras", None)),
("each", getattr(e, "each", None)),
("property", getattr(e, "property", None)),
("reprs", getattr(e, "reprs", None)),
]
)
unique_validator_key = {
"message": message,
"validator": e.validator,
"assumption": e.assumption if hasattr(e, "assumption") else None,
# Don't pass this value for 'enum' and 'required' validators,
# because it is not needed, and it will mean less grouping, which
# we don't want.
"validator_value": e.validator_value
if e.validator not in ["enum", "required"]
else None,
"message_type": validator_type,
"path_no_number": path_no_number,
"header": header,
"header_extra": header_extra,
"null_clause": null_clause,
"error_id": e.error_id if hasattr(e, "error_id") else None,
"exclusiveMinimum": e.schema.get("exclusiveMinimum"),
"exclusiveMaximum": e.schema.get("exclusiveMaximum"),
"extras": getattr(e, "extras", None),
"each": getattr(e, "each", None),
"property": getattr(e, "property", None),
"reprs": getattr(e, "reprs", None),
}
if instance is not None:
unique_validator_key["instance"] = instance
validation_errors[
Expand Down Expand Up @@ -986,10 +980,10 @@ def get_json_data_deprecated_fields(json_data_paths, schema_obj):
deprecated_json_data_paths = [
path for path in deprecated_schema_paths if path[0] in json_data_paths
]
# Generate an OrderedDict sorted by deprecated field names (keys) mapping
# Generate a dict sorted by deprecated field names (keys) mapping
# to a unordered tuple of tuples:
# {deprecated_field: ((path, path... ), (version, description))}
deprecated_fields = OrderedDict()
deprecated_fields = {}
for generic_path in sorted(deprecated_json_data_paths, key=lambda tup: tup[0][-1]):
deprecated_fields[generic_path[0][-1]] = tuple()

Expand All @@ -1008,7 +1002,7 @@ def get_json_data_deprecated_fields(json_data_paths, schema_obj):
)

# Order the path tuples in values for deprecated_fields.
deprecated_fields_output = OrderedDict()
deprecated_fields_output = {}
for field, paths in deprecated_fields.items():
sorted_paths = tuple(sorted(paths[0]))

Expand Down Expand Up @@ -1068,7 +1062,7 @@ def _generate_data_path(json_data, path=()):


def get_fields_present_with_examples(*args, **kwargs):
counter = collections.OrderedDict()
counter = {}
for key, value in fields_present_generator(*args, **kwargs):
if key not in counter:
counter[key] = {"count": 1, "examples": []}
Expand Down
33 changes: 12 additions & 21 deletions tests/lib/test_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
from collections import OrderedDict
from datetime import datetime
from decimal import Decimal
from unittest import mock
Expand Down Expand Up @@ -211,27 +210,19 @@ def test_get_json_data_deprecated_fields():
deprecated_data_fields = get_json_data_deprecated_fields(
json_data_paths, schema_obj
)
expected_result = OrderedDict(
[
(
"initiationType",
{
"paths": ("releases/0", "releases/1"),
"explanation": (
"1.1",
"Not a useful field as always has to be tender",
),
},
),
(
"quantity",
{
"paths": ("releases/0/tender/items/0",),
"explanation": ("1.1", "Nobody cares about quantities"),
},
expected_result = {
"initiationType": {
"paths": ("releases/0", "releases/1"),
"explanation": (
"1.1",
"Not a useful field as always has to be tender",
),
]
)
},
"quantity": {
"paths": ("releases/0/tender/items/0",),
"explanation": ("1.1", "Nobody cares about quantities"),
},
}
for field_name in expected_result.keys():
assert field_name in deprecated_data_fields
assert (
Expand Down