Skip to content

Commit c2ee22c

Browse files
authored
Merge pull request #137 from ImogenBits/import_machinery
Fix problem importing
2 parents 4451360 + 8ee192d commit c2ee22c

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

algobattle/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ def init(
350350
gitignore += f"{res_path.relative_to(target)}/\n"
351351
target.joinpath(".gitignore").write_text(gitignore)
352352

353+
if not parsed_config.problem.location.is_absolute():
354+
parsed_config.problem.location = target / parsed_config.problem.location
353355
problem_obj = parsed_config.loaded_problem
354356
if schemas:
355357
instance: type[Instance] = problem_obj.instance_cls

algobattle/match.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -624,15 +624,6 @@ class AlgobattleConfig(BaseModel):
624624

625625
model_config = ConfigDict(revalidate_instances="always")
626626

627-
@model_validator(mode="after")
628-
def check_problem_defined(self) -> Self:
629-
"""Validates that the specified problem is either installed or dynamically specified."""
630-
prob = self.match.problem
631-
if not self.problem.location.is_file() and prob not in Problem.available():
632-
raise ValueError(f"The specified problem {prob} cannot be found")
633-
else:
634-
return self
635-
636627
@cached_property
637628
def loaded_problem(self) -> Problem:
638629
"""The problem this config uses."""

algobattle/problem.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,15 @@ def score(
332332
def load_file(cls, name: str, file: Path) -> Self:
333333
"""Loads the problem from the specified file."""
334334
existing_problems = cls._problems.copy()
335-
import_file_as_module(file, "__algobattle_problem__")
336-
new_problems = {n: p for n, p in cls._problems.items() if n not in existing_problems}
337-
if name not in new_problems:
338-
raise ValueError(f"The {name} problem is not defined in {file}")
339-
else:
340-
return cls._problems[name]
335+
cls._problems = {}
336+
try:
337+
import_file_as_module(file, "__algobattle_problem__")
338+
if name not in cls._problems:
339+
raise ValueError(f"The {name} problem is not defined in {file}")
340+
else:
341+
return cls._problems[name]
342+
finally:
343+
cls._problems = existing_problems
341344

342345
@classmethod
343346
def load(cls, name: str, file: Path | None = None) -> Self:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "pdm.backend"
44

55
[project]
66
name = "algobattle-base"
7-
version = "4.0.0"
7+
version = "4.0.1"
88
description = "The Algobattle lab course package."
99
readme = "README.md"
1010
requires-python = ">=3.11"

0 commit comments

Comments
 (0)