Skip to content

Commit

Permalink
Always getattr() in lazy import machinery (#1963)
Browse files Browse the repository at this point in the history
* Always getattr() in lazy import machinery

* Add explanatory safety comment about getattr recursion

* update releasehistory

---------

Co-authored-by: Jeff Wagner <[email protected]>
  • Loading branch information
Yoshanuikabundi and j-wags authored Jan 7, 2025
1 parent c303f30 commit 18d9ddb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w
### API-breaking changes

### Behavior changes
- [PR #1963](https://github.com/openforcefield/openff-toolkit/pull/1963): Always use `getattr` in lazy loading machinery.

### Bugfixes

Expand Down
11 changes: 7 additions & 4 deletions openff/toolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ def __getattr__(name):
obj_mod = _lazy_imports_obj.get(name)
if obj_mod is not None:
mod = importlib.import_module(obj_mod)
try:
return mod.__dict__[name]
except KeyError: # account for lazy loaders
return getattr(mod, name)
# This only causes an infinite recursive loop if an object that does not
# exist is declared in `_lazy_imports_obj` to be lazy loaded from the
# current module. Since there is no reason to include either an object
# from the current module or an object that does not exist in
# `_lazy_imports_obj`, and since the function would fail in this case
# anyway, this is safe.
return getattr(mod, name)

lazy_mod = _lazy_imports_mod.get(name)
if lazy_mod is not None:
Expand Down

0 comments on commit 18d9ddb

Please sign in to comment.