Skip to content

Commit

Permalink
Replace all other instances of __import__
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanmai committed Jun 23, 2023
1 parent b98ac8b commit 6347118
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 5 additions & 5 deletions scripts/data_overlap/common/object_spec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
import importlib
from typing import Any, Dict, Tuple


Expand All @@ -18,12 +19,11 @@ def __hash__(self):

def create_object(spec: ObjectSpec):
"""Create the actual object given the `spec`."""
# Adapted from https://stackoverflow.com/questions/547829/how-to-dynamically-load-a-python-class
components = spec.class_name.split(".")
module: Any = __import__(components[0])
for component in components[1:]:
module = getattr(module, component)
return module(**spec.args)
class_name = components[-1]
module_name = ".".join(components[:-1])
cls = getattr(importlib.import_module(module_name), class_name)
return cls(**spec.args)


def parse_object_spec(description: str) -> ObjectSpec:
Expand Down
9 changes: 5 additions & 4 deletions src/helm/benchmark/run_specs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib
import itertools
from typing import Any, Callable, List, Dict, Optional, Set, TypeVar

Expand Down Expand Up @@ -2281,10 +2282,10 @@ def alter_run_spec(run_spec: RunSpec) -> RunSpec:
increase_max_tokens_expander = IncreaseMaxTokensRunExpander(value=AnthropicClient.ADDITIONAL_TOKENS)
# Get scenario tags
components = run_spec.scenario_spec.class_name.split(".")
module: Any = __import__(components[0])
for component in components[1:]:
module = getattr(module, component)
scenario_tags: List[str] = module.tags
class_name = components[-1]
module_name = ".".join(components[:-1])
cls = getattr(importlib.import_module(module_name), class_name)
scenario_tags: List[str] = cls.tags
# If the scenario is instruction, do not use PROMPT_ANSWER_START
if "instructions" in scenario_tags:
format_expander = FormatPromptRunExpander(
Expand Down

0 comments on commit 6347118

Please sign in to comment.