Skip to content

Commit

Permalink
Fix formatting & type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Apr 12, 2024
1 parent b1f7b39 commit 49dfcfc
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ jobs:
- run: echo "$(pipenv --venv)/bin" >> $GITHUB_PATH
- uses: jakebailey/pyright-action@v1
with:
version: '1.1.316'
version: '1.1.354'
warnings: true
working-directory: tested/
2 changes: 1 addition & 1 deletion tested/descriptions/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _construct_datatype(
locale=bundle.config.natural_language,
language=bundle.language,
type_=enum_type,
others=others,
others=others, # type: ignore
)


Expand Down
1 change: 1 addition & 0 deletions tested/dodona.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import json
import typing
from enum import StrEnum, auto, unique
from typing import IO, Literal, Union

Expand Down
4 changes: 2 additions & 2 deletions tested/dsl/ast_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _is_and_get_allowed_empty(node: ast.Call) -> Value | None:
Returns the empty value if allowed, otherwise None.
"""
assert isinstance(node.func, ast.Name)
type_ = get_converter().structure(node.func.id, AllTypes)
type_ = get_converter().structure(node.func.id, AllTypes) # type: ignore
if isinstance(type_, AdvancedSequenceTypes):
return SequenceType(type=cast(AdvancedSequenceTypes, type_), data=[])
elif isinstance(type_, BasicSequenceTypes):
Expand Down Expand Up @@ -223,7 +223,7 @@ def _convert_expression(node: ast.expr, is_return: bool) -> Expression:
raise InvalidDslError(
"The argument of a cast function must resolve to a value."
)
return evolve(value, type=get_converter().structure(node.func.id, AllTypes))
return evolve(value, type=get_converter().structure(node.func.id, AllTypes)) # type: ignore
elif isinstance(node, ast.Call):
if is_return:
raise InvalidDslError(
Expand Down
2 changes: 1 addition & 1 deletion tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _validate_dsl(dsl_object: YamlObject):


def _tested_type_to_value(tested_type: TestedType) -> Value:
type_enum = get_converter().structure(tested_type.type, AllTypes)
type_enum = get_converter().structure(tested_type.type, AllTypes) # type: ignore
if isinstance(type_enum, NumericTypes):
# Some special cases for advanced numeric types.
if type_enum == AdvancedNumericTypes.FIXED_PRECISION:
Expand Down
12 changes: 7 additions & 5 deletions tested/judge/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@ def execute_unit(
main_file_name = unit.name
argument = None

executable, status = bundle.language.find_main_file(files, main_file_name)
_logger.debug(f"Found main file: {executable}")
executable_or_status = bundle.language.find_main_file(files, main_file_name)
_logger.debug(f"Found main file or not: {executable_or_status}")

if status != Status.CORRECT:
return None, status
if not isinstance(executable_or_status, Path):
return None, executable_or_status

executable = executable_or_status

files.remove(executable)
stdin = unit.get_stdin(bundle.config.resources)
Expand Down Expand Up @@ -269,4 +271,4 @@ def execute_unit(
memory=base_result.memory,
)

return result, status
return result, Status.CORRECT
23 changes: 8 additions & 15 deletions tested/languages/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def execution(self, cwd: Path, file: str, arguments: list[str]) -> Command:
"""
raise NotImplementedError

def get_string_quote(self):
def get_string_quote(self) -> str:
"""
:return: The symbol used to quote strings.
"""
Expand Down Expand Up @@ -348,37 +348,30 @@ def filter_function(file: Path) -> bool:

return list(x for x in files if filter_function(x))

@typing.overload
def find_main_file(
self,
files: list[Path],
name: str,
) -> tuple[Path, typing.Literal[Status.CORRECT]]: ...

@typing.overload
def find_main_file(self, files: list[Path], name: str) -> tuple[None, Status]: ...

