Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix replace() use in AgentSpec #326

Merged
merged 5 commits into from
Dec 22, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions smarts/core/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ def __post_init__(self):
if self.policy_builder:
logger.warning(
f"[DEPRECATED] Please use AgentSpec(agent_builder=<...>) instead of AgentSpec(policy_builder=<..>):\n {self}"
"policy_builder will overwrite agent_builder"
)
assert self.agent_builder is None, self.agent_builder
self.agent_builder = self.policy_builder

if self.policy_params:
logger.warning(
f"[DEPRECATED] Please use AgentSpec(agent_params=<...>) instead of AgentSpec(policy_params=<..>):\n {self}"
"policy_builder will overwrite agent_builder"
)
assert self.agent_params is None, self.agent_params
self.agent_params = self.policy_params

self.policy_params = self.agent_params
Expand All @@ -146,7 +146,34 @@ def __post_init__(self):
def replace(self, **kwargs) -> "AgentSpec":
"""Return a copy of this AgentSpec with the given fields updated."""

return replace(self, **kwargs)
replacements = [
("policy_builder", "agent_builder"),
("policy_params", "agent_params"),
("perform_self_test", None),
]

assert None not in kwargs, f"Error: replace contains invalid key: ({None})"

kwargs_copy = kwargs.copy()
for deprecated, current in replacements:
if deprecated in kwargs:
if current:
logger.warning(
f"[DEPRECATED] Please use AgentSpec.replace({current}=<...>) instead of AgentSpec.replace({deprecated}=<...>)\n"
)
else:
logger.warning(
f"[DEPRECATED] Attribute {deprecated} no longer has effect."
)
assert (
current not in kwargs
), f"Mixed current ({current}) and deprecated ({deprecated}) values in replace"
moved = kwargs[deprecated]
del kwargs_copy[deprecated]
if current:
kwargs_copy[current] = moved

return replace(self, **kwargs_copy)

def build_agent(self) -> Agent:
"""Construct an Agent from the AgentSpec configuration."""
Expand Down