Skip to content

Commit

Permalink
Do not _import_dotted_name if name is not dotted.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 714882855
  • Loading branch information
Fiddle-Config Team authored and copybara-github committed Jan 13, 2025
1 parent 439c0e9 commit 24b8b2e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions fiddle/_src/absl_flags/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def error_prefix(self, name: str) -> str:
return f'Could not load fiddler {name!r}'


def _is_dotted_name(name: str) -> bool:
return len(name.split('.')) >= 2


def _import_dotted_name(
name: str,
mode: ImportDottedNameDebugContext,
Expand All @@ -69,16 +73,17 @@ def _import_dotted_name(
AttributeError: If the imported module does not contain a value with
the indicated name.
"""
name_pieces = name.split('.')
if module is not None:
name_pieces = [module.__name__] + name_pieces

if len(name_pieces) < 2:
if not _is_dotted_name(name):
raise ValueError(
f'{mode.error_prefix(name)}: Expected a dotted name including the '
'module name.'
)

name_pieces = name.split('.')
if module is not None:
name_pieces = [module.__name__] + name_pieces

# We don't know where the module ends and the name begins; so we need to
# try different split points. Longer module names take precedence.
for i in range(len(name_pieces) - 1, 0, -1):
Expand Down Expand Up @@ -245,7 +250,7 @@ def resolve_function_reference(
"""
if hasattr(module, function_name):
return getattr(module, function_name)
elif allow_imports:
elif allow_imports and _is_dotted_name(function_name):
# Try a relative import first.
if module is not None:
try:
Expand All @@ -271,8 +276,8 @@ def resolve_function_reference(
else:
available_names = module_reflection.find_base_config_like_things(module)
raise ValueError(
f'{failure_msg_prefix} {function_name!r}; '
f'available names: {", ".join(available_names)}.'
f'{failure_msg_prefix} {function_name!r}: Could not resolve reference '
f'to named function, available names: {", ".join(available_names)}.'
)


Expand Down
2 changes: 1 addition & 1 deletion fiddle/_src/absl_flags/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_module_relative_resolution_falls_back_to_absolute(self):

def test_raises_without_resolvable_name(self):
with self.assertRaisesRegex(
ValueError, "Could not init a buildable from 'config_bar'"
ValueError, "'config_bar': Could not resolve reference"
):
utils.resolve_function_reference(
function_name='config_bar',
Expand Down

0 comments on commit 24b8b2e

Please sign in to comment.