Skip to content

Commit

Permalink
[Fix] Fix get class attribute from a string (#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
HAOCHENYE authored Sep 18, 2023
1 parent be3d5c6 commit 53474ef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
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

0 comments on commit 53474ef

Please sign in to comment.