diff options
author | hippo91 <guillaume.peillex@gmail.com> | 2021-08-29 20:57:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-29 20:57:34 +0200 |
commit | 8049834c518adae06f6848f01bcba33f49e5cdf5 (patch) | |
tree | 99cc737e324758219dc0fcfa5eb608b5f58760da /astroid/modutils.py | |
parent | e90016f8baf7de815249ee60dc82636b3d7380b0 (diff) | |
download | astroid-git-8049834c518adae06f6848f01bcba33f49e5cdf5.tar.gz |
Bug pylint 3342 (#1148)
* Adds some type hints
* Adds a logger module
* Remove this useless module
* It is probably not a good idea to apply transforms on module which have been authorized to be directly imported
* Adds a function named is_module_name_part_of_extension_package_whitelist which returns True if the beginning of a module dotted name is part of the package whitelist in argument
* Adds the extension_package_whitelist variable to the brainless manager in order unittest dealing with this managert to be ok
* Adds two tests that check the behavior of the is_module_name_part_of_extension_package_whitelist function
* Updating Changelog
* Adds explanation to the ChangeLog entry
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'astroid/modutils.py')
-rw-r--r-- | astroid/modutils.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/astroid/modutils.py b/astroid/modutils.py index 2604f9b4..65a6253e 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -46,8 +46,10 @@ import itertools import os import platform import sys +import types from distutils.errors import DistutilsPlatformError # pylint: disable=import-error from distutils.sysconfig import get_python_lib # pylint: disable=import-error +from typing import Set from astroid.interpreter._import import spec, util @@ -197,7 +199,7 @@ def _cache_normalize_path(path): return result -def load_module_from_name(dotted_name): +def load_module_from_name(dotted_name: str) -> types.ModuleType: """Load a Python module from its name. :type dotted_name: str @@ -648,3 +650,18 @@ def is_namespace(specobj): def is_directory(specobj): return specobj.type == spec.ModuleType.PKG_DIRECTORY + + +def is_module_name_part_of_extension_package_whitelist( + module_name: str, package_whitelist: Set[str] +) -> bool: + """ + Returns True if one part of the module name is in the package whitelist + + >>> is_module_name_part_of_extension_package_whitelist('numpy.core.umath', {'numpy'}) + True + """ + parts = module_name.split(".") + return any( + ".".join(parts[:x]) in package_whitelist for x in range(1, len(parts) + 1) + ) |