MCPcopy Index your code
hub / github.com/python/mypy / compute_search_paths

Function compute_search_paths

mypy/modulefinder.py:884–969  ·  view source on GitHub ↗

Compute the search paths as specified in PEP 561. There are the following 4 members created: - User code (from `sources`) - MYPYPATH (set either via config or environment variable) - installed package directories (which will later be split into stub-only and inline) - typeshed

(
    sources: list[BuildSource], options: Options, data_dir: str, alt_lib_path: str | None = None
)

Source from the content-addressed store, hash-verified

882
883
884def compute_search_paths(
885 sources: list[BuildSource], options: Options, data_dir: str, alt_lib_path: str | None = None
886) -> SearchPaths:
887 """Compute the search paths as specified in PEP 561.
888
889 There are the following 4 members created:
890 - User code (from `sources`)
891 - MYPYPATH (set either via config or environment variable)
892 - installed package directories (which will later be split into stub-only and inline)
893 - typeshed
894 """
895 # Determine the default module search path.
896 lib_path = collections.deque(
897 default_lib_path(
898 data_dir, options.python_version, custom_typeshed_dir=options.custom_typeshed_dir
899 )
900 )
901
902 if options.use_builtins_fixtures:
903 # Use stub builtins (to speed up test cases and to make them easier to
904 # debug). This is a test-only feature, so assume our files are laid out
905 # as in the source tree.
906 # We also need to allow overriding where to look for it. Argh.
907 root_dir = os.getenv("MYPY_TEST_PREFIX", None)
908 if not root_dir:
909 root_dir = os.path.dirname(os.path.dirname(__file__))
910 root_dir = os.path.abspath(root_dir)
911 lib_path.appendleft(os.path.join(root_dir, "test-data", "unit", "lib-stub"))
912 # alt_lib_path is used by some tests to bypass the normal lib_path mechanics.
913 # If we don't have one, grab directories of source files.
914 python_path: list[str] = []
915 if not alt_lib_path:
916 for source in sources:
917 # Include directory of the program file in the module search path.
918 if source.base_dir:
919 dir = source.base_dir
920 if dir not in python_path:
921 python_path.append(dir)
922
923 # Do this even if running as a file, for sanity (mainly because with
924 # multiple builds, there could be a mix of files/modules, so its easier
925 # to just define the semantics that we always add the current director
926 # to the lib_path
927 # TODO: Don't do this in some cases; for motivation see see
928 # https://github.com/python/mypy/issues/4195#issuecomment-341915031
929 if options.bazel:
930 dir = "."
931 else:
932 dir = os.getcwd()
933 if dir not in lib_path:
934 python_path.insert(0, dir)
935
936 # Start with a MYPYPATH environment variable at the front of the mypy_path, if defined.
937 mypypath = mypy_path()
938
939 # Add a config-defined mypy path.
940 mypypath.extend(options.mypy_path)
941

Callers 4

build_innerFunction · 0.90
setup_worker_managerFunction · 0.90

Calls 14

default_lib_pathFunction · 0.85
mypy_pathFunction · 0.85
get_search_dirsFunction · 0.85
anyFunction · 0.85
printFunction · 0.85
SearchPathsClass · 0.85
tupleClass · 0.85
reversedFunction · 0.85
appendMethod · 0.80
insertMethod · 0.80
extendMethod · 0.80
exitMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…