From 03391ad27ddbe5997da2eccfc13c12744590bdc4 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 31 Mar 2023 08:54:58 -0700 Subject: [PATCH] Be more paranoid with attribute access on modules (#603) On some TensorFlow module, getattr(module, "__flle__", None) can throw a TypeError. --- docs/changelog.md | 1 + pyanalyze/node_visitor.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 116ba0fe..b2cd93e0 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,7 @@ ## Unreleased +- Fix crash when `getattr()` on a module object throws an error (#603) - Fix handling of positional-only arguments using `/` syntax in stubs (#601) - Fix bug where objects with a `__call__` method that takes `*args` instead of `self` was not considered callable (#600) diff --git a/pyanalyze/node_visitor.py b/pyanalyze/node_visitor.py index ed43548e..76bb8172 100644 --- a/pyanalyze/node_visitor.py +++ b/pyanalyze/node_visitor.py @@ -43,6 +43,7 @@ from typing_extensions import NotRequired, Protocol, TypedDict from . import analysis_lib +from .safe import safe_getattr, safe_isinstance Error = Dict[str, Any] @@ -977,7 +978,9 @@ def _get_all_python_files( if module is None: continue # ignore compiled modules - if not getattr(module, "__file__", None) or module.__file__.endswith(".so"): + if not safe_isinstance( + safe_getattr(module, "__file__", None), str + ) or module.__file__.endswith(".so"): continue if cls._should_ignore_module(module_name): continue