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] Fix get class attribute from a string #1345

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Changes from 2 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
7 changes: 4 additions & 3 deletions mmengine/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,16 +527,17 @@ def get_object_from_string(obj_name: str):
module_name = f'{module_name}.{part}'
except StopIteration:
# if obj is a module
return module
return obj
zhouzaida marked this conversation as resolved.
Show resolved Hide resolved
except ImportError:
return None

# get class or attribute from module
obj = module
while True:
try:
obj_cls = getattr(module, part)
obj = getattr(obj, part)
part = next(parts)
except StopIteration:
return obj_cls
return obj
except AttributeError:
return None