Skip to content

Commit

Permalink
fixed linting
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Dec 11, 2024
1 parent d0cf7de commit 0334e1f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
15 changes: 14 additions & 1 deletion tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,27 @@ class TestedType:
class ExpressionString(str):
pass


class ReturnOracle(dict):
pass


class NaturalLanguageMap(dict):
pass


OptionDict = dict[str, int | bool]
YamlObject = (
YamlDict | list | bool | float | int | str | None | ExpressionString | ReturnOracle | NaturalLanguageMap
YamlDict
| list
| bool
| float
| int
| str
| None
| ExpressionString
| ReturnOracle
| NaturalLanguageMap
)


Expand Down Expand Up @@ -139,13 +150,15 @@ def _return_oracle(loader: yaml.Loader, node: yaml.Node) -> ReturnOracle:
), f"A custom oracle must be an object, got {result} which is a {type(result)}."
return ReturnOracle(result)


def _natural_language_map(loader: yaml.Loader, node: yaml.Node) -> NaturalLanguageMap:
result = _parse_yaml_value(loader, node)
assert isinstance(

Check warning on line 156 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L155-L156

Added lines #L155 - L156 were not covered by tests
result, dict
), f"A natural language map must be an object, got {result} which is a {type(result)}."
return NaturalLanguageMap(result)

Check warning on line 159 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L159

Added line #L159 was not covered by tests


def _parse_yaml(yaml_stream: str) -> YamlObject:
"""
Parse a string or stream to YAML.
Expand Down
36 changes: 26 additions & 10 deletions tested/nat_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
from typing import cast

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'cast' is not used.

Check warning on line 2 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L1-L2

Added lines #L1 - L2 were not covered by tests

import yaml
from tested.dsl.translate_parser import _parse_yaml, YamlObject, YamlDict, ReturnOracle, \
ExpressionString, _validate_testcase_combinations, NaturalLanguageMap
from tested.dsl.translate_parser import (

Check warning on line 5 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L4-L5

Added lines #L4 - L5 were not covered by tests
_parse_yaml,
YamlObject,
YamlDict,
ReturnOracle,
ExpressionString,
_validate_testcase_combinations,
NaturalLanguageMap,
)


def translate_testcase(testcase: YamlDict, language: str) -> YamlDict:
Expand Down Expand Up @@ -79,13 +86,16 @@ def translate_testcase(testcase: YamlDict, language: str) -> YamlDict:
if isinstance(description, NaturalLanguageMap):
assert language in description
testcase["description"] = description[language]
elif isinstance(description, dict) and isinstance(description["description"], dict):
elif isinstance(description, dict) and isinstance(

Check warning on line 89 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L86-L89

Added lines #L86 - L89 were not covered by tests
description["description"], dict
):
assert language in description["description"]
description["description"] = description["description"][language]
testcase["description"] = description

Check warning on line 94 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L92-L94

Added lines #L92 - L94 were not covered by tests

return testcase

Check warning on line 96 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L96

Added line #L96 was not covered by tests


def translate_testcases(testcases: list, language: str) -> list:
result = []
for testcase in testcases:
Expand All @@ -94,6 +104,7 @@ def translate_testcases(testcases: list, language: str) -> list:

return result

Check warning on line 105 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L105

Added line #L105 was not covered by tests


def translate_contexts(contexts: list, language: str) -> list:
result = []
for context in contexts:
Expand All @@ -104,6 +115,7 @@ def translate_contexts(contexts: list, language: str) -> list:

return result

Check warning on line 116 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L116

Added line #L116 was not covered by tests


def translate_tab(tab: YamlDict, language: str) -> YamlDict:
key_to_set = "units" if "units" in tab else "tab"
name = tab.get(key_to_set)

Check warning on line 121 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L119-L121

Added lines #L119 - L121 were not covered by tests
Expand Down Expand Up @@ -133,6 +145,7 @@ def translate_tab(tab: YamlDict, language: str) -> YamlDict:
tab["scripts"] = translate_testcases(tab["scripts"], language)
return tab

Check warning on line 146 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L142-L146

Added lines #L142 - L146 were not covered by tests


def translate_tabs(dsl_list: list, language: str) -> list:
result = []
for tab in dsl_list:
Expand All @@ -141,7 +154,8 @@ def translate_tabs(dsl_list: list, language: str) -> list:

return result

Check warning on line 155 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L155

Added line #L155 was not covered by tests

def translate_dsl(dsl_object: YamlObject, language:str) -> YamlObject:

def translate_dsl(dsl_object: YamlObject, language: str) -> YamlObject:
if isinstance(dsl_object, list):
return translate_tabs(dsl_object, language)

Check warning on line 160 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L158-L160

Added lines #L158 - L160 were not covered by tests
else:
Expand All @@ -152,25 +166,28 @@ def translate_dsl(dsl_object: YamlObject, language:str) -> YamlObject:
dsl_object[key_to_set] = translate_tabs(tab_list, language)
return dsl_object

Check warning on line 167 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L162-L167

Added lines #L162 - L167 were not covered by tests

def parse_yaml(yaml_path:str) -> YamlObject:
with open(yaml_path, 'r') as stream:

def parse_yaml(yaml_path: str) -> YamlObject:
with open(yaml_path, "r") as stream:
result = _parse_yaml(stream.read())

Check warning on line 172 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L170-L172

Added lines #L170 - L172 were not covered by tests

return result

Check warning on line 174 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L174

Added line #L174 was not covered by tests


def convert_to_yaml(yaml_object: YamlObject) -> str:
def oracle_representer(dumper, data):
return dumper.represent_mapping('!oracle', data)
return dumper.represent_mapping("!oracle", data)

Check warning on line 179 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L177-L179

Added lines #L177 - L179 were not covered by tests

def expression_representer(dumper, data):
return dumper.represent_scalar('!expression', data)
return dumper.represent_scalar("!expression", data)

Check warning on line 182 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L181-L182

Added lines #L181 - L182 were not covered by tests

# Register the representer for the ReturnOracle object
yaml.add_representer(ReturnOracle, oracle_representer)
yaml.add_representer(ExpressionString, expression_representer)
return yaml.dump(yaml_object, sort_keys=False)

Check warning on line 187 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L185-L187

Added lines #L185 - L187 were not covered by tests

if __name__ == '__main__':

if __name__ == "__main__":
n = len(sys.argv)
assert n > 1, "Expected atleast two argument (path to yaml file and language)."

Check warning on line 192 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L190-L192

Added lines #L190 - L192 were not covered by tests

Expand All @@ -179,4 +196,3 @@ def expression_representer(dumper, data):
new_yaml = parse_yaml(path)
translated_dsl = translate_dsl(new_yaml, lang)
print(convert_to_yaml(translated_dsl))

Check warning on line 198 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L194-L198

Added lines #L194 - L198 were not covered by tests

0 comments on commit 0334e1f

Please sign in to comment.