Return default standard library search paths. Guaranteed to be normalised.
(
data_dir: str, pyversion: tuple[int, int], custom_typeshed_dir: str | None
)
| 792 | |
| 793 | |
| 794 | def default_lib_path( |
| 795 | data_dir: str, pyversion: tuple[int, int], custom_typeshed_dir: str | None |
| 796 | ) -> list[str]: |
| 797 | """Return default standard library search paths. Guaranteed to be normalised.""" |
| 798 | |
| 799 | data_dir = os.path.abspath(data_dir) |
| 800 | path: list[str] = [] |
| 801 | |
| 802 | if custom_typeshed_dir: |
| 803 | custom_typeshed_dir = os.path.abspath(custom_typeshed_dir) |
| 804 | typeshed_dir = os.path.join(custom_typeshed_dir, "stdlib") |
| 805 | mypy_extensions_dir = os.path.join(custom_typeshed_dir, "stubs", "mypy-extensions") |
| 806 | librt_dir = os.path.join(custom_typeshed_dir, "stubs", "librt") |
| 807 | versions_file = os.path.join(typeshed_dir, "VERSIONS") |
| 808 | if not os.path.isdir(typeshed_dir) or not os.path.isfile(versions_file): |
| 809 | print( |
| 810 | "error: --custom-typeshed-dir does not point to a valid typeshed ({})".format( |
| 811 | custom_typeshed_dir |
| 812 | ), |
| 813 | file=sys.stderr, |
| 814 | ) |
| 815 | sys.exit(2) |
| 816 | else: |
| 817 | auto = os.path.join(data_dir, "stubs-auto") |
| 818 | if os.path.isdir(auto): |
| 819 | data_dir = auto |
| 820 | typeshed_dir = os.path.join(data_dir, "typeshed", "stdlib") |
| 821 | mypy_extensions_dir = os.path.join(data_dir, "typeshed", "stubs", "mypy-extensions") |
| 822 | librt_dir = os.path.join(data_dir, "typeshed", "stubs", "librt") |
| 823 | path.append(typeshed_dir) |
| 824 | |
| 825 | # Get mypy-extensions and librt stubs from typeshed, since we treat them as |
| 826 | # "internal" libraries, similar to typing and typing-extensions. |
| 827 | path.append(mypy_extensions_dir) |
| 828 | path.append(librt_dir) |
| 829 | |
| 830 | # Add fallback path that can be used if we have a broken installation. |
| 831 | if sys.platform != "win32": |
| 832 | path.append("/usr/local/lib/mypy") |
| 833 | if not path: |
| 834 | print( |
| 835 | "Could not resolve typeshed subdirectories. Your mypy install is broken.\n" |
| 836 | "Python executable is located at {}.\nMypy located at {}".format( |
| 837 | sys.executable, data_dir |
| 838 | ), |
| 839 | file=sys.stderr, |
| 840 | ) |
| 841 | sys.exit(1) |
| 842 | return path |
| 843 | |
| 844 | |
| 845 | @functools.cache |