Skip to content

Commit

Permalink
Added error for when "require MONITOR" instead of "require monitor MO…
Browse files Browse the repository at this point in the history
…NITOR"
  • Loading branch information
Eric-Vin committed Jan 11, 2024
1 parent 29649c6 commit a37695b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/scenic/core/dynamics/scenarios.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Dynamic scenarios."""

import ast
from collections import defaultdict
import dataclasses
import functools
Expand All @@ -8,8 +9,9 @@

import rv_ltl

import scenic
import scenic.core.dynamics as dynamics
from scenic.core.errors import InvalidScenarioError
from scenic.core.errors import InvalidScenarioError, ScenicSyntaxError
from scenic.core.lazy_eval import DelayedArgument, needsLazyEvaluation
from scenic.core.requirements import (
DynamicRequirement,
Expand Down Expand Up @@ -429,6 +431,23 @@ def _compileRequirements(self):
assert requirementSyntax is not None
for reqID, requirement in self._pendingRequirements.items():
syntax = requirementSyntax[reqID] if requirementSyntax else None

# Catch the simple case where someone has most likely forgotten the "monitor"
# keyword.
if (
(not requirement.ty == RequirementType.monitor)
and isinstance(syntax, ast.Call)
and isinstance(syntax.func, ast.Name)
and syntax.func.id in namespace
and isinstance(namespace[syntax.func.id], type)
and issubclass(
namespace[syntax.func.id], scenic.core.dynamics.behaviors.Monitor
)
):
raise ScenicSyntaxError(
f"Missing 'monitor' keyword after 'require' when instantiating '{syntax.func.id}'"
)

compiledReq = requirement.compile(namespace, self, syntax)

self._registerCompiledRequirement(compiledReq)
Expand Down
12 changes: 12 additions & 0 deletions tests/syntax/test_dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,18 @@ def test_require_monitor_invalid():
sampleScene(scenario)


def test_require_monitor_error():
with pytest.raises(ScenicSyntaxError):
compileScenic(
"""
monitor Monitor():
wait
ego = new Object
require Monitor()
"""
)


def test_old_style_monitor():
with pytest.raises(ScenicSyntaxError):
compileScenic(
Expand Down

0 comments on commit a37695b

Please sign in to comment.