Skip to content

Commit

Permalink
More conservative interpretation of PathFinder failures
Browse files Browse the repository at this point in the history
Follow up to pylint-dev#1536
  • Loading branch information
jacobtylerwalls committed May 31, 2022
1 parent 1ccb1c4 commit 900056f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
15 changes: 11 additions & 4 deletions astroid/interpreter/_import/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@

import pathlib
import sys
from functools import lru_cache
from importlib._bootstrap_external import _NamespacePath
from importlib.util import _find_spec_from_path


@lru_cache(maxsize=4096)
def is_namespace(modname: str) -> bool:
if modname in sys.builtin_module_names:
return False

try:
if sys.modules[modname].__spec__ is None and modname != "__main__":
return True
except KeyError:
pass
except AttributeError:
# work around for "py" library:
# https://github.com/pytest-dev/apipkg/issues/13
return False

found_spec = None

# find_spec() attempts to import parent packages when given dotted paths.
Expand All @@ -34,8 +42,7 @@ def is_namespace(modname: str) -> bool:
working_modname, path=last_submodule_search_locations
)
except ValueError:
# Assume it's a .pth file, unless it's __main__
return modname != "__main__"
return False
except KeyError:
# Intermediate steps might raise KeyErrors
# https://github.com/python/cpython/issues/93334
Expand Down
3 changes: 1 addition & 2 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from astroid.const import BRAIN_MODULES_DIRECTORY
from astroid.exceptions import AstroidBuildingError, AstroidImportError
from astroid.interpreter._import import spec, util
from astroid.interpreter._import import spec
from astroid.modutils import (
NoSourceFile,
_cache_normalize_path_,
Expand Down Expand Up @@ -384,7 +384,6 @@ def clear_cache(self) -> None:
for lru_cache in (
LookupMixIn.lookup,
_cache_normalize_path_,
util.is_namespace,
ObjectModel.attributes,
):
lru_cache.cache_clear()
Expand Down
2 changes: 0 additions & 2 deletions tests/unittest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
lrus = (
astroid.nodes.node_classes.LookupMixIn.lookup,
astroid.modutils._cache_normalize_path_,
util.is_namespace,
astroid.interpreter.objectmodel.ObjectModel.attributes,
)

Expand All @@ -344,7 +343,6 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
# Generate some hits and misses
ClassDef().lookup("garbage")
is_standard_module("unittest", std_path=["garbage_path"])
util.is_namespace("unittest")
astroid.interpreter.objectmodel.ObjectModel().attributes()

# Did the hits or misses actually happen?
Expand Down

0 comments on commit 900056f

Please sign in to comment.