summaryrefslogtreecommitdiff
path: root/astroid/modutils.py
diff options
context:
space:
mode:
Diffstat (limited to 'astroid/modutils.py')
-rw-r--r--astroid/modutils.py68
1 files changed, 30 insertions, 38 deletions
diff --git a/astroid/modutils.py b/astroid/modutils.py
index 0c009b13..4e6ed86b 100644
--- a/astroid/modutils.py
+++ b/astroid/modutils.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-# Copyright (c) 2014-2018 Claudiu Popa <pcmanticore@gmail.com>
+# Copyright (c) 2014-2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2014 Google, Inc.
# Copyright (c) 2014 Denis Laxalde <denis.laxalde@logilab.fr>
# Copyright (c) 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
@@ -9,9 +9,13 @@
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
# Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
# Copyright (c) 2016 Ceridwen <ceridwenv@gmail.com>
+# Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
# Copyright (c) 2018 Mario Corchero <mcorcherojim@bloomberg.net>
# Copyright (c) 2018 Mario Corchero <mariocj89@gmail.com>
# Copyright (c) 2018 Anthony Sottile <asottile@umich.edu>
+# Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
+# Copyright (c) 2019 markmcclain <markmcclain@users.noreply.github.com>
+# Copyright (c) 2019 BasPH <BasPH@users.noreply.github.com>
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
@@ -40,6 +44,7 @@ from distutils.errors import DistutilsPlatformError
# distutils is replaced by virtualenv with a module that does
# weird path manipulations in order to get to the
# real distutils module.
+from typing import Optional, List
from .interpreter._import import spec
from .interpreter._import import util
@@ -198,16 +203,15 @@ def load_module_from_name(dotted_name, path=None, use_sys=True):
return load_module_from_modpath(dotted_name.split("."), path, use_sys)
-def load_module_from_modpath(parts, path=None, use_sys=1):
+def load_module_from_modpath(parts, path: Optional[List[str]] = None, use_sys=1):
"""Load a python module from its split name.
:type parts: list(str) or tuple(str)
:param parts:
python name of a module or package split on '.'
- :type path: list or None
:param path:
- optional list of path where the module or package should be
+ Optional list of path where the module or package should be
searched (use sys.path if nothing or None is given)
:type use_sys: bool
@@ -254,15 +258,16 @@ def load_module_from_modpath(parts, path=None, use_sys=1):
return module
-def load_module_from_file(filepath, path=None, use_sys=True, extrapath=None):
+def load_module_from_file(
+ filepath: str, path: Optional[List[str]] = None, use_sys=True
+):
"""Load a Python module from it's path.
:type filepath: str
:param filepath: path to the python module or package
- :type path: list or None
- :param path:
- optional list of path where the module or package should be
+ :param Optional[List[str]] path:
+ Optional list of path where the module or package should be
searched (use sys.path if nothing or None is given)
:type use_sys: bool
@@ -270,13 +275,12 @@ def load_module_from_file(filepath, path=None, use_sys=True, extrapath=None):
boolean indicating whether the sys.modules dictionary should be
used or not
-
:raise ImportError: if the module or package is not found
:rtype: module
:return: the loaded module
"""
- modpath = modpath_from_file(filepath, extrapath)
+ modpath = modpath_from_file(filepath)
return load_module_from_modpath(modpath, path, use_sys)
@@ -327,28 +331,18 @@ def _get_relative_base_path(filename, path_to_check):
return None
-def modpath_from_file_with_callback(filename, extrapath=None, is_package_cb=None):
+def modpath_from_file_with_callback(filename, path=None, is_package_cb=None):
filename = os.path.expanduser(_path_from_filename(filename))
-
- if extrapath is not None:
- for path_ in itertools.chain(map(_canonicalize_path, extrapath), extrapath):
- path = os.path.abspath(path_)
- if not path:
- continue
- submodpath = _get_relative_base_path(filename, path)
- if not submodpath:
- continue
- if is_package_cb(path, submodpath[:-1]):
- return extrapath[path_].split(".") + submodpath
-
- for path in itertools.chain(map(_canonicalize_path, sys.path), sys.path):
- path = _cache_normalize_path(path)
- if not path:
+ for pathname in itertools.chain(
+ path or [], map(_canonicalize_path, sys.path), sys.path
+ ):
+ pathname = _cache_normalize_path(pathname)
+ if not pathname:
continue
- modpath = _get_relative_base_path(filename, path)
+ modpath = _get_relative_base_path(filename, pathname)
if not modpath:
continue
- if is_package_cb(path, modpath[:-1]):
+ if is_package_cb(pathname, modpath[:-1]):
return modpath
raise ImportError(
@@ -356,19 +350,17 @@ def modpath_from_file_with_callback(filename, extrapath=None, is_package_cb=None
)
-def modpath_from_file(filename, extrapath=None):
- """given a file path return the corresponding split module's name
- (i.e name of a module or package split on '.')
+def modpath_from_file(filename, path=None):
+ """Get the corresponding split module's name from a filename
+
+ This function will return the name of a module or package split on `.`.
:type filename: str
:param filename: file's path for which we want the module's name
- :type extrapath: dict
- :param extrapath:
- optional extra search path, with path as key and package name for the path
- as value. This is usually useful to handle package split in multiple
- directories using __path__ trick.
-
+ :type Optional[List[str]] path:
+ Optional list of path where the module or package should be
+ searched (use sys.path if nothing or None is given)
:raise ImportError:
if the corresponding module's name has not been found
@@ -376,7 +368,7 @@ def modpath_from_file(filename, extrapath=None):
:rtype: list(str)
:return: the corresponding split module's name
"""
- return modpath_from_file_with_callback(filename, extrapath, check_modpath_has_init)
+ return modpath_from_file_with_callback(filename, path, check_modpath_has_init)
def file_from_modpath(modpath, path=None, context_file=None):