def find_main_file(
self,
files: list[Path],
name: str,
) -> tuple[Path | None, Status]:
) -> Path | Status:
"""
Find the "main" file in a list of files.
This method is invoked to find the "main" file, i.e. the file with the main
method (or at least the file that should be executed).
If the "status" is correct, the main file is returned. Otherwise, an error
status is returned instead.
:param files: A list of files.
:param name: The name of the main file.
:return: The main file or a list of messages.
:return: The main file or the error status.
"""
_logger.debug("Finding %s in %s", name, files)
possible_main_files = [x for x in files if x.name.startswith(name)]
if possible_main_files:
return possible_main_files[0], Status.CORRECT
return possible_main_files[0]
else:
return None, Status.COMPILATION_ERROR
return Status.COMPILATION_ERROR

def cleanup_stacktrace(self, stacktrace: str) -> str:
"""
Expand Down
10 changes: 6 additions & 4 deletions tested/languages/csharp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,20 @@ def execution(self, cwd: Path, file: str, arguments: list[str]) -> Command:
return ["dotnet", file, *arguments]

def find_main_file(
self, files: list[Path], name: str
) -> tuple[Path | None, Status]:
self,
files: list[Path],
name: str,
) -> Path | Status:
# TODO: specify the extension (if any) of the output files, so we don't need to
# override this.
logger.debug("Finding %s in %s", name, files)
possible_main_files = [
x for x in files if x.name.startswith(name) and x.suffix == ".dll"
]
if possible_main_files:
return possible_main_files[0], Status.CORRECT
return possible_main_files[0]
else:
return None, Status.COMPILATION_ERROR
return Status.COMPILATION_ERROR

def modify_solution(self, solution: Path):
with open(solution, "r") as file:
Expand Down
8 changes: 4 additions & 4 deletions tested/languages/haskell/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def linter(self, remaining: float) -> tuple[list[Message], list[AnnotateCode]]:
assert self.config
return linter.run_hlint(self.config.dodona, remaining)

def cleanup_description(self, description: str) -> str:
return cleanup_description(self, description)
def cleanup_description(self, statement: str) -> str:
return cleanup_description(self, statement)

def cleanup_stacktrace(self, traceback_str: str) -> str:
def cleanup_stacktrace(self, stacktrace: str) -> str:
filename = submission_file(self)
context_file_regex = re.compile(r"(Context[0-9]+|Selector)")
compile_line_regex = re.compile(r"^([0-9]+)(\s*\|.*)$")
Expand All @@ -121,7 +121,7 @@ def cleanup_stacktrace(self, traceback_str: str) -> str:
)
parse_module = r"error: parse error on input ‘module’"
replace_module = r"error: unexpected ‘module’"
traceback = traceback_str.splitlines()
traceback = stacktrace.splitlines()
skip_line, lines = False, []
for line in traceback:
if not line or line == "undefined":
Expand Down
4 changes: 2 additions & 2 deletions tested/languages/java/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def linter(self, remaining: float) -> tuple[list[Message], list[AnnotateCode]]:
assert self.config
return linter.run_checkstyle(self.config.dodona, remaining)

def cleanup_stacktrace(self, traceback: str) -> str:
return jvm_cleanup_stacktrace(traceback, submission_file(self))
def cleanup_stacktrace(self, stacktrace: str) -> str:
return jvm_cleanup_stacktrace(stacktrace, submission_file(self))

def generate_statement(self, statement: Statement) -> str:
from tested.languages.java import generators
Expand Down
8 changes: 4 additions & 4 deletions tested/languages/javascript/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def linter(self, remaining: float) -> tuple[list[Message], list[AnnotateCode]]:
assert self.config
return linter.run_eslint(self.config.dodona, remaining)

def cleanup_stacktrace(self, traceback: str) -> str:
def cleanup_stacktrace(self, stacktrace: str) -> str:
assert self.config
# What this does:
# 1a. While inside the submission code, replace all references to the location with <code>
Expand All @@ -168,7 +168,7 @@ def cleanup_stacktrace(self, traceback: str) -> str:
submission_namespace = f"{submission_name(self)}."

resulting_lines = ""
for line in traceback.splitlines(keepends=True):
for line in stacktrace.splitlines(keepends=True):
# If we encounter an execution location, we are done.
if re.search(execution_location_regex, line):
break
Expand All @@ -183,8 +183,8 @@ def cleanup_stacktrace(self, traceback: str) -> str:

return resulting_lines

def cleanup_description(self, description: str) -> str:
description = cleanup_description(self, description)
def cleanup_description(self, statement: str) -> str:
description = cleanup_description(self, statement)
await_regex = re.compile(r"await\s+")
return await_regex.sub("", description)

Expand Down
16 changes: 9 additions & 7 deletions tested/languages/kotlin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,14 @@ def linter(self, remaining: float) -> tuple[list[Message], list[AnnotateCode]]:
return linter.run_ktlint(self.config.dodona, remaining)

def find_main_file(
self, files: list[Path], name: str
) -> tuple[Path | None, Status]:
self,
files: list[Path],
name: str,
) -> Path | Status:
logger.debug("Finding %s in %s", name, files)
main, status = Language.find_main_file(self, files, name + "Kt")
if status == Status.CORRECT:
return main, status
path_or_status = Language.find_main_file(self, files, name + "Kt")
if isinstance(path_or_status, Path):
return path_or_status
else:
return Language.find_main_file(self, files, name)

Expand All @@ -207,8 +209,8 @@ def filter_function(file_path: Path) -> bool:

return list(x for x in files if filter_function(x))

def cleanup_stacktrace(self, traceback: str) -> str:
return jvm_cleanup_stacktrace(traceback, submission_file(self))
def cleanup_stacktrace(self, stacktrace: str) -> str:
return jvm_cleanup_stacktrace(stacktrace, submission_file(self))

def generate_statement(self, statement: Statement) -> str:
from tested.languages.kotlin import generators
Expand Down
1 change: 1 addition & 0 deletions tested/languages/preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def _create_handling_function(
evaluator = output.oracle.for_language(bundle.config.programming_language)
evaluator_name = conventionalize_namespace(lang_config, evaluator.file.stem)
else:
evaluator = None
evaluator_name = None

def generator(expression: Expression) -> Statement:
Expand Down
6 changes: 3 additions & 3 deletions tested/languages/python/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ def linter(self, remaining: float) -> tuple[list[Message], list[AnnotateCode]]:
return linter.run_pylint(self.config.dodona, remaining)

# Idea and original code: dodona/judge-pythia
def cleanup_stacktrace(self, stacktrace_str: str) -> str:
def cleanup_stacktrace(self, stacktrace: str) -> str:
context_file_regex = re.compile(r"context_[0-9]+_[0-9]+\.py")
file_line_regex = re.compile(rf"\({submission_file(self)}, line (\d+)\)")
file_full_regex = re.compile(rf'File "./{submission_file(self)}", line (\d+)')
stacktrace = stacktrace_str.splitlines(True)
split_stacktrace = stacktrace.splitlines(True)

skip_line, lines = False, []
for line in stacktrace:
for line in split_stacktrace:
line = line.strip("\n")
logger.debug(line)

Expand Down
2 changes: 1 addition & 1 deletion tested/languages/runhaskell/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def compilation(self, files: list[str]) -> CallbackResult:
def execution(self, cwd: Path, file: str, arguments: list[str]) -> Command:
return ["runhaskell", file, *arguments]

def filter_dependencies(self, files: list[str], context_name: str) -> list[str]:
def filter_dependencies(self, files: list[Path], context_name: str) -> list[Path]:
return files

def path_to_dependencies(self) -> list[Path]:
Expand Down
2 changes: 1 addition & 1 deletion tested/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def parse_json_value(value: str) -> "Value":
initialise_converter()
from tested.serialisation import Value

return _suite_converter.loads(value, Value)
return _suite_converter.loads(value, Value) # type: ignore


def parse_json_suite(value: str) -> "Suite":
Expand Down
5 changes: 4 additions & 1 deletion tests/test_serialisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ def run_encoder(bundle: Bundle, values: list[Value]) -> list[str]:

files = filter_files(files, dest)
files = bundle.language.filter_dependencies(files, name)
executable, _ = bundle.language.find_main_file(files, name)
executable = bundle.language.find_main_file(files, name)
assert isinstance(
executable, Path
), f"Could not find main file? Files: {files}, needle: {name}"

# Run the code.
r = execute_file(bundle, executable.name, dest, None)
Expand Down

0 comments on commit 49dfcfc

Please sign in to comment.