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
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions mmengine/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,12 @@ def get_object_from_string(obj_name: str):
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
2 changes: 2 additions & 0 deletions tests/test_utils/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,5 @@ def test_locate():
assert get_object_from_string('mmengine.model') is model_module
assert get_object_from_string(
'mmengine.model.BaseModel') is model_module.BaseModel
assert get_object_from_string('mmengine.model.BaseModel.forward') is \
model_module.BaseModel.forward
Loading