Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 19, 2023
1 parent 025b66d commit d305f30
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions test_ncattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ def local_attr(request):
def test_support():
raise ValueError("iris.FUTURE = " + str(iris.FUTURE))


_SKIP_WARNCHECK = "_no_warnings_check"


def check_captured_warnings(
expected_keys: List[str], captured_warnings: List[warnings.WarningMessage]
):
Expand Down Expand Up @@ -483,16 +485,23 @@ def fetch_results(
_MATRIX_ATTRNAMES = _LOCAL_TEST_ATTRS + list(_GLOBAL_TEST_ATTRS) + ["user"]
# remove special-cases, for now
_SPECIAL_ATTRS = [
'Conventions', "ukmo__process_flags", "missing_value", "standard_error_multiplier"
"Conventions",
"ukmo__process_flags",
"missing_value",
"standard_error_multiplier",
]
_MATRIX_ATTRNAMES = [
attr for attr in _MATRIX_ATTRNAMES if not attr in _SPECIAL_ATTRS
]
_MATRIX_ATTRNAMES = [attr for attr in _MATRIX_ATTRNAMES if not attr in _SPECIAL_ATTRS]


#
# A routine to work "backwards" from am attribute name to its "style", i.e. type category.
# Possible ones are "globalstyle", "localstyle", "userstyle".
#
_ATTR_STYLES = ["localstyle", "globalstyle", "userstyle"]


def deduce_attr_style(attrname: str) -> str:
# Extract the attribute "style type" from an attr_param name
if attrname in _LOCAL_TEST_ATTRS:
Expand All @@ -504,31 +513,30 @@ def deduce_attr_style(attrname: str) -> str:
style = "userstyle"
return style


#
# Decode a matrix "input spec" to codes for global + local values.
#


def decode_matrix_input(input_spec):
def decode_specstring(spec: str) -> List[Union[str, None]]:
# Decode an input spec-string to input/output attribute values
assert spec[0] == "G" and spec[2] == "L"
global_val = spec[1]
allvals = spec[1] + spec[3:]
result = [
None if valchar == '-' else valchar
for valchar in allvals
]
result = [None if valchar == "-" else valchar for valchar in allvals]
return result

if isinstance(input_spec, str):
# Single-source spec (one cube or one file)
gA, vA = decode_specstring(input_spec)
result = [[gA, vA ]]
result = [[gA, vA]]
else:
# Dual-source spec (two files, or sets of cubes with common global)
gA, vA = decode_specstring(input_spec[0])
gB, vB = decode_specstring(input_spec[1])
result = [[gA, vA ], [gB, vB]]
result = [[gA, vA], [gB, vB]]

return result

Expand All @@ -539,17 +547,14 @@ def encode_matrix_result(results: List[List[str]]):
if isinstance(results[0], str):
results = [results]
assert all(
all(
val is None or len(val) == 1
for val in vals
)
for vals in results
all(val is None or len(val) == 1 for val in vals) for vals in results
)

def valrep(val):
return "-" if val is None else val

return list(
''.join(['G', valrep(vals[0]), 'L']
+ list(map(valrep, vals[1:])))
"".join(["G", valrep(vals[0]), "L"] + list(map(valrep, vals[1:])))
for vals in results
)

Expand All @@ -559,6 +564,7 @@ def valrep(val):
# We have the technology to save the found results also.
#


@pytest.fixture(autouse=True, scope="session")
def matrix_results():
matrix_filepath = Path(__file__).parent / "_testattrs_matrix_results.json"
Expand All @@ -570,11 +576,11 @@ def matrix_results():
else:
# Initialise empty matrix results content
matrix_results = {}
for testtype in ('load', 'save', 'roundtrip'):
for testtype in ("load", "save", "roundtrip"):
test_specs = matrix_results.setdefault(testtype, {})
for testcase in _MATRIX_TESTCASES:
test_case_spec = test_specs.setdefault(testcase, {})
test_case_spec['input'] = _MATRIX_TESTCASE_INPUTS[testcase]
test_case_spec["input"] = _MATRIX_TESTCASE_INPUTS[testcase]
for attrstyle in _ATTR_STYLES:
test_case_spec[attrstyle] = None # empty

Expand All @@ -585,7 +591,6 @@ def matrix_results():
json.dump(matrix_results, matrix_filepath)



class TestRoundtrip(MixinAttrsTesting):
"""
Test handling of attributes in roundtrip netcdf-iris-netcdf.
Expand Down Expand Up @@ -963,8 +968,8 @@ def test_16_localstyle(self, local_attr, origin_style, do_split):
@pytest.mark.parametrize("attrname", _MATRIX_ATTRNAMES)
def test_matrix(self, testcase, attrname, matrix_results):
do_saves, matrix_results = matrix_results
test_spec = matrix_results['roundtrip'][testcase]
input_spec = test_spec['input']
test_spec = matrix_results["roundtrip"][testcase]
input_spec = test_spec["input"]
values = decode_matrix_input(input_spec)

self.run_roundtrip_testcase(attrname, values)
Expand Down

0 comments on commit d305f30

Please sign in to comment.