__getattr__ should rase AttributeError not ImportError on missing attributes (#12801)

[The python
spec](https://docs.python.org/3/reference/datamodel.html#object.__getattr__)
requires that `__getattr__` throw `AttributeError` for missing
attributes but there are several places throwing `ImportError` in the
current code base. This causes a specific problem with `hasattr` since
it calls `__getattr__` then looks only for `AttributeError` exceptions.
At present, calling `hasattr` on any of these modules will raise an
unexpected exception that most code will not handle as `hasattr`
throwing exceptions is not expected.

In our case this is triggered by an exception tracker (Airbrake) that
attempts to collect the version of all installed modules with code that
looks like: `if hasattr(mod, "__version__"):`. With `HEAD` this is
causing our exception tracker to fail on all exceptions.

I only changed instances of unknown attributes raising `ImportError` and
left instances of known attributes raising `ImportError`. It feels a
little weird but doesn't seem to break anything.
This commit is contained in:
Mark Bell 2023-11-02 17:08:54 -04:00 committed by GitHub
parent d966e4d13a
commit 3276aa3e17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 7 additions and 7 deletions

View File

@ -90,7 +90,7 @@ def __getattr__(name: str) -> Any:
"for more information.\n"
f"Please update your import statement from: `{old_path}` to `{new_path}`."
)
raise ImportError(f"{name} does not exist")
raise AttributeError(f"{name} does not exist")
__all__ = [

View File

@ -78,7 +78,7 @@ def __getattr__(name: str) -> Any:
"for more information.\n"
f"Please update your import statement from: `{old_path}` to `{new_path}`."
)
raise ImportError(f"{name} does not exist")
raise AttributeError(f"{name} does not exist")
__all__ = [

View File

@ -11,7 +11,7 @@ def __getattr__(name: str) -> Any:
old_path = "langchain." + here + "." + name
new_path = "langchain_experimental." + here + "." + name
raise ImportError(
raise AttributeError(
"This agent has been moved to langchain experiment. "
"This agent relies on python REPL tool under the hood, so to use it "
"safely please sandbox the python REPL. "

View File

@ -11,7 +11,7 @@ def __getattr__(name: str) -> Any:
old_path = "langchain." + here + "." + name
new_path = "langchain_experimental." + here + "." + name
raise ImportError(
raise AttributeError(
"This agent has been moved to langchain experiment. "
"This agent relies on python REPL tool under the hood, so to use it "
"safely please sandbox the python REPL. "

View File

@ -11,7 +11,7 @@ def __getattr__(name: str) -> Any:
old_path = "langchain." + here + "." + name
new_path = "langchain_experimental." + here + "." + name
raise ImportError(
raise AttributeError(
"This agent has been moved to langchain experiment. "
"This agent relies on python REPL tool under the hood, so to use it "
"safely please sandbox the python REPL. "

View File

@ -11,7 +11,7 @@ def __getattr__(name: str) -> Any:
old_path = "langchain." + here + "." + name
new_path = "langchain_experimental." + here + "." + name
raise ImportError(
raise AttributeError(
"This agent has been moved to langchain experiment. "
"This agent relies on python REPL tool under the hood, so to use it "
"safely please sandbox the python REPL. "

View File

@ -11,7 +11,7 @@ def __getattr__(name: str) -> Any:
old_path = "langchain." + here + "." + name
new_path = "langchain_experimental." + here + "." + name
raise ImportError(
raise AttributeError(
"This agent has been moved to langchain experiment. "
"This agent relies on python REPL tool under the hood, so to use it "
"safely please sandbox the python REPL. "