_stdlib.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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:
  14. if sys.version_info.minor == 8:
  15. return stdlib3_8
  16. if sys.version_info.minor == 9:
  17. return stdlib3_9
  18. if sys.version_info.minor >= 10:
  19. return sys.stdlib_module_names # type: ignore[attr-defined]
  20. elif sys.version_info.major > 3:
  21. return sys.stdlib_module_names # type: ignore[attr-defined]
  22. raise RuntimeError(f"Unsupported Python version: {sys.version_info}")
  23. stdlib3_8 = {
  24. "_dummy_thread",
  25. "_thread",
  26. "abc",
  27. "aifc",
  28. "argparse",
  29. "array",
  30. "ast",
  31. "asynchat",
  32. "asyncio",
  33. "asyncore",
  34. "atexit",
  35. "audioop",
  36. "base64",
  37. "bdb",
  38. "binascii",
  39. "binhex",
  40. "bisect",
  41. "builtins",
  42. "bz2",
  43. "cProfile",
  44. "calendar",
  45. "cgi",
  46. "cgitb",
  47. "chunk",
  48. "cmath",
  49. "cmd",
  50. "code",
  51. "codecs",
  52. "codeop",
  53. "collections",
  54. "colorsys",
  55. "compileall",
  56. "concurrent",
  57. "configparser",
  58. "contextlib",
  59. "contextvars",
  60. "copy",
  61. "copyreg",
  62. "crypt",
  63. "csv",
  64. "ctypes",
  65. "curses",
  66. "dataclasses",
  67. "datetime",
  68. "dbm",
  69. "decimal",
  70. "difflib",
  71. "dis",
  72. "distutils",
  73. "doctest",
  74. "dummy_threading",
  75. "email",
  76. "encodings",
  77. "ensurepip",
  78. "enum",
  79. "errno",
  80. "faulthandler",
  81. "fcntl",
  82. "filecmp",
  83. "fileinput",
  84. "fnmatch",
  85. "formatter",
  86. "fractions",
  87. "ftplib",
  88. "functools",
  89. "gc",
  90. "getopt",
  91. "getpass",
  92. "gettext",
  93. "glob",
  94. "grp",
  95. "gzip",
  96. "hashlib",
  97. "heapq",
  98. "hmac",
  99. "html",
  100. "http",
  101. "imaplib",
  102. "imghdr",
  103. "imp",
  104. "importlib",
  105. "inspect",
  106. "io",
  107. "ipaddress",
  108. "itertools",
  109. "json",
  110. "keyword",
  111. "lib2to3",
  112. "linecache",
  113. "locale",
  114. "logging",
  115. "lzma",
  116. "mailbox",
  117. "mailcap",
  118. "marshal",
  119. "math",
  120. "mimetypes",
  121. "mmap",
  122. "modulefinder",
  123. "msilib",
  124. "msvcrt",
  125. "multiprocessing",
  126. "netrc",
  127. "nis",
  128. "nntplib",
  129. "ntpath",
  130. "numbers",
  131. "operator",
  132. "optparse",
  133. "os",
  134. "ossaudiodev",
  135. "parser",
  136. "pathlib",
  137. "pdb",
  138. "pickle",
  139. "pickletools",
  140. "pipes",
  141. "pkgutil",
  142. "platform",
  143. "plistlib",
  144. "poplib",
  145. "posix",
  146. "posixpath",
  147. "pprint",
  148. "profile",
  149. "pstats",
  150. "pty",
  151. "pwd",
  152. "py_compile",
  153. "pyclbr",
  154. "pydoc",
  155. "queue",
  156. "quopri",
  157. "random",
  158. "re",
  159. "readline",
  160. "reprlib",
  161. "resource",
  162. "rlcompleter",
  163. "runpy",
  164. "sched",
  165. "secrets",
  166. "select",
  167. "selectors",
  168. "shelve",
  169. "shlex",
  170. "shutil",
  171. "signal",
  172. "site",
  173. "smtpd",
  174. "smtplib",
  175. "sndhdr",
  176. "socket",
  177. "socketserver",
  178. "spwd",
  179. "sqlite3",
  180. "sre",
  181. "sre_compile",
  182. "sre_constants",
  183. "sre_parse",
  184. "ssl",
  185. "stat",
  186. "statistics",
  187. "string",
  188. "stringprep",
  189. "struct",
  190. "subprocess",
  191. "sunau",
  192. "symbol",
  193. "symtable",
  194. "sys",
  195. "sysconfig",
  196. "syslog",
  197. "tabnanny",
  198. "tarfile",
  199. "telnetlib",
  200. "tempfile",
  201. "termios",
  202. "test",
  203. "textwrap",
  204. "threading",
  205. "time",
  206. "timeit",
  207. "tkinter",
  208. "token",
  209. "tokenize",
  210. "trace",
  211. "traceback",
  212. "tracemalloc",
  213. "tty",
  214. "turtle",
  215. "turtledemo",
  216. "types",
  217. "typing",
  218. "unicodedata",
  219. "unittest",
  220. "urllib",
  221. "uu",
  222. "uuid",
  223. "venv",
  224. "warnings",
  225. "wave",
  226. "weakref",
  227. "webbrowser",
  228. "winreg",
  229. "winsound",
  230. "wsgiref",
  231. "xdrlib",
  232. "xml",
  233. "xmlrpc",
  234. "zipapp",
  235. "zipfile",
  236. "zipimport",
  237. "zlib",
  238. }
  239. stdlib3_9 = {
  240. "_thread",
  241. "abc",
  242. "aifc",
  243. "argparse",
  244. "array",
  245. "ast",
  246. "asynchat",
  247. "asyncio",
  248. "asyncore",
  249. "atexit",
  250. "audioop",
  251. "base64",
  252. "bdb",
  253. "binascii",
  254. "binhex",
  255. "bisect",
  256. "builtins",
  257. "bz2",
  258. "cProfile",
  259. "calendar",
  260. "cgi",
  261. "cgitb",
  262. "chunk",
  263. "cmath",
  264. "cmd",
  265. "code",
  266. "codecs",
  267. "codeop",
  268. "collections",
  269. "colorsys",
  270. "compileall",
  271. "concurrent",
  272. "configparser",
  273. "contextlib",
  274. "contextvars",
  275. "copy",
  276. "copyreg",
  277. "crypt",
  278. "csv",
  279. "ctypes",
  280. "curses",
  281. "dataclasses",
  282. "datetime",
  283. "dbm",
  284. "decimal",
  285. "difflib",
  286. "dis",
  287. "distutils",
  288. "doctest",
  289. "email",
  290. "encodings",
  291. "ensurepip",
  292. "enum",
  293. "errno",
  294. "faulthandler",
  295. "fcntl",
  296. "filecmp",
  297. "fileinput",
  298. "fnmatch",
  299. "formatter",
  300. "fractions",
  301. "ftplib",
  302. "functools",
  303. "gc",
  304. "getopt",
  305. "getpass",
  306. "gettext",
  307. "glob",
  308. "graphlib",
  309. "grp",
  310. "gzip",
  311. "hashlib",
  312. "heapq",
  313. "hmac",
  314. "html",
  315. "http",
  316. "imaplib",
  317. "imghdr",
  318. "imp",
  319. "importlib",
  320. "inspect",
  321. "io",
  322. "ipaddress",
  323. "itertools",
  324. "json",
  325. "keyword",
  326. "lib2to3",
  327. "linecache",
  328. "locale",
  329. "logging",
  330. "lzma",
  331. "mailbox",
  332. "mailcap",
  333. "marshal",
  334. "math",
  335. "mimetypes",
  336. "mmap",
  337. "modulefinder",
  338. "msilib",
  339. "msvcrt",
  340. "multiprocessing",
  341. "netrc",
  342. "nis",
  343. "nntplib",
  344. "ntpath",
  345. "numbers",
  346. "operator",
  347. "optparse",
  348. "os",
  349. "ossaudiodev",
  350. "parser",
  351. "pathlib",
  352. "pdb",
  353. "pickle",
  354. "pickletools",
  355. "pipes",
  356. "pkgutil",
  357. "platform",
  358. "plistlib",
  359. "poplib",
  360. "posix",
  361. "posixpath",
  362. "pprint",
  363. "profile",
  364. "pstats",
  365. "pty",
  366. "pwd",
  367. "py_compile",
  368. "pyclbr",
  369. "pydoc",
  370. "queue",
  371. "quopri",
  372. "random",
  373. "re",
  374. "readline",
  375. "reprlib",
  376. "resource",
  377. "rlcompleter",
  378. "runpy",
  379. "sched",
  380. "secrets",
  381. "select",
  382. "selectors",
  383. "shelve",
  384. "shlex",
  385. "shutil",
  386. "signal",
  387. "site",
  388. "smtpd",
  389. "smtplib",
  390. "sndhdr",
  391. "socket",
  392. "socketserver",
  393. "spwd",
  394. "sqlite3",
  395. "sre",
  396. "sre_compile",
  397. "sre_constants",
  398. "sre_parse",
  399. "ssl",
  400. "stat",
  401. "statistics",
  402. "string",
  403. "stringprep",
  404. "struct",
  405. "subprocess",
  406. "sunau",
  407. "symbol",
  408. "symtable",
  409. "sys",
  410. "sysconfig",
  411. "syslog",
  412. "tabnanny",
  413. "tarfile",
  414. "telnetlib",
  415. "tempfile",
  416. "termios",
  417. "test",
  418. "textwrap",
  419. "threading",
  420. "time",
  421. "timeit",
  422. "tkinter",
  423. "token",
  424. "tokenize",
  425. "trace",
  426. "traceback",
  427. "tracemalloc",
  428. "tty",
  429. "turtle",
  430. "turtledemo",
  431. "types",
  432. "typing",
  433. "unicodedata",
  434. "unittest",
  435. "urllib",
  436. "uu",
  437. "uuid",
  438. "venv",
  439. "warnings",
  440. "wave",
  441. "weakref",
  442. "webbrowser",
  443. "winreg",
  444. "winsound",
  445. "wsgiref",
  446. "xdrlib",
  447. "xml",
  448. "xmlrpc",
  449. "zipapp",
  450. "zipfile",
  451. "zipimport",
  452. "zlib",
  453. "zoneinfo",
  454. }