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

Add brain to fix tensorflow import errors. #2174

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions astroid/brain/brain_tensorflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
"""Astroid hooks for understanding tensorflow's imports."""
from astroid import MANAGER
from astroid.exceptions import AstroidBuildingError


def _tensorflow_fail_hook(modname: str):
parts = modname.split(".", 1)
fallbacks = ("python", "_api.v2")
if parts[0] == "tensorflow":
for fallback in fallbacks:
if parts[1].startswith(fallbacks):
continue
try:
return MANAGER.ast_from_module_name(f"tensorflow.{fallback}.{parts[1]}")
except AstroidBuildingError:
continue
raise AstroidBuildingError(modname=modname)


MANAGER.register_failed_import_hook(_tensorflow_fail_hook)