_stdlib.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # mypy: allow-untyped-defs
  2. """List of Python standard library modules.
  3. Sadly, there is no reliable way to tell whether a module is part of the
  4. standard library except by comparing to a canonical list.
  5. This is taken from https://github.com/PyCQA/isort/tree/develop/isort/stdlibs,
  6. which itself is sourced from the Python documentation.
  7. """
  8. import sys
  9. def is_stdlib_module(module: str) -> bool:
  10. base_module = module.partition(".")[0]
  11. return base_module in _get_stdlib_modules()
  12. def _get_stdlib_modules():
  13. if sys.version_info.major == 3: # noqa: UP036
  14. if sys.version_info.minor == 9:
  15. return stdlib3_9
  16. if sys.version_info.minor >= 10: # noqa: YTT204
  17. return sys.stdlib_module_names # type: ignore[attr-defined]
  18. elif sys.version_info.major > 3: # noqa: UP036
  19. return sys.stdlib_module_names # type: ignore[attr-defined]
  20. raise RuntimeError(f"Unsupported Python version: {sys.version_info}")
  21. stdlib3_9 = {
  22. "_thread",
  23. "abc",
  24. "aifc",
  25. "argparse",
  26. "array",
  27. "ast",
  28. "asynchat",
  29. "asyncio",
  30. "asyncore",
  31. "atexit",
  32. "audioop",
  33. "base64",
  34. "bdb",
  35. "binascii",
  36. "binhex",
  37. "bisect",
  38. "builtins",
  39. "bz2",
  40. "cProfile",
  41. "calendar",
  42. "cgi",
  43. "cgitb",
  44. "chunk",
  45. "cmath",
  46. "cmd",
  47. "code",
  48. "codecs",
  49. "codeop",
  50. "collections",
  51. "colorsys",
  52. "compileall",
  53. "concurrent",
  54. "configparser",
  55. "contextlib",
  56. "contextvars",
  57. "copy",
  58. "copyreg",
  59. "crypt",
  60. "csv",
  61. "ctypes",
  62. "curses",
  63. "dataclasses",
  64. "datetime",
  65. "dbm",
  66. "decimal",
  67. "difflib",
  68. "dis",
  69. "distutils",
  70. "doctest",
  71. "email",
  72. "encodings",
  73. "ensurepip",
  74. "enum",
  75. "errno",
  76. "faulthandler",
  77. "fcntl",
  78. "filecmp",
  79. "fileinput",
  80. "fnmatch",
  81. "formatter",
  82. "fractions",
  83. "ftplib",
  84. "functools",
  85. "gc",
  86. "getopt",
  87. "getpass",
  88. "gettext",
  89. "glob",
  90. "graphlib",
  91. "grp",
  92. "gzip",
  93. "hashlib",
  94. "heapq",
  95. "hmac",
  96. "html",
  97. "http",
  98. "imaplib",
  99. "imghdr",
  100. "imp",
  101. "importlib",
  102. "inspect",
  103. "io",
  104. "ipaddress",
  105. "itertools",
  106. "json",
  107. "keyword",
  108. "lib2to3",
  109. "linecache",
  110. "locale",
  111. "logging",
  112. "lzma",
  113. "mailbox",
  114. "mailcap",
  115. "marshal",
  116. "math",
  117. "mimetypes",
  118. "mmap",
  119. "modulefinder",
  120. "msilib",
  121. "msvcrt",
  122. "multiprocessing",
  123. "netrc",
  124. "nis",
  125. "nntplib",
  126. "ntpath",
  127. "numbers",
  128. "operator",
  129. "optparse",
  130. "os",
  131. "ossaudiodev",
  132. "parser",
  133. "pathlib",
  134. "pdb",
  135. "pickle",
  136. "pickletools",
  137. "pipes",
  138. "pkgutil",
  139. "platform",
  140. "plistlib",
  141. "poplib",
  142. "posix",
  143. "posixpath",
  144. "pprint",
  145. "profile",
  146. "pstats",
  147. "pty",
  148. "pwd",
  149. "py_compile",
  150. "pyclbr",
  151. "pydoc",
  152. "queue",
  153. "quopri",
  154. "random",
  155. "re",
  156. "readline",
  157. "reprlib",
  158. "resource",
  159. "rlcompleter",
  160. "runpy",
  161. "sched",
  162. "secrets",
  163. "select",
  164. "selectors",
  165. "shelve",
  166. "shlex",
  167. "shutil",
  168. "signal",
  169. "site",
  170. "smtpd",
  171. "smtplib",
  172. "sndhdr",
  173. "socket",
  174. "socketserver",
  175. "spwd",
  176. "sqlite3",
  177. "sre",
  178. "sre_compile",
  179. "sre_constants",
  180. "sre_parse",
  181. "ssl",
  182. "stat",
  183. "statistics",
  184. "string",
  185. "stringprep",
  186. "struct",
  187. "subprocess",
  188. "sunau",
  189. "symbol",
  190. "symtable",
  191. "sys",
  192. "sysconfig",
  193. "syslog",
  194. "tabnanny",
  195. "tarfile",
  196. "telnetlib",
  197. "tempfile",
  198. "termios",
  199. "test",
  200. "textwrap",
  201. "threading",
  202. "time",
  203. "timeit",
  204. "tkinter",
  205. "token",
  206. "tokenize",
  207. "trace",
  208. "traceback",
  209. "tracemalloc",
  210. "tty",
  211. "turtle",
  212. "turtledemo",
  213. "types",
  214. "typing",
  215. "unicodedata",
  216. "unittest",
  217. "urllib",
  218. "uu",
  219. "uuid",
  220. "venv",
  221. "warnings",
  222. "wave",
  223. "weakref",
  224. "webbrowser",
  225. "winreg",
  226. "winsound",
  227. "wsgiref",
  228. "xdrlib",
  229. "xml",
  230. "xmlrpc",
  231. "zipapp",
  232. "zipfile",
  233. "zipimport",
  234. "zlib",
  235. "zoneinfo",
  236. }