mypy-dev 1.9.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1018) hide show
  1. mypy/__init__.py +1 -0
  2. mypy/__main__.py +37 -0
  3. mypy/api.py +94 -0
  4. mypy/applytype.py +156 -0
  5. mypy/argmap.py +268 -0
  6. mypy/binder.py +538 -0
  7. mypy/bogus_type.py +27 -0
  8. mypy/build.py +3562 -0
  9. mypy/checker.py +8372 -0
  10. mypy/checkexpr.py +6615 -0
  11. mypy/checkmember.py +1356 -0
  12. mypy/checkpattern.py +801 -0
  13. mypy/checkstrformat.py +1109 -0
  14. mypy/config_parser.py +670 -0
  15. mypy/constant_fold.py +187 -0
  16. mypy/constraints.py +1624 -0
  17. mypy/copytype.py +133 -0
  18. mypy/defaults.py +46 -0
  19. mypy/dmypy/__init__.py +0 -0
  20. mypy/dmypy/__main__.py +6 -0
  21. mypy/dmypy/client.py +749 -0
  22. mypy/dmypy_os.py +42 -0
  23. mypy/dmypy_server.py +1107 -0
  24. mypy/dmypy_util.py +117 -0
  25. mypy/erasetype.py +278 -0
  26. mypy/errorcodes.py +285 -0
  27. mypy/errors.py +1280 -0
  28. mypy/evalexpr.py +205 -0
  29. mypy/expandtype.py +510 -0
  30. mypy/exprtotype.py +206 -0
  31. mypy/fastparse.py +2147 -0
  32. mypy/find_sources.py +243 -0
  33. mypy/fixup.py +426 -0
  34. mypy/freetree.py +23 -0
  35. mypy/fscache.py +309 -0
  36. mypy/fswatcher.py +106 -0
  37. mypy/gclogger.py +47 -0
  38. mypy/git.py +34 -0
  39. mypy/graph_utils.py +112 -0
  40. mypy/indirection.py +121 -0
  41. mypy/infer.py +75 -0
  42. mypy/inspections.py +627 -0
  43. mypy/ipc.py +310 -0
  44. mypy/join.py +866 -0
  45. mypy/literals.py +306 -0
  46. mypy/lookup.py +61 -0
  47. mypy/main.py +1574 -0
  48. mypy/maptype.py +106 -0
  49. mypy/meet.py +1140 -0
  50. mypy/memprofile.py +121 -0
  51. mypy/message_registry.py +326 -0
  52. mypy/messages.py +3171 -0
  53. mypy/metastore.py +225 -0
  54. mypy/mixedtraverser.py +112 -0
  55. mypy/modulefinder.py +875 -0
  56. mypy/moduleinspect.py +184 -0
  57. mypy/mro.py +62 -0
  58. mypy/nodes.py +4112 -0
  59. mypy/operators.py +126 -0
  60. mypy/options.py +556 -0
  61. mypy/parse.py +30 -0
  62. mypy/partially_defined.py +675 -0
  63. mypy/patterns.py +150 -0
  64. mypy/plugin.py +901 -0
  65. mypy/plugins/__init__.py +0 -0
  66. mypy/plugins/attrs.py +1152 -0
  67. mypy/plugins/common.py +440 -0
  68. mypy/plugins/ctypes.py +245 -0
  69. mypy/plugins/dataclasses.py +1101 -0
  70. mypy/plugins/default.py +518 -0
  71. mypy/plugins/enums.py +259 -0
  72. mypy/plugins/functools.py +104 -0
  73. mypy/plugins/proper_plugin.py +175 -0
  74. mypy/plugins/singledispatch.py +224 -0
  75. mypy/py.typed +1 -0
  76. mypy/pyinfo.py +78 -0
  77. mypy/reachability.py +362 -0
  78. mypy/refinfo.py +92 -0
  79. mypy/renaming.py +568 -0
  80. mypy/report.py +924 -0
  81. mypy/scope.py +125 -0
  82. mypy/semanal.py +7023 -0
  83. mypy/semanal_classprop.py +187 -0
  84. mypy/semanal_enum.py +253 -0
  85. mypy/semanal_infer.py +128 -0
  86. mypy/semanal_main.py +511 -0
  87. mypy/semanal_namedtuple.py +670 -0
  88. mypy/semanal_newtype.py +273 -0
  89. mypy/semanal_pass1.py +156 -0
  90. mypy/semanal_shared.py +490 -0
  91. mypy/semanal_typeargs.py +265 -0
  92. mypy/semanal_typeddict.py +575 -0
  93. mypy/server/__init__.py +0 -0
  94. mypy/server/astdiff.py +518 -0
  95. mypy/server/astmerge.py +562 -0
  96. mypy/server/aststrip.py +281 -0
  97. mypy/server/deps.py +1137 -0
  98. mypy/server/mergecheck.py +83 -0
  99. mypy/server/objgraph.py +101 -0
  100. mypy/server/subexpr.py +198 -0
  101. mypy/server/target.py +11 -0
  102. mypy/server/trigger.py +26 -0
  103. mypy/server/update.py +1339 -0
  104. mypy/sharedparse.py +112 -0
  105. mypy/solve.py +562 -0
  106. mypy/split_namespace.py +35 -0
  107. mypy/state.py +28 -0
  108. mypy/stats.py +489 -0
  109. mypy/strconv.py +641 -0
  110. mypy/stubdoc.py +491 -0
  111. mypy/stubgen.py +1873 -0
  112. mypy/stubgenc.py +993 -0
  113. mypy/stubinfo.py +173 -0
  114. mypy/stubtest.py +2079 -0
  115. mypy/stubutil.py +807 -0
  116. mypy/subtypes.py +1967 -0
  117. mypy/suggestions.py +1046 -0
  118. mypy/test/__init__.py +0 -0
  119. mypy/test/config.py +28 -0
  120. mypy/test/data.py +823 -0
  121. mypy/test/helpers.py +476 -0
  122. mypy/test/meta/__init__.py +0 -0
  123. mypy/test/meta/_pytest.py +72 -0
  124. mypy/test/meta/test_diff_helper.py +47 -0
  125. mypy/test/meta/test_parse_data.py +73 -0
  126. mypy/test/meta/test_update_data.py +135 -0
  127. mypy/test/test_find_sources.py +376 -0
  128. mypy/test/test_ref_info.py +45 -0
  129. mypy/test/testapi.py +45 -0
  130. mypy/test/testargs.py +77 -0
  131. mypy/test/testcheck.py +322 -0
  132. mypy/test/testcmdline.py +152 -0
  133. mypy/test/testconstraints.py +134 -0
  134. mypy/test/testdaemon.py +132 -0
  135. mypy/test/testdeps.py +77 -0
  136. mypy/test/testdiff.py +67 -0
  137. mypy/test/testerrorstream.py +46 -0
  138. mypy/test/testfinegrained.py +438 -0
  139. mypy/test/testfinegrainedcache.py +18 -0
  140. mypy/test/testformatter.py +85 -0
  141. mypy/test/testfscache.py +101 -0
  142. mypy/test/testgraph.py +83 -0
  143. mypy/test/testinfer.py +373 -0
  144. mypy/test/testipc.py +119 -0
  145. mypy/test/testmerge.py +238 -0
  146. mypy/test/testmodulefinder.py +278 -0
  147. mypy/test/testmypyc.py +14 -0
  148. mypy/test/testparse.py +107 -0
  149. mypy/test/testpep561.py +211 -0
  150. mypy/test/testpythoneval.py +117 -0
  151. mypy/test/testreports.py +55 -0
  152. mypy/test/testsemanal.py +209 -0
  153. mypy/test/testsolve.py +285 -0
  154. mypy/test/teststubgen.py +1412 -0
  155. mypy/test/teststubinfo.py +12 -0
  156. mypy/test/teststubtest.py +2492 -0
  157. mypy/test/testsubtypes.py +303 -0
  158. mypy/test/testtransform.py +64 -0
  159. mypy/test/testtypegen.py +83 -0
  160. mypy/test/testtypes.py +1551 -0
  161. mypy/test/testutil.py +111 -0
  162. mypy/test/typefixture.py +415 -0
  163. mypy/test/update_data.py +87 -0
  164. mypy/test/visitors.py +63 -0
  165. mypy/traverser.py +961 -0
  166. mypy/treetransform.py +800 -0
  167. mypy/tvar_scope.py +147 -0
  168. mypy/type_visitor.py +564 -0
  169. mypy/typeanal.py +2510 -0
  170. mypy/typeops.py +1082 -0
  171. mypy/types.py +3686 -0
  172. mypy/types_utils.py +166 -0
  173. mypy/typeshed/LICENSE +237 -0
  174. mypy/typeshed/stdlib/VERSIONS +308 -0
  175. mypy/typeshed/stdlib/__future__.pyi +36 -0
  176. mypy/typeshed/stdlib/__main__.pyi +3 -0
  177. mypy/typeshed/stdlib/_ast.pyi +587 -0
  178. mypy/typeshed/stdlib/_bisect.pyi +84 -0
  179. mypy/typeshed/stdlib/_bootlocale.pyi +1 -0
  180. mypy/typeshed/stdlib/_codecs.pyi +133 -0
  181. mypy/typeshed/stdlib/_collections_abc.pyi +94 -0
  182. mypy/typeshed/stdlib/_compat_pickle.pyi +8 -0
  183. mypy/typeshed/stdlib/_compression.pyi +25 -0
  184. mypy/typeshed/stdlib/_csv.pyi +90 -0
  185. mypy/typeshed/stdlib/_ctypes.pyi +205 -0
  186. mypy/typeshed/stdlib/_curses.pyi +557 -0
  187. mypy/typeshed/stdlib/_decimal.pyi +281 -0
  188. mypy/typeshed/stdlib/_dummy_thread.pyi +27 -0
  189. mypy/typeshed/stdlib/_dummy_threading.pyi +164 -0
  190. mypy/typeshed/stdlib/_heapq.pyi +11 -0
  191. mypy/typeshed/stdlib/_imp.pyi +28 -0
  192. mypy/typeshed/stdlib/_json.pyi +49 -0
  193. mypy/typeshed/stdlib/_locale.pyi +100 -0
  194. mypy/typeshed/stdlib/_markupbase.pyi +16 -0
  195. mypy/typeshed/stdlib/_msi.pyi +91 -0
  196. mypy/typeshed/stdlib/_operator.pyi +147 -0
  197. mypy/typeshed/stdlib/_osx_support.pyi +34 -0
  198. mypy/typeshed/stdlib/_posixsubprocess.pyi +32 -0
  199. mypy/typeshed/stdlib/_py_abc.pyi +14 -0
  200. mypy/typeshed/stdlib/_pydecimal.pyi +43 -0
  201. mypy/typeshed/stdlib/_random.pyi +12 -0
  202. mypy/typeshed/stdlib/_sitebuiltins.pyi +16 -0
  203. mypy/typeshed/stdlib/_socket.pyi +802 -0
  204. mypy/typeshed/stdlib/_stat.pyi +103 -0
  205. mypy/typeshed/stdlib/_thread.pyi +52 -0
  206. mypy/typeshed/stdlib/_threading_local.pyi +17 -0
  207. mypy/typeshed/stdlib/_tkinter.pyi +120 -0
  208. mypy/typeshed/stdlib/_tracemalloc.pyi +17 -0
  209. mypy/typeshed/stdlib/_typeshed/__init__.pyi +347 -0
  210. mypy/typeshed/stdlib/_typeshed/dbapi.pyi +37 -0
  211. mypy/typeshed/stdlib/_typeshed/wsgi.pyi +44 -0
  212. mypy/typeshed/stdlib/_typeshed/xml.pyi +9 -0
  213. mypy/typeshed/stdlib/_warnings.pyi +55 -0
  214. mypy/typeshed/stdlib/_weakref.pyi +41 -0
  215. mypy/typeshed/stdlib/_weakrefset.pyi +51 -0
  216. mypy/typeshed/stdlib/_winapi.pyi +251 -0
  217. mypy/typeshed/stdlib/abc.pyi +49 -0
  218. mypy/typeshed/stdlib/aifc.pyi +91 -0
  219. mypy/typeshed/stdlib/antigravity.pyi +3 -0
  220. mypy/typeshed/stdlib/argparse.pyi +571 -0
  221. mypy/typeshed/stdlib/array.pyi +92 -0
  222. mypy/typeshed/stdlib/ast.pyi +277 -0
  223. mypy/typeshed/stdlib/asynchat.pyi +21 -0
  224. mypy/typeshed/stdlib/asyncio/__init__.pyi +41 -0
  225. mypy/typeshed/stdlib/asyncio/base_events.pyi +440 -0
  226. mypy/typeshed/stdlib/asyncio/base_futures.pyi +19 -0
  227. mypy/typeshed/stdlib/asyncio/base_subprocess.pyi +63 -0
  228. mypy/typeshed/stdlib/asyncio/base_tasks.pyi +9 -0
  229. mypy/typeshed/stdlib/asyncio/constants.pyi +20 -0
  230. mypy/typeshed/stdlib/asyncio/coroutines.pyi +26 -0
  231. mypy/typeshed/stdlib/asyncio/events.pyi +574 -0
  232. mypy/typeshed/stdlib/asyncio/exceptions.pyi +43 -0
  233. mypy/typeshed/stdlib/asyncio/format_helpers.pyi +20 -0
  234. mypy/typeshed/stdlib/asyncio/futures.pyi +57 -0
  235. mypy/typeshed/stdlib/asyncio/locks.pyi +121 -0
  236. mypy/typeshed/stdlib/asyncio/log.pyi +3 -0
  237. mypy/typeshed/stdlib/asyncio/mixins.pyi +9 -0
  238. mypy/typeshed/stdlib/asyncio/proactor_events.pyi +64 -0
  239. mypy/typeshed/stdlib/asyncio/protocols.pyi +34 -0
  240. mypy/typeshed/stdlib/asyncio/queues.pyi +47 -0
  241. mypy/typeshed/stdlib/asyncio/runners.pyi +32 -0
  242. mypy/typeshed/stdlib/asyncio/selector_events.pyi +8 -0
  243. mypy/typeshed/stdlib/asyncio/sslproto.pyi +163 -0
  244. mypy/typeshed/stdlib/asyncio/staggered.pyi +10 -0
  245. mypy/typeshed/stdlib/asyncio/streams.pyi +148 -0
  246. mypy/typeshed/stdlib/asyncio/subprocess.pyi +229 -0
  247. mypy/typeshed/stdlib/asyncio/taskgroups.pyi +25 -0
  248. mypy/typeshed/stdlib/asyncio/tasks.pyi +477 -0
  249. mypy/typeshed/stdlib/asyncio/threads.pyi +9 -0
  250. mypy/typeshed/stdlib/asyncio/timeouts.pyi +19 -0
  251. mypy/typeshed/stdlib/asyncio/transports.pyi +47 -0
  252. mypy/typeshed/stdlib/asyncio/trsock.pyi +98 -0
  253. mypy/typeshed/stdlib/asyncio/unix_events.pyi +173 -0
  254. mypy/typeshed/stdlib/asyncio/windows_events.pyi +83 -0
  255. mypy/typeshed/stdlib/asyncio/windows_utils.pyi +49 -0
  256. mypy/typeshed/stdlib/asyncore.pyi +90 -0
  257. mypy/typeshed/stdlib/atexit.pyi +12 -0
  258. mypy/typeshed/stdlib/audioop.pyi +42 -0
  259. mypy/typeshed/stdlib/base64.pyi +59 -0
  260. mypy/typeshed/stdlib/bdb.pyi +102 -0
  261. mypy/typeshed/stdlib/binascii.pyi +36 -0
  262. mypy/typeshed/stdlib/binhex.pyi +45 -0
  263. mypy/typeshed/stdlib/bisect.pyi +4 -0
  264. mypy/typeshed/stdlib/builtins.pyi +1937 -0
  265. mypy/typeshed/stdlib/bz2.pyi +146 -0
  266. mypy/typeshed/stdlib/cProfile.pyi +35 -0
  267. mypy/typeshed/stdlib/calendar.pyi +206 -0
  268. mypy/typeshed/stdlib/cgi.pyi +118 -0
  269. mypy/typeshed/stdlib/cgitb.pyi +32 -0
  270. mypy/typeshed/stdlib/chunk.pyi +20 -0
  271. mypy/typeshed/stdlib/cmath.pyi +36 -0
  272. mypy/typeshed/stdlib/cmd.pyi +45 -0
  273. mypy/typeshed/stdlib/code.pyi +33 -0
  274. mypy/typeshed/stdlib/codecs.pyi +285 -0
  275. mypy/typeshed/stdlib/codeop.pyi +13 -0
  276. mypy/typeshed/stdlib/collections/__init__.pyi +477 -0
  277. mypy/typeshed/stdlib/collections/abc.pyi +2 -0
  278. mypy/typeshed/stdlib/colorsys.pyi +13 -0
  279. mypy/typeshed/stdlib/compileall.pyi +111 -0
  280. mypy/typeshed/stdlib/concurrent/__init__.pyi +0 -0
  281. mypy/typeshed/stdlib/concurrent/futures/__init__.pyi +32 -0
  282. mypy/typeshed/stdlib/concurrent/futures/_base.pyi +120 -0
  283. mypy/typeshed/stdlib/concurrent/futures/process.pyi +233 -0
  284. mypy/typeshed/stdlib/concurrent/futures/thread.pyi +80 -0
  285. mypy/typeshed/stdlib/configparser.pyi +313 -0
  286. mypy/typeshed/stdlib/contextlib.pyi +207 -0
  287. mypy/typeshed/stdlib/contextvars.pyi +63 -0
  288. mypy/typeshed/stdlib/copy.pyi +16 -0
  289. mypy/typeshed/stdlib/copyreg.pyi +21 -0
  290. mypy/typeshed/stdlib/crypt.pyi +12 -0
  291. mypy/typeshed/stdlib/csv.pyi +147 -0
  292. mypy/typeshed/stdlib/ctypes/__init__.pyi +187 -0
  293. mypy/typeshed/stdlib/ctypes/_endian.pyi +19 -0
  294. mypy/typeshed/stdlib/ctypes/util.pyi +6 -0
  295. mypy/typeshed/stdlib/ctypes/wintypes.pyi +298 -0
  296. mypy/typeshed/stdlib/curses/__init__.pyi +21 -0
  297. mypy/typeshed/stdlib/curses/ascii.pyi +63 -0
  298. mypy/typeshed/stdlib/curses/has_key.pyi +4 -0
  299. mypy/typeshed/stdlib/curses/panel.pyi +25 -0
  300. mypy/typeshed/stdlib/curses/textpad.pyi +13 -0
  301. mypy/typeshed/stdlib/dataclasses.pyi +313 -0
  302. mypy/typeshed/stdlib/datetime.pyi +295 -0
  303. mypy/typeshed/stdlib/dbm/__init__.pyi +95 -0
  304. mypy/typeshed/stdlib/dbm/dumb.pyi +31 -0
  305. mypy/typeshed/stdlib/dbm/gnu.pyi +40 -0
  306. mypy/typeshed/stdlib/dbm/ndbm.pyi +36 -0
  307. mypy/typeshed/stdlib/decimal.pyi +2 -0
  308. mypy/typeshed/stdlib/difflib.pyi +140 -0
  309. mypy/typeshed/stdlib/dis.pyi +144 -0
  310. mypy/typeshed/stdlib/distutils/__init__.pyi +5 -0
  311. mypy/typeshed/stdlib/distutils/archive_util.pyi +20 -0
  312. mypy/typeshed/stdlib/distutils/bcppcompiler.pyi +3 -0
  313. mypy/typeshed/stdlib/distutils/ccompiler.pyi +152 -0
  314. mypy/typeshed/stdlib/distutils/cmd.pyi +66 -0
  315. mypy/typeshed/stdlib/distutils/command/__init__.pyi +0 -0
  316. mypy/typeshed/stdlib/distutils/command/bdist.pyi +25 -0
  317. mypy/typeshed/stdlib/distutils/command/bdist_dumb.pyi +21 -0
  318. mypy/typeshed/stdlib/distutils/command/bdist_msi.pyi +45 -0
  319. mypy/typeshed/stdlib/distutils/command/bdist_packager.pyi +0 -0
  320. mypy/typeshed/stdlib/distutils/command/bdist_rpm.pyi +52 -0
  321. mypy/typeshed/stdlib/distutils/command/bdist_wininst.pyi +16 -0
  322. mypy/typeshed/stdlib/distutils/command/build.pyi +31 -0
  323. mypy/typeshed/stdlib/distutils/command/build_clib.pyi +27 -0
  324. mypy/typeshed/stdlib/distutils/command/build_ext.pyi +50 -0
  325. mypy/typeshed/stdlib/distutils/command/build_py.pyi +44 -0
  326. mypy/typeshed/stdlib/distutils/command/build_scripts.pyi +24 -0
  327. mypy/typeshed/stdlib/distutils/command/check.pyi +39 -0
  328. mypy/typeshed/stdlib/distutils/command/clean.pyi +17 -0
  329. mypy/typeshed/stdlib/distutils/command/config.pyi +83 -0
  330. mypy/typeshed/stdlib/distutils/command/install.pyi +63 -0
  331. mypy/typeshed/stdlib/distutils/command/install_data.pyi +19 -0
  332. mypy/typeshed/stdlib/distutils/command/install_egg_info.pyi +18 -0
  333. mypy/typeshed/stdlib/distutils/command/install_headers.pyi +16 -0
  334. mypy/typeshed/stdlib/distutils/command/install_lib.pyi +25 -0
  335. mypy/typeshed/stdlib/distutils/command/install_scripts.pyi +18 -0
  336. mypy/typeshed/stdlib/distutils/command/register.pyi +18 -0
  337. mypy/typeshed/stdlib/distutils/command/sdist.pyi +42 -0
  338. mypy/typeshed/stdlib/distutils/command/upload.pyi +17 -0
  339. mypy/typeshed/stdlib/distutils/config.pyi +17 -0
  340. mypy/typeshed/stdlib/distutils/core.pyi +57 -0
  341. mypy/typeshed/stdlib/distutils/cygwinccompiler.pyi +20 -0
  342. mypy/typeshed/stdlib/distutils/debug.pyi +1 -0
  343. mypy/typeshed/stdlib/distutils/dep_util.pyi +3 -0
  344. mypy/typeshed/stdlib/distutils/dir_util.pyi +13 -0
  345. mypy/typeshed/stdlib/distutils/dist.pyi +146 -0
  346. mypy/typeshed/stdlib/distutils/errors.pyi +19 -0
  347. mypy/typeshed/stdlib/distutils/extension.pyi +36 -0
  348. mypy/typeshed/stdlib/distutils/fancy_getopt.pyi +34 -0
  349. mypy/typeshed/stdlib/distutils/file_util.pyi +14 -0
  350. mypy/typeshed/stdlib/distutils/filelist.pyi +50 -0
  351. mypy/typeshed/stdlib/distutils/log.pyi +25 -0
  352. mypy/typeshed/stdlib/distutils/msvccompiler.pyi +3 -0
  353. mypy/typeshed/stdlib/distutils/spawn.pyi +2 -0
  354. mypy/typeshed/stdlib/distutils/sysconfig.pyi +22 -0
  355. mypy/typeshed/stdlib/distutils/text_file.pyi +21 -0
  356. mypy/typeshed/stdlib/distutils/unixccompiler.pyi +3 -0
  357. mypy/typeshed/stdlib/distutils/util.pyi +46 -0
  358. mypy/typeshed/stdlib/distutils/version.pyi +36 -0
  359. mypy/typeshed/stdlib/doctest.pyi +248 -0
  360. mypy/typeshed/stdlib/dummy_threading.pyi +2 -0
  361. mypy/typeshed/stdlib/email/__init__.pyi +29 -0
  362. mypy/typeshed/stdlib/email/_header_value_parser.pyi +392 -0
  363. mypy/typeshed/stdlib/email/_policybase.pyi +51 -0
  364. mypy/typeshed/stdlib/email/base64mime.pyi +13 -0
  365. mypy/typeshed/stdlib/email/charset.pyi +34 -0
  366. mypy/typeshed/stdlib/email/contentmanager.pyi +11 -0
  367. mypy/typeshed/stdlib/email/encoders.pyi +8 -0
  368. mypy/typeshed/stdlib/email/errors.pyi +39 -0
  369. mypy/typeshed/stdlib/email/feedparser.pyi +23 -0
  370. mypy/typeshed/stdlib/email/generator.pyi +40 -0
  371. mypy/typeshed/stdlib/email/header.pyi +31 -0
  372. mypy/typeshed/stdlib/email/headerregistry.pyi +178 -0
  373. mypy/typeshed/stdlib/email/iterators.pyi +12 -0
  374. mypy/typeshed/stdlib/email/message.pyi +161 -0
  375. mypy/typeshed/stdlib/email/mime/__init__.pyi +0 -0
  376. mypy/typeshed/stdlib/email/mime/application.pyi +17 -0
  377. mypy/typeshed/stdlib/email/mime/audio.pyi +17 -0
  378. mypy/typeshed/stdlib/email/mime/base.pyi +8 -0
  379. mypy/typeshed/stdlib/email/mime/image.pyi +17 -0
  380. mypy/typeshed/stdlib/email/mime/message.pyi +8 -0
  381. mypy/typeshed/stdlib/email/mime/multipart.pyi +18 -0
  382. mypy/typeshed/stdlib/email/mime/nonmultipart.pyi +5 -0
  383. mypy/typeshed/stdlib/email/mime/text.pyi +9 -0
  384. mypy/typeshed/stdlib/email/parser.pyi +26 -0
  385. mypy/typeshed/stdlib/email/policy.pyi +38 -0
  386. mypy/typeshed/stdlib/email/quoprimime.pyi +28 -0
  387. mypy/typeshed/stdlib/email/utils.pyi +66 -0
  388. mypy/typeshed/stdlib/encodings/__init__.pyi +10 -0
  389. mypy/typeshed/stdlib/encodings/utf_8.pyi +21 -0
  390. mypy/typeshed/stdlib/encodings/utf_8_sig.pyi +22 -0
  391. mypy/typeshed/stdlib/ensurepip/__init__.pyi +12 -0
  392. mypy/typeshed/stdlib/enum.pyi +317 -0
  393. mypy/typeshed/stdlib/errno.pyi +222 -0
  394. mypy/typeshed/stdlib/faulthandler.pyi +13 -0
  395. mypy/typeshed/stdlib/fcntl.pyi +127 -0
  396. mypy/typeshed/stdlib/filecmp.pyi +57 -0
  397. mypy/typeshed/stdlib/fileinput.pyi +213 -0
  398. mypy/typeshed/stdlib/fnmatch.pyi +9 -0
  399. mypy/typeshed/stdlib/formatter.pyi +88 -0
  400. mypy/typeshed/stdlib/fractions.pyi +150 -0
  401. mypy/typeshed/stdlib/ftplib.pyi +178 -0
  402. mypy/typeshed/stdlib/functools.pyi +200 -0
  403. mypy/typeshed/stdlib/gc.pyi +37 -0
  404. mypy/typeshed/stdlib/genericpath.pyi +52 -0
  405. mypy/typeshed/stdlib/getopt.pyi +11 -0
  406. mypy/typeshed/stdlib/getpass.pyi +8 -0
  407. mypy/typeshed/stdlib/gettext.pyi +169 -0
  408. mypy/typeshed/stdlib/glob.pyi +42 -0
  409. mypy/typeshed/stdlib/graphlib.pyi +28 -0
  410. mypy/typeshed/stdlib/grp.pyi +21 -0
  411. mypy/typeshed/stdlib/gzip.pyi +160 -0
  412. mypy/typeshed/stdlib/hashlib.pyi +165 -0
  413. mypy/typeshed/stdlib/heapq.pyi +17 -0
  414. mypy/typeshed/stdlib/hmac.pyi +38 -0
  415. mypy/typeshed/stdlib/html/__init__.pyi +6 -0
  416. mypy/typeshed/stdlib/html/entities.pyi +6 -0
  417. mypy/typeshed/stdlib/html/parser.pyi +34 -0
  418. mypy/typeshed/stdlib/http/__init__.pyi +105 -0
  419. mypy/typeshed/stdlib/http/client.pyi +259 -0
  420. mypy/typeshed/stdlib/http/cookiejar.pyi +159 -0
  421. mypy/typeshed/stdlib/http/cookies.pyi +60 -0
  422. mypy/typeshed/stdlib/http/server.pyi +83 -0
  423. mypy/typeshed/stdlib/imaplib.pyi +168 -0
  424. mypy/typeshed/stdlib/imghdr.pyi +17 -0
  425. mypy/typeshed/stdlib/imp.pyi +62 -0
  426. mypy/typeshed/stdlib/importlib/__init__.pyi +24 -0
  427. mypy/typeshed/stdlib/importlib/_abc.pyi +15 -0
  428. mypy/typeshed/stdlib/importlib/abc.pyi +172 -0
  429. mypy/typeshed/stdlib/importlib/machinery.pyi +179 -0
  430. mypy/typeshed/stdlib/importlib/metadata/__init__.pyi +276 -0
  431. mypy/typeshed/stdlib/importlib/metadata/_meta.pyi +40 -0
  432. mypy/typeshed/stdlib/importlib/readers.pyi +68 -0
  433. mypy/typeshed/stdlib/importlib/resources/__init__.pyi +46 -0
  434. mypy/typeshed/stdlib/importlib/resources/abc.pyi +12 -0
  435. mypy/typeshed/stdlib/importlib/resources/readers.pyi +14 -0
  436. mypy/typeshed/stdlib/importlib/resources/simple.pyi +49 -0
  437. mypy/typeshed/stdlib/importlib/simple.pyi +11 -0
  438. mypy/typeshed/stdlib/importlib/util.pyi +43 -0
  439. mypy/typeshed/stdlib/inspect.pyi +631 -0
  440. mypy/typeshed/stdlib/io.pyi +204 -0
  441. mypy/typeshed/stdlib/ipaddress.pyi +208 -0
  442. mypy/typeshed/stdlib/itertools.pyi +293 -0
  443. mypy/typeshed/stdlib/json/__init__.pyi +61 -0
  444. mypy/typeshed/stdlib/json/decoder.pyi +32 -0
  445. mypy/typeshed/stdlib/json/encoder.pyi +38 -0
  446. mypy/typeshed/stdlib/json/tool.pyi +1 -0
  447. mypy/typeshed/stdlib/keyword.pyi +21 -0
  448. mypy/typeshed/stdlib/lib2to3/__init__.pyi +0 -0
  449. mypy/typeshed/stdlib/lib2to3/btm_matcher.pyi +28 -0
  450. mypy/typeshed/stdlib/lib2to3/fixer_base.pyi +42 -0
  451. mypy/typeshed/stdlib/lib2to3/fixes/__init__.pyi +0 -0
  452. mypy/typeshed/stdlib/lib2to3/fixes/fix_apply.pyi +8 -0
  453. mypy/typeshed/stdlib/lib2to3/fixes/fix_asserts.pyi +10 -0
  454. mypy/typeshed/stdlib/lib2to3/fixes/fix_basestring.pyi +8 -0
  455. mypy/typeshed/stdlib/lib2to3/fixes/fix_buffer.pyi +8 -0
  456. mypy/typeshed/stdlib/lib2to3/fixes/fix_dict.pyi +16 -0
  457. mypy/typeshed/stdlib/lib2to3/fixes/fix_except.pyi +14 -0
  458. mypy/typeshed/stdlib/lib2to3/fixes/fix_exec.pyi +8 -0
  459. mypy/typeshed/stdlib/lib2to3/fixes/fix_execfile.pyi +8 -0
  460. mypy/typeshed/stdlib/lib2to3/fixes/fix_exitfunc.pyi +13 -0
  461. mypy/typeshed/stdlib/lib2to3/fixes/fix_filter.pyi +9 -0
  462. mypy/typeshed/stdlib/lib2to3/fixes/fix_funcattrs.pyi +8 -0
  463. mypy/typeshed/stdlib/lib2to3/fixes/fix_future.pyi +8 -0
  464. mypy/typeshed/stdlib/lib2to3/fixes/fix_getcwdu.pyi +8 -0
  465. mypy/typeshed/stdlib/lib2to3/fixes/fix_has_key.pyi +8 -0
  466. mypy/typeshed/stdlib/lib2to3/fixes/fix_idioms.pyi +15 -0
  467. mypy/typeshed/stdlib/lib2to3/fixes/fix_import.pyi +16 -0
  468. mypy/typeshed/stdlib/lib2to3/fixes/fix_imports.pyi +21 -0
  469. mypy/typeshed/stdlib/lib2to3/fixes/fix_imports2.pyi +6 -0
  470. mypy/typeshed/stdlib/lib2to3/fixes/fix_input.pyi +11 -0
  471. mypy/typeshed/stdlib/lib2to3/fixes/fix_intern.pyi +9 -0
  472. mypy/typeshed/stdlib/lib2to3/fixes/fix_isinstance.pyi +8 -0
  473. mypy/typeshed/stdlib/lib2to3/fixes/fix_itertools.pyi +9 -0
  474. mypy/typeshed/stdlib/lib2to3/fixes/fix_itertools_imports.pyi +7 -0
  475. mypy/typeshed/stdlib/lib2to3/fixes/fix_long.pyi +7 -0
  476. mypy/typeshed/stdlib/lib2to3/fixes/fix_map.pyi +9 -0
  477. mypy/typeshed/stdlib/lib2to3/fixes/fix_metaclass.pyi +17 -0
  478. mypy/typeshed/stdlib/lib2to3/fixes/fix_methodattrs.pyi +10 -0
  479. mypy/typeshed/stdlib/lib2to3/fixes/fix_ne.pyi +8 -0
  480. mypy/typeshed/stdlib/lib2to3/fixes/fix_next.pyi +19 -0
  481. mypy/typeshed/stdlib/lib2to3/fixes/fix_nonzero.pyi +8 -0
  482. mypy/typeshed/stdlib/lib2to3/fixes/fix_numliterals.pyi +8 -0
  483. mypy/typeshed/stdlib/lib2to3/fixes/fix_operator.pyi +12 -0
  484. mypy/typeshed/stdlib/lib2to3/fixes/fix_paren.pyi +8 -0
  485. mypy/typeshed/stdlib/lib2to3/fixes/fix_print.pyi +12 -0
  486. mypy/typeshed/stdlib/lib2to3/fixes/fix_raise.pyi +8 -0
  487. mypy/typeshed/stdlib/lib2to3/fixes/fix_raw_input.pyi +8 -0
  488. mypy/typeshed/stdlib/lib2to3/fixes/fix_reduce.pyi +8 -0
  489. mypy/typeshed/stdlib/lib2to3/fixes/fix_reload.pyi +9 -0
  490. mypy/typeshed/stdlib/lib2to3/fixes/fix_renames.pyi +17 -0
  491. mypy/typeshed/stdlib/lib2to3/fixes/fix_repr.pyi +8 -0
  492. mypy/typeshed/stdlib/lib2to3/fixes/fix_set_literal.pyi +7 -0
  493. mypy/typeshed/stdlib/lib2to3/fixes/fix_standarderror.pyi +8 -0
  494. mypy/typeshed/stdlib/lib2to3/fixes/fix_sys_exc.pyi +9 -0
  495. mypy/typeshed/stdlib/lib2to3/fixes/fix_throw.pyi +8 -0
  496. mypy/typeshed/stdlib/lib2to3/fixes/fix_tuple_params.pyi +17 -0
  497. mypy/typeshed/stdlib/lib2to3/fixes/fix_types.pyi +8 -0
  498. mypy/typeshed/stdlib/lib2to3/fixes/fix_unicode.pyi +12 -0
  499. mypy/typeshed/stdlib/lib2to3/fixes/fix_urllib.pyi +15 -0
  500. mypy/typeshed/stdlib/lib2to3/fixes/fix_ws_comma.pyi +12 -0
  501. mypy/typeshed/stdlib/lib2to3/fixes/fix_xrange.pyi +20 -0
  502. mypy/typeshed/stdlib/lib2to3/fixes/fix_xreadlines.pyi +8 -0
  503. mypy/typeshed/stdlib/lib2to3/fixes/fix_zip.pyi +9 -0
  504. mypy/typeshed/stdlib/lib2to3/main.pyi +42 -0
  505. mypy/typeshed/stdlib/lib2to3/pgen2/__init__.pyi +9 -0
  506. mypy/typeshed/stdlib/lib2to3/pgen2/driver.pyi +27 -0
  507. mypy/typeshed/stdlib/lib2to3/pgen2/grammar.pyi +24 -0
  508. mypy/typeshed/stdlib/lib2to3/pgen2/literals.pyi +7 -0
  509. mypy/typeshed/stdlib/lib2to3/pgen2/parse.pyi +30 -0
  510. mypy/typeshed/stdlib/lib2to3/pgen2/pgen.pyi +50 -0
  511. mypy/typeshed/stdlib/lib2to3/pgen2/token.pyi +67 -0
  512. mypy/typeshed/stdlib/lib2to3/pgen2/tokenize.pyi +96 -0
  513. mypy/typeshed/stdlib/lib2to3/pygram.pyi +114 -0
  514. mypy/typeshed/stdlib/lib2to3/pytree.pyi +117 -0
  515. mypy/typeshed/stdlib/lib2to3/refactor.pyi +82 -0
  516. mypy/typeshed/stdlib/linecache.pyi +23 -0
  517. mypy/typeshed/stdlib/locale.pyi +152 -0
  518. mypy/typeshed/stdlib/logging/__init__.pyi +655 -0
  519. mypy/typeshed/stdlib/logging/config.pyi +134 -0
  520. mypy/typeshed/stdlib/logging/handlers.pyi +275 -0
  521. mypy/typeshed/stdlib/lzma.pyi +197 -0
  522. mypy/typeshed/stdlib/mailbox.pyi +256 -0
  523. mypy/typeshed/stdlib/mailcap.pyi +11 -0
  524. mypy/typeshed/stdlib/marshal.pyi +33 -0
  525. mypy/typeshed/stdlib/math.pyi +125 -0
  526. mypy/typeshed/stdlib/mimetypes.pyi +46 -0
  527. mypy/typeshed/stdlib/mmap.pyi +113 -0
  528. mypy/typeshed/stdlib/modulefinder.pyi +66 -0
  529. mypy/typeshed/stdlib/msilib/__init__.pyi +176 -0
  530. mypy/typeshed/stdlib/msilib/schema.pyi +94 -0
  531. mypy/typeshed/stdlib/msilib/sequence.pyi +13 -0
  532. mypy/typeshed/stdlib/msilib/text.pyi +7 -0
  533. mypy/typeshed/stdlib/msvcrt.pyi +32 -0
  534. mypy/typeshed/stdlib/multiprocessing/__init__.pyi +90 -0
  535. mypy/typeshed/stdlib/multiprocessing/connection.pyi +75 -0
  536. mypy/typeshed/stdlib/multiprocessing/context.pyi +187 -0
  537. mypy/typeshed/stdlib/multiprocessing/dummy/__init__.pyi +77 -0
  538. mypy/typeshed/stdlib/multiprocessing/dummy/connection.pyi +39 -0
  539. mypy/typeshed/stdlib/multiprocessing/forkserver.pyi +31 -0
  540. mypy/typeshed/stdlib/multiprocessing/heap.pyi +36 -0
  541. mypy/typeshed/stdlib/multiprocessing/managers.pyi +212 -0
  542. mypy/typeshed/stdlib/multiprocessing/pool.pyi +103 -0
  543. mypy/typeshed/stdlib/multiprocessing/popen_fork.pyi +23 -0
  544. mypy/typeshed/stdlib/multiprocessing/popen_forkserver.pyi +16 -0
  545. mypy/typeshed/stdlib/multiprocessing/popen_spawn_posix.pyi +20 -0
  546. mypy/typeshed/stdlib/multiprocessing/popen_spawn_win32.pyi +30 -0
  547. mypy/typeshed/stdlib/multiprocessing/process.pyi +39 -0
  548. mypy/typeshed/stdlib/multiprocessing/queues.pyi +41 -0
  549. mypy/typeshed/stdlib/multiprocessing/reduction.pyi +89 -0
  550. mypy/typeshed/stdlib/multiprocessing/resource_sharer.pyi +20 -0
  551. mypy/typeshed/stdlib/multiprocessing/resource_tracker.pyi +18 -0
  552. mypy/typeshed/stdlib/multiprocessing/shared_memory.pyi +40 -0
  553. mypy/typeshed/stdlib/multiprocessing/sharedctypes.pyi +101 -0
  554. mypy/typeshed/stdlib/multiprocessing/spawn.pyi +32 -0
  555. mypy/typeshed/stdlib/multiprocessing/synchronize.pyi +54 -0
  556. mypy/typeshed/stdlib/multiprocessing/util.pyi +79 -0
  557. mypy/typeshed/stdlib/netrc.pyi +23 -0
  558. mypy/typeshed/stdlib/nis.pyi +9 -0
  559. mypy/typeshed/stdlib/nntplib.pyi +125 -0
  560. mypy/typeshed/stdlib/nt.pyi +111 -0
  561. mypy/typeshed/stdlib/ntpath.pyi +119 -0
  562. mypy/typeshed/stdlib/nturl2path.pyi +2 -0
  563. mypy/typeshed/stdlib/numbers.pyi +154 -0
  564. mypy/typeshed/stdlib/opcode.pyi +59 -0
  565. mypy/typeshed/stdlib/operator.pyi +110 -0
  566. mypy/typeshed/stdlib/optparse.pyi +252 -0
  567. mypy/typeshed/stdlib/os/__init__.pyi +1139 -0
  568. mypy/typeshed/stdlib/os/path.pyi +8 -0
  569. mypy/typeshed/stdlib/ossaudiodev.pyi +131 -0
  570. mypy/typeshed/stdlib/parser.pyi +24 -0
  571. mypy/typeshed/stdlib/pathlib.pyi +232 -0
  572. mypy/typeshed/stdlib/pdb.pyi +181 -0
  573. mypy/typeshed/stdlib/pickle.pyi +270 -0
  574. mypy/typeshed/stdlib/pickletools.pyi +167 -0
  575. mypy/typeshed/stdlib/pipes.pyi +16 -0
  576. mypy/typeshed/stdlib/pkgutil.pyi +53 -0
  577. mypy/typeshed/stdlib/platform.pyi +42 -0
  578. mypy/typeshed/stdlib/plistlib.pyi +113 -0
  579. mypy/typeshed/stdlib/poplib.pyi +71 -0
  580. mypy/typeshed/stdlib/posix.pyi +359 -0
  581. mypy/typeshed/stdlib/posixpath.pyi +161 -0
  582. mypy/typeshed/stdlib/pprint.pyi +112 -0
  583. mypy/typeshed/stdlib/profile.pyi +31 -0
  584. mypy/typeshed/stdlib/pstats.pyi +78 -0
  585. mypy/typeshed/stdlib/pty.pyi +19 -0
  586. mypy/typeshed/stdlib/pwd.pyi +27 -0
  587. mypy/typeshed/stdlib/py_compile.pyi +34 -0
  588. mypy/typeshed/stdlib/pyclbr.pyi +74 -0
  589. mypy/typeshed/stdlib/pydoc.pyi +261 -0
  590. mypy/typeshed/stdlib/pydoc_data/__init__.pyi +0 -0
  591. mypy/typeshed/stdlib/pydoc_data/topics.pyi +1 -0
  592. mypy/typeshed/stdlib/pyexpat/__init__.pyi +80 -0
  593. mypy/typeshed/stdlib/pyexpat/errors.pyi +49 -0
  594. mypy/typeshed/stdlib/pyexpat/model.pyi +11 -0
  595. mypy/typeshed/stdlib/queue.pyi +58 -0
  596. mypy/typeshed/stdlib/quopri.pyi +11 -0
  597. mypy/typeshed/stdlib/random.pyi +138 -0
  598. mypy/typeshed/stdlib/re.pyi +290 -0
  599. mypy/typeshed/stdlib/readline.pyi +36 -0
  600. mypy/typeshed/stdlib/reprlib.pyi +65 -0
  601. mypy/typeshed/stdlib/resource.pyi +92 -0
  602. mypy/typeshed/stdlib/rlcompleter.pyi +9 -0
  603. mypy/typeshed/stdlib/runpy.pyi +24 -0
  604. mypy/typeshed/stdlib/sched.pyi +42 -0
  605. mypy/typeshed/stdlib/secrets.pyi +15 -0
  606. mypy/typeshed/stdlib/select.pyi +151 -0
  607. mypy/typeshed/stdlib/selectors.pyi +67 -0
  608. mypy/typeshed/stdlib/shelve.pyi +46 -0
  609. mypy/typeshed/stdlib/shlex.pyi +43 -0
  610. mypy/typeshed/stdlib/shutil.pyi +189 -0
  611. mypy/typeshed/stdlib/signal.pyi +189 -0
  612. mypy/typeshed/stdlib/site.pyi +27 -0
  613. mypy/typeshed/stdlib/smtpd.pyi +91 -0
  614. mypy/typeshed/stdlib/smtplib.pyi +204 -0
  615. mypy/typeshed/stdlib/sndhdr.pyi +14 -0
  616. mypy/typeshed/stdlib/socket.pyi +825 -0
  617. mypy/typeshed/stdlib/socketserver.pyi +168 -0
  618. mypy/typeshed/stdlib/spwd.pyi +40 -0
  619. mypy/typeshed/stdlib/sqlite3/__init__.pyi +1 -0
  620. mypy/typeshed/stdlib/sqlite3/dbapi2.pyi +469 -0
  621. mypy/typeshed/stdlib/sre_compile.pyi +11 -0
  622. mypy/typeshed/stdlib/sre_constants.pyi +130 -0
  623. mypy/typeshed/stdlib/sre_parse.pyi +104 -0
  624. mypy/typeshed/stdlib/ssl.pyi +529 -0
  625. mypy/typeshed/stdlib/stat.pyi +1 -0
  626. mypy/typeshed/stdlib/statistics.pyi +132 -0
  627. mypy/typeshed/stdlib/string.pyi +83 -0
  628. mypy/typeshed/stdlib/stringprep.pyi +27 -0
  629. mypy/typeshed/stdlib/struct.pyi +26 -0
  630. mypy/typeshed/stdlib/subprocess.pyi +2614 -0
  631. mypy/typeshed/stdlib/sunau.pyi +86 -0
  632. mypy/typeshed/stdlib/symbol.pyi +93 -0
  633. mypy/typeshed/stdlib/symtable.pyi +58 -0
  634. mypy/typeshed/stdlib/sys/__init__.pyi +373 -0
  635. mypy/typeshed/stdlib/sys/_monitoring.pyi +52 -0
  636. mypy/typeshed/stdlib/sysconfig.pyi +43 -0
  637. mypy/typeshed/stdlib/syslog.pyi +46 -0
  638. mypy/typeshed/stdlib/tabnanny.pyi +16 -0
  639. mypy/typeshed/stdlib/tarfile.pyi +438 -0
  640. mypy/typeshed/stdlib/telnetlib.pyi +122 -0
  641. mypy/typeshed/stdlib/tempfile.pyi +477 -0
  642. mypy/typeshed/stdlib/termios.pyi +267 -0
  643. mypy/typeshed/stdlib/textwrap.pyi +103 -0
  644. mypy/typeshed/stdlib/this.pyi +2 -0
  645. mypy/typeshed/stdlib/threading.pyi +187 -0
  646. mypy/typeshed/stdlib/time.pyi +107 -0
  647. mypy/typeshed/stdlib/timeit.pyi +32 -0
  648. mypy/typeshed/stdlib/tkinter/__init__.pyi +3641 -0
  649. mypy/typeshed/stdlib/tkinter/colorchooser.pyi +20 -0
  650. mypy/typeshed/stdlib/tkinter/commondialog.pyi +14 -0
  651. mypy/typeshed/stdlib/tkinter/constants.pyi +80 -0
  652. mypy/typeshed/stdlib/tkinter/dialog.pyi +16 -0
  653. mypy/typeshed/stdlib/tkinter/dnd.pyi +20 -0
  654. mypy/typeshed/stdlib/tkinter/filedialog.pyi +151 -0
  655. mypy/typeshed/stdlib/tkinter/font.pyi +116 -0
  656. mypy/typeshed/stdlib/tkinter/messagebox.pyi +44 -0
  657. mypy/typeshed/stdlib/tkinter/scrolledtext.pyi +10 -0
  658. mypy/typeshed/stdlib/tkinter/simpledialog.pyi +54 -0
  659. mypy/typeshed/stdlib/tkinter/tix.pyi +299 -0
  660. mypy/typeshed/stdlib/tkinter/ttk.pyi +1191 -0
  661. mypy/typeshed/stdlib/token.pyi +159 -0
  662. mypy/typeshed/stdlib/tokenize.pyi +177 -0
  663. mypy/typeshed/stdlib/tomllib.pyi +10 -0
  664. mypy/typeshed/stdlib/trace.pyi +79 -0
  665. mypy/typeshed/stdlib/traceback.pyi +260 -0
  666. mypy/typeshed/stdlib/tracemalloc.pyi +124 -0
  667. mypy/typeshed/stdlib/tty.pyi +30 -0
  668. mypy/typeshed/stdlib/turtle.pyi +713 -0
  669. mypy/typeshed/stdlib/types.pyi +605 -0
  670. mypy/typeshed/stdlib/typing.pyi +968 -0
  671. mypy/typeshed/stdlib/typing_extensions.pyi +503 -0
  672. mypy/typeshed/stdlib/unicodedata.pyi +71 -0
  673. mypy/typeshed/stdlib/unittest/__init__.pyi +67 -0
  674. mypy/typeshed/stdlib/unittest/_log.pyi +27 -0
  675. mypy/typeshed/stdlib/unittest/async_case.pyi +21 -0
  676. mypy/typeshed/stdlib/unittest/case.pyi +342 -0
  677. mypy/typeshed/stdlib/unittest/loader.pyi +51 -0
  678. mypy/typeshed/stdlib/unittest/main.pyi +69 -0
  679. mypy/typeshed/stdlib/unittest/mock.pyi +430 -0
  680. mypy/typeshed/stdlib/unittest/result.pyi +46 -0
  681. mypy/typeshed/stdlib/unittest/runner.pyi +72 -0
  682. mypy/typeshed/stdlib/unittest/signals.pyi +15 -0
  683. mypy/typeshed/stdlib/unittest/suite.pyi +22 -0
  684. mypy/typeshed/stdlib/unittest/util.pyi +23 -0
  685. mypy/typeshed/stdlib/urllib/__init__.pyi +0 -0
  686. mypy/typeshed/stdlib/urllib/error.pyi +23 -0
  687. mypy/typeshed/stdlib/urllib/parse.pyi +210 -0
  688. mypy/typeshed/stdlib/urllib/request.pyi +399 -0
  689. mypy/typeshed/stdlib/urllib/response.pyi +43 -0
  690. mypy/typeshed/stdlib/urllib/robotparser.pyi +20 -0
  691. mypy/typeshed/stdlib/uu.pyi +13 -0
  692. mypy/typeshed/stdlib/uuid.pyi +100 -0
  693. mypy/typeshed/stdlib/venv/__init__.pyi +79 -0
  694. mypy/typeshed/stdlib/warnings.pyi +112 -0
  695. mypy/typeshed/stdlib/wave.pyi +85 -0
  696. mypy/typeshed/stdlib/weakref.pyi +150 -0
  697. mypy/typeshed/stdlib/webbrowser.pyi +74 -0
  698. mypy/typeshed/stdlib/winreg.pyi +103 -0
  699. mypy/typeshed/stdlib/winsound.pyi +28 -0
  700. mypy/typeshed/stdlib/wsgiref/__init__.pyi +0 -0
  701. mypy/typeshed/stdlib/wsgiref/handlers.pyi +91 -0
  702. mypy/typeshed/stdlib/wsgiref/headers.pyi +26 -0
  703. mypy/typeshed/stdlib/wsgiref/simple_server.pyi +37 -0
  704. mypy/typeshed/stdlib/wsgiref/types.pyi +32 -0
  705. mypy/typeshed/stdlib/wsgiref/util.pyi +24 -0
  706. mypy/typeshed/stdlib/wsgiref/validate.pyi +50 -0
  707. mypy/typeshed/stdlib/xdrlib.pyi +57 -0
  708. mypy/typeshed/stdlib/xml/__init__.pyi +1 -0
  709. mypy/typeshed/stdlib/xml/dom/NodeFilter.pyi +19 -0
  710. mypy/typeshed/stdlib/xml/dom/__init__.pyi +69 -0
  711. mypy/typeshed/stdlib/xml/dom/domreg.pyi +8 -0
  712. mypy/typeshed/stdlib/xml/dom/expatbuilder.pyi +100 -0
  713. mypy/typeshed/stdlib/xml/dom/minicompat.pyi +22 -0
  714. mypy/typeshed/stdlib/xml/dom/minidom.pyi +404 -0
  715. mypy/typeshed/stdlib/xml/dom/pulldom.pyi +94 -0
  716. mypy/typeshed/stdlib/xml/dom/xmlbuilder.pyi +108 -0
  717. mypy/typeshed/stdlib/xml/etree/ElementInclude.pyi +28 -0
  718. mypy/typeshed/stdlib/xml/etree/ElementPath.pyi +34 -0
  719. mypy/typeshed/stdlib/xml/etree/ElementTree.pyi +321 -0
  720. mypy/typeshed/stdlib/xml/etree/__init__.pyi +0 -0
  721. mypy/typeshed/stdlib/xml/etree/cElementTree.pyi +1 -0
  722. mypy/typeshed/stdlib/xml/parsers/__init__.pyi +1 -0
  723. mypy/typeshed/stdlib/xml/parsers/expat/__init__.pyi +1 -0
  724. mypy/typeshed/stdlib/xml/parsers/expat/errors.pyi +1 -0
  725. mypy/typeshed/stdlib/xml/parsers/expat/model.pyi +1 -0
  726. mypy/typeshed/stdlib/xml/sax/__init__.pyi +25 -0
  727. mypy/typeshed/stdlib/xml/sax/_exceptions.pyi +19 -0
  728. mypy/typeshed/stdlib/xml/sax/handler.pyi +55 -0
  729. mypy/typeshed/stdlib/xml/sax/saxutils.pyi +60 -0
  730. mypy/typeshed/stdlib/xml/sax/xmlreader.pyi +87 -0
  731. mypy/typeshed/stdlib/xmlrpc/__init__.pyi +0 -0
  732. mypy/typeshed/stdlib/xmlrpc/client.pyi +296 -0
  733. mypy/typeshed/stdlib/xmlrpc/server.pyi +143 -0
  734. mypy/typeshed/stdlib/xxlimited.pyi +22 -0
  735. mypy/typeshed/stdlib/zipapp.pyi +20 -0
  736. mypy/typeshed/stdlib/zipfile/__init__.pyi +306 -0
  737. mypy/typeshed/stdlib/zipfile/_path.pyi +95 -0
  738. mypy/typeshed/stdlib/zipimport.pyi +32 -0
  739. mypy/typeshed/stdlib/zlib.pyi +56 -0
  740. mypy/typeshed/stdlib/zoneinfo/__init__.pyi +38 -0
  741. mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi +218 -0
  742. mypy/typestate.py +323 -0
  743. mypy/typetraverser.py +142 -0
  744. mypy/typevars.py +93 -0
  745. mypy/typevartuples.py +32 -0
  746. mypy/util.py +869 -0
  747. mypy/version.py +1 -0
  748. mypy/visitor.py +621 -0
  749. mypy/xml/mypy-html.css +104 -0
  750. mypy/xml/mypy-html.xslt +81 -0
  751. mypy/xml/mypy-txt.xslt +100 -0
  752. mypy/xml/mypy.xsd +50 -0
  753. mypy_dev-1.9.0.dev0.dist-info/LICENSE +229 -0
  754. mypy_dev-1.9.0.dev0.dist-info/METADATA +39 -0
  755. mypy_dev-1.9.0.dev0.dist-info/RECORD +1018 -0
  756. mypy_dev-1.9.0.dev0.dist-info/WHEEL +5 -0
  757. mypy_dev-1.9.0.dev0.dist-info/entry_points.txt +6 -0
  758. mypy_dev-1.9.0.dev0.dist-info/top_level.txt +2 -0
  759. mypyc/README.md +133 -0
  760. mypyc/__init__.py +0 -0
  761. mypyc/__main__.py +57 -0
  762. mypyc/analysis/__init__.py +0 -0
  763. mypyc/analysis/attrdefined.py +436 -0
  764. mypyc/analysis/blockfreq.py +32 -0
  765. mypyc/analysis/dataflow.py +624 -0
  766. mypyc/analysis/ircheck.py +429 -0
  767. mypyc/analysis/selfleaks.py +207 -0
  768. mypyc/build.py +616 -0
  769. mypyc/codegen/__init__.py +0 -0
  770. mypyc/codegen/cstring.py +54 -0
  771. mypyc/codegen/emit.py +1193 -0
  772. mypyc/codegen/emitclass.py +1060 -0
  773. mypyc/codegen/emitfunc.py +846 -0
  774. mypyc/codegen/emitmodule.py +1131 -0
  775. mypyc/codegen/emitwrapper.py +979 -0
  776. mypyc/codegen/literals.py +302 -0
  777. mypyc/common.py +136 -0
  778. mypyc/crash.py +31 -0
  779. mypyc/doc/Makefile +20 -0
  780. mypyc/doc/bool_operations.rst +27 -0
  781. mypyc/doc/compilation_units.rst +20 -0
  782. mypyc/doc/conf.py +59 -0
  783. mypyc/doc/cpython-timings.md +25 -0
  784. mypyc/doc/dev-intro.md +548 -0
  785. mypyc/doc/dict_operations.rst +59 -0
  786. mypyc/doc/differences_from_python.rst +332 -0
  787. mypyc/doc/float_operations.rst +50 -0
  788. mypyc/doc/future.md +42 -0
  789. mypyc/doc/getting_started.rst +240 -0
  790. mypyc/doc/index.rst +61 -0
  791. mypyc/doc/int_operations.rst +162 -0
  792. mypyc/doc/introduction.rst +150 -0
  793. mypyc/doc/list_operations.rst +65 -0
  794. mypyc/doc/make.bat +35 -0
  795. mypyc/doc/native_classes.rst +206 -0
  796. mypyc/doc/native_operations.rst +55 -0
  797. mypyc/doc/performance_tips_and_tricks.rst +244 -0
  798. mypyc/doc/set_operations.rst +47 -0
  799. mypyc/doc/str_operations.rst +35 -0
  800. mypyc/doc/tuple_operations.rst +33 -0
  801. mypyc/doc/using_type_annotations.rst +398 -0
  802. mypyc/errors.py +29 -0
  803. mypyc/external/googletest/LICENSE +28 -0
  804. mypyc/external/googletest/README.md +280 -0
  805. mypyc/external/googletest/include/gtest/gtest-death-test.h +294 -0
  806. mypyc/external/googletest/include/gtest/gtest-message.h +250 -0
  807. mypyc/external/googletest/include/gtest/gtest-param-test.h +1444 -0
  808. mypyc/external/googletest/include/gtest/gtest-param-test.h.pump +510 -0
  809. mypyc/external/googletest/include/gtest/gtest-printers.h +993 -0
  810. mypyc/external/googletest/include/gtest/gtest-spi.h +232 -0
  811. mypyc/external/googletest/include/gtest/gtest-test-part.h +179 -0
  812. mypyc/external/googletest/include/gtest/gtest-typed-test.h +263 -0
  813. mypyc/external/googletest/include/gtest/gtest.h +2236 -0
  814. mypyc/external/googletest/include/gtest/gtest_pred_impl.h +358 -0
  815. mypyc/external/googletest/include/gtest/gtest_prod.h +58 -0
  816. mypyc/external/googletest/include/gtest/internal/custom/gtest-port.h +69 -0
  817. mypyc/external/googletest/include/gtest/internal/custom/gtest-printers.h +42 -0
  818. mypyc/external/googletest/include/gtest/internal/custom/gtest.h +41 -0
  819. mypyc/external/googletest/include/gtest/internal/gtest-death-test-internal.h +319 -0
  820. mypyc/external/googletest/include/gtest/internal/gtest-filepath.h +206 -0
  821. mypyc/external/googletest/include/gtest/internal/gtest-internal.h +1238 -0
  822. mypyc/external/googletest/include/gtest/internal/gtest-linked_ptr.h +243 -0
  823. mypyc/external/googletest/include/gtest/internal/gtest-param-util-generated.h +5146 -0
  824. mypyc/external/googletest/include/gtest/internal/gtest-param-util-generated.h.pump +286 -0
  825. mypyc/external/googletest/include/gtest/internal/gtest-param-util.h +731 -0
  826. mypyc/external/googletest/include/gtest/internal/gtest-port-arch.h +93 -0
  827. mypyc/external/googletest/include/gtest/internal/gtest-port.h +2560 -0
  828. mypyc/external/googletest/include/gtest/internal/gtest-string.h +167 -0
  829. mypyc/external/googletest/include/gtest/internal/gtest-tuple.h +1020 -0
  830. mypyc/external/googletest/include/gtest/internal/gtest-tuple.h.pump +347 -0
  831. mypyc/external/googletest/include/gtest/internal/gtest-type-util.h +3331 -0
  832. mypyc/external/googletest/include/gtest/internal/gtest-type-util.h.pump +297 -0
  833. mypyc/external/googletest/make/Makefile +61 -0
  834. mypyc/external/googletest/src/gtest-all.cc +48 -0
  835. mypyc/external/googletest/src/gtest-death-test.cc +1342 -0
  836. mypyc/external/googletest/src/gtest-filepath.cc +387 -0
  837. mypyc/external/googletest/src/gtest-internal-inl.h +1183 -0
  838. mypyc/external/googletest/src/gtest-port.cc +1259 -0
  839. mypyc/external/googletest/src/gtest-printers.cc +373 -0
  840. mypyc/external/googletest/src/gtest-test-part.cc +110 -0
  841. mypyc/external/googletest/src/gtest-typed-test.cc +118 -0
  842. mypyc/external/googletest/src/gtest.cc +5388 -0
  843. mypyc/external/googletest/src/gtest_main.cc +38 -0
  844. mypyc/ir/__init__.py +0 -0
  845. mypyc/ir/class_ir.py +499 -0
  846. mypyc/ir/func_ir.py +370 -0
  847. mypyc/ir/module_ir.py +88 -0
  848. mypyc/ir/ops.py +1642 -0
  849. mypyc/ir/pprint.py +496 -0
  850. mypyc/ir/rtypes.py +1038 -0
  851. mypyc/irbuild/__init__.py +0 -0
  852. mypyc/irbuild/ast_helpers.py +120 -0
  853. mypyc/irbuild/builder.py +1398 -0
  854. mypyc/irbuild/callable_class.py +173 -0
  855. mypyc/irbuild/classdef.py +850 -0
  856. mypyc/irbuild/constant_fold.py +95 -0
  857. mypyc/irbuild/context.py +186 -0
  858. mypyc/irbuild/env_class.py +223 -0
  859. mypyc/irbuild/expression.py +1067 -0
  860. mypyc/irbuild/for_helpers.py +1046 -0
  861. mypyc/irbuild/format_str_tokenizer.py +250 -0
  862. mypyc/irbuild/function.py +1089 -0
  863. mypyc/irbuild/generator.py +346 -0
  864. mypyc/irbuild/ll_builder.py +2407 -0
  865. mypyc/irbuild/main.py +153 -0
  866. mypyc/irbuild/mapper.py +221 -0
  867. mypyc/irbuild/match.py +355 -0
  868. mypyc/irbuild/nonlocalcontrol.py +197 -0
  869. mypyc/irbuild/prebuildvisitor.py +203 -0
  870. mypyc/irbuild/prepare.py +609 -0
  871. mypyc/irbuild/specialize.py +822 -0
  872. mypyc/irbuild/statement.py +1017 -0
  873. mypyc/irbuild/targets.py +57 -0
  874. mypyc/irbuild/util.py +189 -0
  875. mypyc/irbuild/visitor.py +401 -0
  876. mypyc/irbuild/vtable.py +82 -0
  877. mypyc/lib-rt/CPy.h +643 -0
  878. mypyc/lib-rt/bytes_ops.c +143 -0
  879. mypyc/lib-rt/dict_ops.c +446 -0
  880. mypyc/lib-rt/exc_ops.c +259 -0
  881. mypyc/lib-rt/float_ops.c +192 -0
  882. mypyc/lib-rt/generic_ops.c +64 -0
  883. mypyc/lib-rt/getargs.c +450 -0
  884. mypyc/lib-rt/getargsfast.c +569 -0
  885. mypyc/lib-rt/init.c +13 -0
  886. mypyc/lib-rt/int_ops.c +803 -0
  887. mypyc/lib-rt/list_ops.c +335 -0
  888. mypyc/lib-rt/misc_ops.c +942 -0
  889. mypyc/lib-rt/module_shim.tmpl +18 -0
  890. mypyc/lib-rt/mypyc_util.h +118 -0
  891. mypyc/lib-rt/pythoncapi_compat.h +497 -0
  892. mypyc/lib-rt/pythonsupport.h +548 -0
  893. mypyc/lib-rt/set_ops.c +17 -0
  894. mypyc/lib-rt/setup.py +70 -0
  895. mypyc/lib-rt/str_ops.c +241 -0
  896. mypyc/lib-rt/test_capi.cc +585 -0
  897. mypyc/lib-rt/tuple_ops.c +61 -0
  898. mypyc/namegen.py +115 -0
  899. mypyc/options.py +32 -0
  900. mypyc/primitives/__init__.py +0 -0
  901. mypyc/primitives/bytes_ops.py +101 -0
  902. mypyc/primitives/dict_ops.py +325 -0
  903. mypyc/primitives/exc_ops.py +101 -0
  904. mypyc/primitives/float_ops.py +168 -0
  905. mypyc/primitives/generic_ops.py +384 -0
  906. mypyc/primitives/int_ops.py +304 -0
  907. mypyc/primitives/list_ops.py +300 -0
  908. mypyc/primitives/misc_ops.py +241 -0
  909. mypyc/primitives/registry.py +323 -0
  910. mypyc/primitives/set_ops.py +121 -0
  911. mypyc/primitives/str_ops.py +229 -0
  912. mypyc/primitives/tuple_ops.py +83 -0
  913. mypyc/rt_subtype.py +77 -0
  914. mypyc/sametype.py +83 -0
  915. mypyc/subtype.py +88 -0
  916. mypyc/test/__init__.py +0 -0
  917. mypyc/test/config.py +13 -0
  918. mypyc/test/test_alwaysdefined.py +46 -0
  919. mypyc/test/test_analysis.py +77 -0
  920. mypyc/test/test_cheader.py +45 -0
  921. mypyc/test/test_commandline.py +82 -0
  922. mypyc/test/test_emit.py +69 -0
  923. mypyc/test/test_emitclass.py +35 -0
  924. mypyc/test/test_emitfunc.py +926 -0
  925. mypyc/test/test_emitwrapper.py +60 -0
  926. mypyc/test/test_exceptions.py +56 -0
  927. mypyc/test/test_external.py +49 -0
  928. mypyc/test/test_irbuild.py +87 -0
  929. mypyc/test/test_ircheck.py +199 -0
  930. mypyc/test/test_literals.py +90 -0
  931. mypyc/test/test_namegen.py +48 -0
  932. mypyc/test/test_pprint.py +42 -0
  933. mypyc/test/test_rarray.py +48 -0
  934. mypyc/test/test_refcount.py +59 -0
  935. mypyc/test/test_run.py +426 -0
  936. mypyc/test/test_serialization.py +108 -0
  937. mypyc/test/test_struct.py +112 -0
  938. mypyc/test/test_tuplename.py +33 -0
  939. mypyc/test/test_typeops.py +97 -0
  940. mypyc/test/testutil.py +283 -0
  941. mypyc/test-data/alwaysdefined.test +732 -0
  942. mypyc/test-data/analysis.test +603 -0
  943. mypyc/test-data/commandline.test +245 -0
  944. mypyc/test-data/driver/driver.py +48 -0
  945. mypyc/test-data/exceptions-freq.test +125 -0
  946. mypyc/test-data/exceptions.test +713 -0
  947. mypyc/test-data/fixtures/ir.py +373 -0
  948. mypyc/test-data/fixtures/testutil.py +103 -0
  949. mypyc/test-data/fixtures/typing-full.pyi +169 -0
  950. mypyc/test-data/irbuild-any.test +238 -0
  951. mypyc/test-data/irbuild-basic.test +3690 -0
  952. mypyc/test-data/irbuild-bool.test +463 -0
  953. mypyc/test-data/irbuild-bytes.test +184 -0
  954. mypyc/test-data/irbuild-classes.test +1303 -0
  955. mypyc/test-data/irbuild-constant-fold.test +480 -0
  956. mypyc/test-data/irbuild-dict.test +585 -0
  957. mypyc/test-data/irbuild-dunders.test +223 -0
  958. mypyc/test-data/irbuild-float.test +497 -0
  959. mypyc/test-data/irbuild-generics.test +151 -0
  960. mypyc/test-data/irbuild-glue-methods.test +437 -0
  961. mypyc/test-data/irbuild-i16.test +526 -0
  962. mypyc/test-data/irbuild-i32.test +598 -0
  963. mypyc/test-data/irbuild-i64.test +2164 -0
  964. mypyc/test-data/irbuild-int.test +235 -0
  965. mypyc/test-data/irbuild-isinstance.test +109 -0
  966. mypyc/test-data/irbuild-lists.test +547 -0
  967. mypyc/test-data/irbuild-match.test +1708 -0
  968. mypyc/test-data/irbuild-math.test +64 -0
  969. mypyc/test-data/irbuild-nested.test +807 -0
  970. mypyc/test-data/irbuild-optional.test +536 -0
  971. mypyc/test-data/irbuild-set.test +838 -0
  972. mypyc/test-data/irbuild-singledispatch.test +257 -0
  973. mypyc/test-data/irbuild-statements.test +1149 -0
  974. mypyc/test-data/irbuild-str.test +314 -0
  975. mypyc/test-data/irbuild-strip-asserts.test +12 -0
  976. mypyc/test-data/irbuild-try.test +523 -0
  977. mypyc/test-data/irbuild-tuple.test +462 -0
  978. mypyc/test-data/irbuild-u8.test +543 -0
  979. mypyc/test-data/irbuild-unreachable.test +241 -0
  980. mypyc/test-data/irbuild-vectorcall.test +153 -0
  981. mypyc/test-data/refcount.test +1588 -0
  982. mypyc/test-data/run-async.test +145 -0
  983. mypyc/test-data/run-attrs.test +318 -0
  984. mypyc/test-data/run-bench.test +196 -0
  985. mypyc/test-data/run-bools.test +229 -0
  986. mypyc/test-data/run-bytes.test +302 -0
  987. mypyc/test-data/run-classes.test +2505 -0
  988. mypyc/test-data/run-dicts.test +334 -0
  989. mypyc/test-data/run-dunders.test +945 -0
  990. mypyc/test-data/run-exceptions.test +448 -0
  991. mypyc/test-data/run-floats.test +516 -0
  992. mypyc/test-data/run-functions.test +1310 -0
  993. mypyc/test-data/run-generators.test +681 -0
  994. mypyc/test-data/run-i16.test +338 -0
  995. mypyc/test-data/run-i32.test +336 -0
  996. mypyc/test-data/run-i64.test +1519 -0
  997. mypyc/test-data/run-imports.test +265 -0
  998. mypyc/test-data/run-integers.test +540 -0
  999. mypyc/test-data/run-lists.test +411 -0
  1000. mypyc/test-data/run-loops.test +485 -0
  1001. mypyc/test-data/run-match.test +283 -0
  1002. mypyc/test-data/run-math.test +106 -0
  1003. mypyc/test-data/run-misc.test +1168 -0
  1004. mypyc/test-data/run-multimodule.test +887 -0
  1005. mypyc/test-data/run-mypy-sim.test +138 -0
  1006. mypyc/test-data/run-primitives.test +375 -0
  1007. mypyc/test-data/run-python37.test +159 -0
  1008. mypyc/test-data/run-python38.test +88 -0
  1009. mypyc/test-data/run-sets.test +150 -0
  1010. mypyc/test-data/run-singledispatch.test +698 -0
  1011. mypyc/test-data/run-strings.test +641 -0
  1012. mypyc/test-data/run-traits.test +411 -0
  1013. mypyc/test-data/run-tuples.test +258 -0
  1014. mypyc/test-data/run-u8.test +303 -0
  1015. mypyc/transform/__init__.py +0 -0
  1016. mypyc/transform/exceptions.py +182 -0
  1017. mypyc/transform/refcount.py +294 -0
  1018. mypyc/transform/uninit.py +190 -0
mypy/constraints.py ADDED
@@ -0,0 +1,1624 @@
1
+ """Type inference constraints."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Final, Iterable, List, Sequence
6
+
7
+ import mypy.subtypes
8
+ import mypy.typeops
9
+ from mypy.argmap import ArgTypeExpander
10
+ from mypy.erasetype import erase_typevars
11
+ from mypy.maptype import map_instance_to_supertype
12
+ from mypy.nodes import (
13
+ ARG_OPT,
14
+ ARG_POS,
15
+ ARG_STAR,
16
+ ARG_STAR2,
17
+ CONTRAVARIANT,
18
+ COVARIANT,
19
+ ArgKind,
20
+ TypeInfo,
21
+ )
22
+ from mypy.types import (
23
+ TUPLE_LIKE_INSTANCE_NAMES,
24
+ AnyType,
25
+ CallableType,
26
+ DeletedType,
27
+ ErasedType,
28
+ Instance,
29
+ LiteralType,
30
+ NoneType,
31
+ NormalizedCallableType,
32
+ Overloaded,
33
+ Parameters,
34
+ ParamSpecType,
35
+ PartialType,
36
+ ProperType,
37
+ TupleType,
38
+ Type,
39
+ TypeAliasType,
40
+ TypedDictType,
41
+ TypeOfAny,
42
+ TypeQuery,
43
+ TypeType,
44
+ TypeVarId,
45
+ TypeVarLikeType,
46
+ TypeVarTupleType,
47
+ TypeVarType,
48
+ TypeVisitor,
49
+ UnboundType,
50
+ UninhabitedType,
51
+ UnionType,
52
+ UnpackType,
53
+ find_unpack_in_list,
54
+ get_proper_type,
55
+ has_recursive_types,
56
+ has_type_vars,
57
+ is_named_instance,
58
+ split_with_prefix_and_suffix,
59
+ )
60
+ from mypy.types_utils import is_union_with_any
61
+ from mypy.typestate import type_state
62
+
63
+ if TYPE_CHECKING:
64
+ from mypy.infer import ArgumentInferContext
65
+
66
+ SUBTYPE_OF: Final = 0
67
+ SUPERTYPE_OF: Final = 1
68
+
69
+
70
+ class Constraint:
71
+ """A representation of a type constraint.
72
+
73
+ It can be either T <: type or T :> type (T is a type variable).
74
+ """
75
+
76
+ type_var: TypeVarId
77
+ op = 0 # SUBTYPE_OF or SUPERTYPE_OF
78
+ target: Type
79
+
80
+ def __init__(self, type_var: TypeVarLikeType, op: int, target: Type) -> None:
81
+ self.type_var = type_var.id
82
+ self.op = op
83
+ # TODO: should we add "assert not isinstance(target, UnpackType)"?
84
+ # UnpackType is a synthetic type, and is never valid as a constraint target.
85
+ self.target = target
86
+ self.origin_type_var = type_var
87
+ # These are additional type variables that should be solved for together with type_var.
88
+ # TODO: A cleaner solution may be to modify the return type of infer_constraints()
89
+ # to include these instead, but this is a rather big refactoring.
90
+ self.extra_tvars: list[TypeVarLikeType] = []
91
+
92
+ def __repr__(self) -> str:
93
+ op_str = "<:"
94
+ if self.op == SUPERTYPE_OF:
95
+ op_str = ":>"
96
+ return f"{self.type_var} {op_str} {self.target}"
97
+
98
+ def __hash__(self) -> int:
99
+ return hash((self.type_var, self.op, self.target))
100
+
101
+ def __eq__(self, other: object) -> bool:
102
+ if not isinstance(other, Constraint):
103
+ return False
104
+ return (self.type_var, self.op, self.target) == (other.type_var, other.op, other.target)
105
+
106
+
107
+ def infer_constraints_for_callable(
108
+ callee: CallableType,
109
+ arg_types: Sequence[Type | None],
110
+ arg_kinds: list[ArgKind],
111
+ arg_names: Sequence[str | None] | None,
112
+ formal_to_actual: list[list[int]],
113
+ context: ArgumentInferContext,
114
+ ) -> list[Constraint]:
115
+ """Infer type variable constraints for a callable and actual arguments.
116
+
117
+ Return a list of constraints.
118
+ """
119
+ constraints: list[Constraint] = []
120
+ mapper = ArgTypeExpander(context)
121
+
122
+ param_spec = callee.param_spec()
123
+ param_spec_arg_types = []
124
+ param_spec_arg_names = []
125
+ param_spec_arg_kinds = []
126
+
127
+ incomplete_star_mapping = False
128
+ for i, actuals in enumerate(formal_to_actual):
129
+ for actual in actuals:
130
+ if actual is None and callee.arg_kinds[i] in (ARG_STAR, ARG_STAR2):
131
+ # We can't use arguments to infer ParamSpec constraint, if only some
132
+ # are present in the current inference pass.
133
+ incomplete_star_mapping = True
134
+ break
135
+
136
+ for i, actuals in enumerate(formal_to_actual):
137
+ if isinstance(callee.arg_types[i], UnpackType):
138
+ unpack_type = callee.arg_types[i]
139
+ assert isinstance(unpack_type, UnpackType)
140
+
141
+ # In this case we are binding all the actuals to *args,
142
+ # and we want a constraint that the typevar tuple being unpacked
143
+ # is equal to a type list of all the actuals.
144
+ actual_types = []
145
+
146
+ unpacked_type = get_proper_type(unpack_type.type)
147
+ if isinstance(unpacked_type, TypeVarTupleType):
148
+ tuple_instance = unpacked_type.tuple_fallback
149
+ elif isinstance(unpacked_type, TupleType):
150
+ tuple_instance = unpacked_type.partial_fallback
151
+ else:
152
+ assert False, "mypy bug: unhandled constraint inference case"
153
+
154
+ for actual in actuals:
155
+ actual_arg_type = arg_types[actual]
156
+ if actual_arg_type is None:
157
+ continue
158
+
159
+ expanded_actual = mapper.expand_actual_type(
160
+ actual_arg_type,
161
+ arg_kinds[actual],
162
+ callee.arg_names[i],
163
+ callee.arg_kinds[i],
164
+ allow_unpack=True,
165
+ )
166
+
167
+ if arg_kinds[actual] != ARG_STAR or isinstance(
168
+ get_proper_type(actual_arg_type), TupleType
169
+ ):
170
+ actual_types.append(expanded_actual)
171
+ else:
172
+ # If we are expanding an iterable inside * actual, append a homogeneous item instead
173
+ actual_types.append(
174
+ UnpackType(tuple_instance.copy_modified(args=[expanded_actual]))
175
+ )
176
+
177
+ if isinstance(unpacked_type, TypeVarTupleType):
178
+ constraints.append(
179
+ Constraint(
180
+ unpacked_type,
181
+ SUPERTYPE_OF,
182
+ TupleType(actual_types, unpacked_type.tuple_fallback),
183
+ )
184
+ )
185
+ elif isinstance(unpacked_type, TupleType):
186
+ # Prefixes get converted to positional args, so technically the only case we
187
+ # should have here is like Tuple[Unpack[Ts], Y1, Y2, Y3]. If this turns out
188
+ # not to hold we can always handle the prefixes too.
189
+ inner_unpack = unpacked_type.items[0]
190
+ assert isinstance(inner_unpack, UnpackType)
191
+ inner_unpacked_type = get_proper_type(inner_unpack.type)
192
+ suffix_len = len(unpacked_type.items) - 1
193
+ if isinstance(inner_unpacked_type, TypeVarTupleType):
194
+ # Variadic item can be either *Ts...
195
+ constraints.append(
196
+ Constraint(
197
+ inner_unpacked_type,
198
+ SUPERTYPE_OF,
199
+ TupleType(
200
+ actual_types[:-suffix_len], inner_unpacked_type.tuple_fallback
201
+ ),
202
+ )
203
+ )
204
+ else:
205
+ # ...or it can be a homogeneous tuple.
206
+ assert (
207
+ isinstance(inner_unpacked_type, Instance)
208
+ and inner_unpacked_type.type.fullname == "builtins.tuple"
209
+ )
210
+ for at in actual_types[:-suffix_len]:
211
+ constraints.extend(
212
+ infer_constraints(inner_unpacked_type.args[0], at, SUPERTYPE_OF)
213
+ )
214
+ # Now handle the suffix (if any).
215
+ if suffix_len:
216
+ for tt, at in zip(unpacked_type.items[1:], actual_types[-suffix_len:]):
217
+ constraints.extend(infer_constraints(tt, at, SUPERTYPE_OF))
218
+ else:
219
+ assert False, "mypy bug: unhandled constraint inference case"
220
+ else:
221
+ for actual in actuals:
222
+ actual_arg_type = arg_types[actual]
223
+ if actual_arg_type is None:
224
+ continue
225
+
226
+ actual_type = mapper.expand_actual_type(
227
+ actual_arg_type, arg_kinds[actual], callee.arg_names[i], callee.arg_kinds[i]
228
+ )
229
+ if param_spec and callee.arg_kinds[i] in (ARG_STAR, ARG_STAR2):
230
+ # If actual arguments are mapped to ParamSpec type, we can't infer individual
231
+ # constraints, instead store them and infer single constraint at the end.
232
+ # It is impossible to map actual kind to formal kind, so use some heuristic.
233
+ # This inference is used as a fallback, so relying on heuristic should be OK.
234
+ if not incomplete_star_mapping:
235
+ param_spec_arg_types.append(
236
+ mapper.expand_actual_type(
237
+ actual_arg_type, arg_kinds[actual], None, arg_kinds[actual]
238
+ )
239
+ )
240
+ actual_kind = arg_kinds[actual]
241
+ param_spec_arg_kinds.append(
242
+ ARG_POS if actual_kind not in (ARG_STAR, ARG_STAR2) else actual_kind
243
+ )
244
+ param_spec_arg_names.append(arg_names[actual] if arg_names else None)
245
+ else:
246
+ c = infer_constraints(callee.arg_types[i], actual_type, SUPERTYPE_OF)
247
+ constraints.extend(c)
248
+ if (
249
+ param_spec
250
+ and not any(c.type_var == param_spec.id for c in constraints)
251
+ and not incomplete_star_mapping
252
+ ):
253
+ # Use ParamSpec constraint from arguments only if there are no other constraints,
254
+ # since as explained above it is quite ad-hoc.
255
+ constraints.append(
256
+ Constraint(
257
+ param_spec,
258
+ SUPERTYPE_OF,
259
+ Parameters(
260
+ arg_types=param_spec_arg_types,
261
+ arg_kinds=param_spec_arg_kinds,
262
+ arg_names=param_spec_arg_names,
263
+ imprecise_arg_kinds=True,
264
+ ),
265
+ )
266
+ )
267
+ if any(isinstance(v, ParamSpecType) for v in callee.variables):
268
+ # As a perf optimization filter imprecise constraints only when we can have them.
269
+ constraints = filter_imprecise_kinds(constraints)
270
+ return constraints
271
+
272
+
273
+ def infer_constraints(
274
+ template: Type, actual: Type, direction: int, skip_neg_op: bool = False
275
+ ) -> list[Constraint]:
276
+ """Infer type constraints.
277
+
278
+ Match a template type, which may contain type variable references,
279
+ recursively against a type which does not contain (the same) type
280
+ variable references. The result is a list of type constrains of
281
+ form 'T is a supertype/subtype of x', where T is a type variable
282
+ present in the template and x is a type without reference to type
283
+ variables present in the template.
284
+
285
+ Assume T and S are type variables. Now the following results can be
286
+ calculated (read as '(template, actual) --> result'):
287
+
288
+ (T, X) --> T :> X
289
+ (X[T], X[Y]) --> T <: Y and T :> Y
290
+ ((T, T), (X, Y)) --> T :> X and T :> Y
291
+ ((T, S), (X, Y)) --> T :> X and S :> Y
292
+ (X[T], Any) --> T <: Any and T :> Any
293
+
294
+ The constraints are represented as Constraint objects. If skip_neg_op == True,
295
+ then skip adding reverse (polymorphic) constraints (since this is already a call
296
+ to infer such constraints).
297
+ """
298
+ if any(
299
+ get_proper_type(template) == get_proper_type(t)
300
+ and get_proper_type(actual) == get_proper_type(a)
301
+ for (t, a) in reversed(type_state.inferring)
302
+ ):
303
+ return []
304
+ if has_recursive_types(template) or isinstance(get_proper_type(template), Instance):
305
+ # This case requires special care because it may cause infinite recursion.
306
+ # Note that we include Instances because the may be recursive as str(Sequence[str]).
307
+ if not has_type_vars(template):
308
+ # Return early on an empty branch.
309
+ return []
310
+ type_state.inferring.append((template, actual))
311
+ res = _infer_constraints(template, actual, direction, skip_neg_op)
312
+ type_state.inferring.pop()
313
+ return res
314
+ return _infer_constraints(template, actual, direction, skip_neg_op)
315
+
316
+
317
+ def _infer_constraints(
318
+ template: Type, actual: Type, direction: int, skip_neg_op: bool
319
+ ) -> list[Constraint]:
320
+ orig_template = template
321
+ template = get_proper_type(template)
322
+ actual = get_proper_type(actual)
323
+
324
+ # Type inference shouldn't be affected by whether union types have been simplified.
325
+ # We however keep any ErasedType items, so that the caller will see it when using
326
+ # checkexpr.has_erased_component().
327
+ if isinstance(template, UnionType):
328
+ template = mypy.typeops.make_simplified_union(template.items, keep_erased=True)
329
+ if isinstance(actual, UnionType):
330
+ actual = mypy.typeops.make_simplified_union(actual.items, keep_erased=True)
331
+
332
+ # Ignore Any types from the type suggestion engine to avoid them
333
+ # causing us to infer Any in situations where a better job could
334
+ # be done otherwise. (This can produce false positives but that
335
+ # doesn't really matter because it is all heuristic anyway.)
336
+ if isinstance(actual, AnyType) and actual.type_of_any == TypeOfAny.suggestion_engine:
337
+ return []
338
+
339
+ # If the template is simply a type variable, emit a Constraint directly.
340
+ # We need to handle this case before handling Unions for two reasons:
341
+ # 1. "T <: Union[U1, U2]" is not equivalent to "T <: U1 or T <: U2",
342
+ # because T can itself be a union (notably, Union[U1, U2] itself).
343
+ # 2. "T :> Union[U1, U2]" is logically equivalent to "T :> U1 and
344
+ # T :> U2", but they are not equivalent to the constraint solver,
345
+ # which never introduces new Union types (it uses join() instead).
346
+ if isinstance(template, TypeVarType):
347
+ return [Constraint(template, direction, actual)]
348
+
349
+ if (
350
+ isinstance(actual, TypeVarType)
351
+ and not actual.id.is_meta_var()
352
+ and direction == SUPERTYPE_OF
353
+ ):
354
+ # Unless template is also a type variable (or a union that contains one), using the upper
355
+ # bound for inference will usually give better result for actual that is a type variable.
356
+ if not isinstance(template, UnionType) or not any(
357
+ isinstance(t, TypeVarType) for t in template.items
358
+ ):
359
+ actual = get_proper_type(actual.upper_bound)
360
+
361
+ # Now handle the case of either template or actual being a Union.
362
+ # For a Union to be a subtype of another type, every item of the Union
363
+ # must be a subtype of that type, so concatenate the constraints.
364
+ if direction == SUBTYPE_OF and isinstance(template, UnionType):
365
+ res = []
366
+ for t_item in template.items:
367
+ res.extend(infer_constraints(t_item, actual, direction))
368
+ return res
369
+ if direction == SUPERTYPE_OF and isinstance(actual, UnionType):
370
+ res = []
371
+ for a_item in actual.items:
372
+ res.extend(infer_constraints(orig_template, a_item, direction))
373
+ return res
374
+
375
+ # Now the potential subtype is known not to be a Union or a type
376
+ # variable that we are solving for. In that case, for a Union to
377
+ # be a supertype of the potential subtype, some item of the Union
378
+ # must be a supertype of it.
379
+ if direction == SUBTYPE_OF and isinstance(actual, UnionType):
380
+ # If some of items is not a complete type, disregard that.
381
+ items = simplify_away_incomplete_types(actual.items)
382
+ # We infer constraints eagerly -- try to find constraints for a type
383
+ # variable if possible. This seems to help with some real-world
384
+ # use cases.
385
+ return any_constraints(
386
+ [infer_constraints_if_possible(template, a_item, direction) for a_item in items],
387
+ eager=True,
388
+ )
389
+ if direction == SUPERTYPE_OF and isinstance(template, UnionType):
390
+ # When the template is a union, we are okay with leaving some
391
+ # type variables indeterminate. This helps with some special
392
+ # cases, though this isn't very principled.
393
+ result = any_constraints(
394
+ [
395
+ infer_constraints_if_possible(t_item, actual, direction)
396
+ for t_item in template.items
397
+ ],
398
+ eager=False,
399
+ )
400
+ if result:
401
+ return result
402
+ elif has_recursive_types(template) and not has_recursive_types(actual):
403
+ return handle_recursive_union(template, actual, direction)
404
+ return []
405
+
406
+ # Remaining cases are handled by ConstraintBuilderVisitor.
407
+ return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op))
408
+
409
+
410
+ def infer_constraints_if_possible(
411
+ template: Type, actual: Type, direction: int
412
+ ) -> list[Constraint] | None:
413
+ """Like infer_constraints, but return None if the input relation is
414
+ known to be unsatisfiable, for example if template=List[T] and actual=int.
415
+ (In this case infer_constraints would return [], just like it would for
416
+ an automatically satisfied relation like template=List[T] and actual=object.)
417
+ """
418
+ if direction == SUBTYPE_OF and not mypy.subtypes.is_subtype(erase_typevars(template), actual):
419
+ return None
420
+ if direction == SUPERTYPE_OF and not mypy.subtypes.is_subtype(
421
+ actual, erase_typevars(template)
422
+ ):
423
+ return None
424
+ if (
425
+ direction == SUPERTYPE_OF
426
+ and isinstance(template, TypeVarType)
427
+ and not mypy.subtypes.is_subtype(actual, erase_typevars(template.upper_bound))
428
+ ):
429
+ # This is not caught by the above branch because of the erase_typevars() call,
430
+ # that would return 'Any' for a type variable.
431
+ return None
432
+ return infer_constraints(template, actual, direction)
433
+
434
+
435
+ def select_trivial(options: Sequence[list[Constraint] | None]) -> list[list[Constraint]]:
436
+ """Select only those lists where each item is a constraint against Any."""
437
+ res = []
438
+ for option in options:
439
+ if option is None:
440
+ continue
441
+ if all(isinstance(get_proper_type(c.target), AnyType) for c in option):
442
+ res.append(option)
443
+ return res
444
+
445
+
446
+ def merge_with_any(constraint: Constraint) -> Constraint:
447
+ """Transform a constraint target into a union with given Any type."""
448
+ target = constraint.target
449
+ if is_union_with_any(target):
450
+ # Do not produce redundant unions.
451
+ return constraint
452
+ # TODO: if we will support multiple sources Any, use this here instead.
453
+ any_type = AnyType(TypeOfAny.implementation_artifact)
454
+ return Constraint(
455
+ constraint.origin_type_var,
456
+ constraint.op,
457
+ UnionType.make_union([target, any_type], target.line, target.column),
458
+ )
459
+
460
+
461
+ def handle_recursive_union(template: UnionType, actual: Type, direction: int) -> list[Constraint]:
462
+ # This is a hack to special-case things like Union[T, Inst[T]] in recursive types. Although
463
+ # it is quite arbitrary, it is a relatively common pattern, so we should handle it well.
464
+ # This function may be called when inferring against such union resulted in different
465
+ # constraints for each item. Normally we give up in such case, but here we instead split
466
+ # the union in two parts, and try inferring sequentially.
467
+ non_type_var_items = [t for t in template.items if not isinstance(t, TypeVarType)]
468
+ type_var_items = [t for t in template.items if isinstance(t, TypeVarType)]
469
+ return infer_constraints(
470
+ UnionType.make_union(non_type_var_items), actual, direction
471
+ ) or infer_constraints(UnionType.make_union(type_var_items), actual, direction)
472
+
473
+
474
+ def any_constraints(options: list[list[Constraint] | None], eager: bool) -> list[Constraint]:
475
+ """Deduce what we can from a collection of constraint lists.
476
+
477
+ It's a given that at least one of the lists must be satisfied. A
478
+ None element in the list of options represents an unsatisfiable
479
+ constraint and is ignored. Ignore empty constraint lists if eager
480
+ is true -- they are always trivially satisfiable.
481
+ """
482
+ if eager:
483
+ valid_options = [option for option in options if option]
484
+ else:
485
+ valid_options = [option for option in options if option is not None]
486
+
487
+ if not valid_options:
488
+ return []
489
+
490
+ if len(valid_options) == 1:
491
+ return valid_options[0]
492
+
493
+ if all(is_same_constraints(valid_options[0], c) for c in valid_options[1:]):
494
+ # Multiple sets of constraints that are all the same. Just pick any one of them.
495
+ return valid_options[0]
496
+
497
+ if all(is_similar_constraints(valid_options[0], c) for c in valid_options[1:]):
498
+ # All options have same structure. In this case we can merge-in trivial
499
+ # options (i.e. those that only have Any) and try again.
500
+ # TODO: More generally, if a given (variable, direction) pair appears in
501
+ # every option, combine the bounds with meet/join always, not just for Any.
502
+ trivial_options = select_trivial(valid_options)
503
+ if trivial_options and len(trivial_options) < len(valid_options):
504
+ merged_options = []
505
+ for option in valid_options:
506
+ if option in trivial_options:
507
+ continue
508
+ if option is not None:
509
+ merged_option: list[Constraint] | None = [merge_with_any(c) for c in option]
510
+ else:
511
+ merged_option = None
512
+ merged_options.append(merged_option)
513
+ return any_constraints(list(merged_options), eager)
514
+
515
+ # If normal logic didn't work, try excluding trivially unsatisfiable constraint (due to
516
+ # upper bounds) from each option, and comparing them again.
517
+ filtered_options = [filter_satisfiable(o) for o in options]
518
+ if filtered_options != options:
519
+ return any_constraints(filtered_options, eager=eager)
520
+
521
+ # Otherwise, there are either no valid options or multiple, inconsistent valid
522
+ # options. Give up and deduce nothing.
523
+ return []
524
+
525
+
526
+ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | None:
527
+ """Keep only constraints that can possibly be satisfied.
528
+
529
+ Currently, we filter out constraints where target is not a subtype of the upper bound.
530
+ Since those can be never satisfied. We may add more cases in future if it improves type
531
+ inference.
532
+ """
533
+ if not option:
534
+ return option
535
+ satisfiable = []
536
+ for c in option:
537
+ if isinstance(c.origin_type_var, TypeVarType) and c.origin_type_var.values:
538
+ if any(
539
+ mypy.subtypes.is_subtype(c.target, value) for value in c.origin_type_var.values
540
+ ):
541
+ satisfiable.append(c)
542
+ elif mypy.subtypes.is_subtype(c.target, c.origin_type_var.upper_bound):
543
+ satisfiable.append(c)
544
+ if not satisfiable:
545
+ return None
546
+ return satisfiable
547
+
548
+
549
+ def is_same_constraints(x: list[Constraint], y: list[Constraint]) -> bool:
550
+ for c1 in x:
551
+ if not any(is_same_constraint(c1, c2) for c2 in y):
552
+ return False
553
+ for c1 in y:
554
+ if not any(is_same_constraint(c1, c2) for c2 in x):
555
+ return False
556
+ return True
557
+
558
+
559
+ def is_same_constraint(c1: Constraint, c2: Constraint) -> bool:
560
+ # Ignore direction when comparing constraints against Any.
561
+ skip_op_check = isinstance(get_proper_type(c1.target), AnyType) and isinstance(
562
+ get_proper_type(c2.target), AnyType
563
+ )
564
+ return (
565
+ c1.type_var == c2.type_var
566
+ and (c1.op == c2.op or skip_op_check)
567
+ and mypy.subtypes.is_same_type(c1.target, c2.target)
568
+ )
569
+
570
+
571
+ def is_similar_constraints(x: list[Constraint], y: list[Constraint]) -> bool:
572
+ """Check that two lists of constraints have similar structure.
573
+
574
+ This means that each list has same type variable plus direction pairs (i.e we
575
+ ignore the target). Except for constraints where target is Any type, there
576
+ we ignore direction as well.
577
+ """
578
+ return _is_similar_constraints(x, y) and _is_similar_constraints(y, x)
579
+
580
+
581
+ def _is_similar_constraints(x: list[Constraint], y: list[Constraint]) -> bool:
582
+ """Check that every constraint in the first list has a similar one in the second.
583
+
584
+ See docstring above for definition of similarity.
585
+ """
586
+ for c1 in x:
587
+ has_similar = False
588
+ for c2 in y:
589
+ # Ignore direction when either constraint is against Any.
590
+ skip_op_check = isinstance(get_proper_type(c1.target), AnyType) or isinstance(
591
+ get_proper_type(c2.target), AnyType
592
+ )
593
+ if c1.type_var == c2.type_var and (c1.op == c2.op or skip_op_check):
594
+ has_similar = True
595
+ break
596
+ if not has_similar:
597
+ return False
598
+ return True
599
+
600
+
601
+ def simplify_away_incomplete_types(types: Iterable[Type]) -> list[Type]:
602
+ complete = [typ for typ in types if is_complete_type(typ)]
603
+ if complete:
604
+ return complete
605
+ else:
606
+ return list(types)
607
+
608
+
609
+ def is_complete_type(typ: Type) -> bool:
610
+ """Is a type complete?
611
+
612
+ A complete doesn't have uninhabited type components or (when not in strict
613
+ optional mode) None components.
614
+ """
615
+ return typ.accept(CompleteTypeVisitor())
616
+
617
+
618
+ class CompleteTypeVisitor(TypeQuery[bool]):
619
+ def __init__(self) -> None:
620
+ super().__init__(all)
621
+
622
+ def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
623
+ return False
624
+
625
+
626
+ class ConstraintBuilderVisitor(TypeVisitor[List[Constraint]]):
627
+ """Visitor class for inferring type constraints."""
628
+
629
+ # The type that is compared against a template
630
+ # TODO: The value may be None. Is that actually correct?
631
+ actual: ProperType
632
+
633
+ def __init__(self, actual: ProperType, direction: int, skip_neg_op: bool) -> None:
634
+ # Direction must be SUBTYPE_OF or SUPERTYPE_OF.
635
+ self.actual = actual
636
+ self.direction = direction
637
+ # Whether to skip polymorphic inference (involves inference in opposite direction)
638
+ # this is used to prevent infinite recursion when both template and actual are
639
+ # generic callables.
640
+ self.skip_neg_op = skip_neg_op
641
+
642
+ # Trivial leaf types
643
+
644
+ def visit_unbound_type(self, template: UnboundType) -> list[Constraint]:
645
+ return []
646
+
647
+ def visit_any(self, template: AnyType) -> list[Constraint]:
648
+ return []
649
+
650
+ def visit_none_type(self, template: NoneType) -> list[Constraint]:
651
+ return []
652
+
653
+ def visit_uninhabited_type(self, template: UninhabitedType) -> list[Constraint]:
654
+ return []
655
+
656
+ def visit_erased_type(self, template: ErasedType) -> list[Constraint]:
657
+ return []
658
+
659
+ def visit_deleted_type(self, template: DeletedType) -> list[Constraint]:
660
+ return []
661
+
662
+ def visit_literal_type(self, template: LiteralType) -> list[Constraint]:
663
+ return []
664
+
665
+ # Errors
666
+
667
+ def visit_partial_type(self, template: PartialType) -> list[Constraint]:
668
+ # We can't do anything useful with a partial type here.
669
+ assert False, "Internal error"
670
+
671
+ # Non-trivial leaf type
672
+
673
+ def visit_type_var(self, template: TypeVarType) -> list[Constraint]:
674
+ assert False, (
675
+ "Unexpected TypeVarType in ConstraintBuilderVisitor"
676
+ " (should have been handled in infer_constraints)"
677
+ )
678
+
679
+ def visit_param_spec(self, template: ParamSpecType) -> list[Constraint]:
680
+ # Can't infer ParamSpecs from component values (only via Callable[P, T]).
681
+ return []
682
+
683
+ def visit_type_var_tuple(self, template: TypeVarTupleType) -> list[Constraint]:
684
+ raise NotImplementedError
685
+
686
+ def visit_unpack_type(self, template: UnpackType) -> list[Constraint]:
687
+ raise RuntimeError("Mypy bug: unpack should be handled at a higher level.")
688
+
689
+ def visit_parameters(self, template: Parameters) -> list[Constraint]:
690
+ # Constraining Any against C[P] turns into infer_against_any([P], Any)
691
+ # ... which seems like the only case this can happen. Better to fail loudly otherwise.
692
+ if isinstance(self.actual, AnyType):
693
+ return self.infer_against_any(template.arg_types, self.actual)
694
+ if type_state.infer_polymorphic and isinstance(self.actual, Parameters):
695
+ # For polymorphic inference we need to be able to infer secondary constraints
696
+ # in situations like [x: T] <: P <: [x: int].
697
+ return infer_callable_arguments_constraints(template, self.actual, self.direction)
698
+ raise RuntimeError("Parameters cannot be constrained to")
699
+
700
+ # Non-leaf types
701
+
702
+ def visit_instance(self, template: Instance) -> list[Constraint]:
703
+ original_actual = actual = self.actual
704
+ res: list[Constraint] = []
705
+ if isinstance(actual, (CallableType, Overloaded)) and template.type.is_protocol:
706
+ if "__call__" in template.type.protocol_members:
707
+ # Special case: a generic callback protocol
708
+ if not any(template == t for t in template.type.inferring):
709
+ template.type.inferring.append(template)
710
+ call = mypy.subtypes.find_member(
711
+ "__call__", template, actual, is_operator=True
712
+ )
713
+ assert call is not None
714
+ if mypy.subtypes.is_subtype(actual, erase_typevars(call)):
715
+ subres = infer_constraints(call, actual, self.direction)
716
+ res.extend(subres)
717
+ template.type.inferring.pop()
718
+ if isinstance(actual, CallableType) and actual.fallback is not None:
719
+ if actual.is_type_obj() and template.type.is_protocol:
720
+ ret_type = get_proper_type(actual.ret_type)
721
+ if isinstance(ret_type, TupleType):
722
+ ret_type = mypy.typeops.tuple_fallback(ret_type)
723
+ if isinstance(ret_type, Instance):
724
+ if self.direction == SUBTYPE_OF:
725
+ subtype = template
726
+ else:
727
+ subtype = ret_type
728
+ res.extend(
729
+ self.infer_constraints_from_protocol_members(
730
+ ret_type, template, subtype, template, class_obj=True
731
+ )
732
+ )
733
+ actual = actual.fallback
734
+ if isinstance(actual, TypeType) and template.type.is_protocol:
735
+ if isinstance(actual.item, Instance):
736
+ if self.direction == SUBTYPE_OF:
737
+ subtype = template
738
+ else:
739
+ subtype = actual.item
740
+ res.extend(
741
+ self.infer_constraints_from_protocol_members(
742
+ actual.item, template, subtype, template, class_obj=True
743
+ )
744
+ )
745
+ if self.direction == SUPERTYPE_OF:
746
+ # Infer constraints for Type[T] via metaclass of T when it makes sense.
747
+ a_item = actual.item
748
+ if isinstance(a_item, TypeVarType):
749
+ a_item = get_proper_type(a_item.upper_bound)
750
+ if isinstance(a_item, Instance) and a_item.type.metaclass_type:
751
+ res.extend(
752
+ self.infer_constraints_from_protocol_members(
753
+ a_item.type.metaclass_type, template, actual, template
754
+ )
755
+ )
756
+
757
+ if isinstance(actual, Overloaded) and actual.fallback is not None:
758
+ actual = actual.fallback
759
+ if isinstance(actual, TypedDictType):
760
+ actual = actual.as_anonymous().fallback
761
+ if isinstance(actual, LiteralType):
762
+ actual = actual.fallback
763
+ if isinstance(actual, Instance):
764
+ instance = actual
765
+ erased = erase_typevars(template)
766
+ assert isinstance(erased, Instance) # type: ignore[misc]
767
+ # We always try nominal inference if possible,
768
+ # it is much faster than the structural one.
769
+ if self.direction == SUBTYPE_OF and template.type.has_base(instance.type.fullname):
770
+ mapped = map_instance_to_supertype(template, instance.type)
771
+ tvars = mapped.type.defn.type_vars
772
+
773
+ if instance.type.has_type_var_tuple_type:
774
+ # Variadic types need special handling to map each type argument to
775
+ # the correct corresponding type variable.
776
+ assert instance.type.type_var_tuple_prefix is not None
777
+ assert instance.type.type_var_tuple_suffix is not None
778
+ prefix_len = instance.type.type_var_tuple_prefix
779
+ suffix_len = instance.type.type_var_tuple_suffix
780
+ tvt = instance.type.defn.type_vars[prefix_len]
781
+ assert isinstance(tvt, TypeVarTupleType)
782
+ fallback = tvt.tuple_fallback
783
+ i_prefix, i_middle, i_suffix = split_with_prefix_and_suffix(
784
+ instance.args, prefix_len, suffix_len
785
+ )
786
+ m_prefix, m_middle, m_suffix = split_with_prefix_and_suffix(
787
+ mapped.args, prefix_len, suffix_len
788
+ )
789
+ instance_args = i_prefix + (TupleType(list(i_middle), fallback),) + i_suffix
790
+ mapped_args = m_prefix + (TupleType(list(m_middle), fallback),) + m_suffix
791
+ else:
792
+ mapped_args = mapped.args
793
+ instance_args = instance.args
794
+
795
+ # N.B: We use zip instead of indexing because the lengths might have
796
+ # mismatches during daemon reprocessing.
797
+ for tvar, mapped_arg, instance_arg in zip(tvars, mapped_args, instance_args):
798
+ if isinstance(tvar, TypeVarType):
799
+ # The constraints for generic type parameters depend on variance.
800
+ # Include constraints from both directions if invariant.
801
+ if tvar.variance != CONTRAVARIANT:
802
+ res.extend(infer_constraints(mapped_arg, instance_arg, self.direction))
803
+ if tvar.variance != COVARIANT:
804
+ res.extend(
805
+ infer_constraints(mapped_arg, instance_arg, neg_op(self.direction))
806
+ )
807
+ elif isinstance(tvar, ParamSpecType) and isinstance(mapped_arg, ParamSpecType):
808
+ prefix = mapped_arg.prefix
809
+ if isinstance(instance_arg, Parameters):
810
+ # No such thing as variance for ParamSpecs, consider them invariant
811
+ # TODO: constraints between prefixes using
812
+ # infer_callable_arguments_constraints()
813
+ suffix: Type = instance_arg.copy_modified(
814
+ instance_arg.arg_types[len(prefix.arg_types) :],
815
+ instance_arg.arg_kinds[len(prefix.arg_kinds) :],
816
+ instance_arg.arg_names[len(prefix.arg_names) :],
817
+ )
818
+ res.append(Constraint(mapped_arg, SUBTYPE_OF, suffix))
819
+ res.append(Constraint(mapped_arg, SUPERTYPE_OF, suffix))
820
+ elif isinstance(instance_arg, ParamSpecType):
821
+ suffix = instance_arg.copy_modified(
822
+ prefix=Parameters(
823
+ instance_arg.prefix.arg_types[len(prefix.arg_types) :],
824
+ instance_arg.prefix.arg_kinds[len(prefix.arg_kinds) :],
825
+ instance_arg.prefix.arg_names[len(prefix.arg_names) :],
826
+ )
827
+ )
828
+ res.append(Constraint(mapped_arg, SUBTYPE_OF, suffix))
829
+ res.append(Constraint(mapped_arg, SUPERTYPE_OF, suffix))
830
+ elif isinstance(tvar, TypeVarTupleType):
831
+ # Handle variadic type variables covariantly for consistency.
832
+ res.extend(infer_constraints(mapped_arg, instance_arg, self.direction))
833
+
834
+ return res
835
+ elif self.direction == SUPERTYPE_OF and instance.type.has_base(template.type.fullname):
836
+ mapped = map_instance_to_supertype(instance, template.type)
837
+ tvars = template.type.defn.type_vars
838
+ if template.type.has_type_var_tuple_type:
839
+ # Variadic types need special handling to map each type argument to
840
+ # the correct corresponding type variable.
841
+ assert template.type.type_var_tuple_prefix is not None
842
+ assert template.type.type_var_tuple_suffix is not None
843
+ prefix_len = template.type.type_var_tuple_prefix
844
+ suffix_len = template.type.type_var_tuple_suffix
845
+ tvt = template.type.defn.type_vars[prefix_len]
846
+ assert isinstance(tvt, TypeVarTupleType)
847
+ fallback = tvt.tuple_fallback
848
+ t_prefix, t_middle, t_suffix = split_with_prefix_and_suffix(
849
+ template.args, prefix_len, suffix_len
850
+ )
851
+ m_prefix, m_middle, m_suffix = split_with_prefix_and_suffix(
852
+ mapped.args, prefix_len, suffix_len
853
+ )
854
+ template_args = t_prefix + (TupleType(list(t_middle), fallback),) + t_suffix
855
+ mapped_args = m_prefix + (TupleType(list(m_middle), fallback),) + m_suffix
856
+ else:
857
+ mapped_args = mapped.args
858
+ template_args = template.args
859
+ # N.B: We use zip instead of indexing because the lengths might have
860
+ # mismatches during daemon reprocessing.
861
+ for tvar, mapped_arg, template_arg in zip(tvars, mapped_args, template_args):
862
+ if isinstance(tvar, TypeVarType):
863
+ # The constraints for generic type parameters depend on variance.
864
+ # Include constraints from both directions if invariant.
865
+ if tvar.variance != CONTRAVARIANT:
866
+ res.extend(infer_constraints(template_arg, mapped_arg, self.direction))
867
+ if tvar.variance != COVARIANT:
868
+ res.extend(
869
+ infer_constraints(template_arg, mapped_arg, neg_op(self.direction))
870
+ )
871
+ elif isinstance(tvar, ParamSpecType) and isinstance(
872
+ template_arg, ParamSpecType
873
+ ):
874
+ prefix = template_arg.prefix
875
+ if isinstance(mapped_arg, Parameters):
876
+ # No such thing as variance for ParamSpecs, consider them invariant
877
+ # TODO: constraints between prefixes using
878
+ # infer_callable_arguments_constraints()
879
+ suffix = mapped_arg.copy_modified(
880
+ mapped_arg.arg_types[len(prefix.arg_types) :],
881
+ mapped_arg.arg_kinds[len(prefix.arg_kinds) :],
882
+ mapped_arg.arg_names[len(prefix.arg_names) :],
883
+ )
884
+ res.append(Constraint(template_arg, SUBTYPE_OF, suffix))
885
+ res.append(Constraint(template_arg, SUPERTYPE_OF, suffix))
886
+ elif isinstance(mapped_arg, ParamSpecType):
887
+ suffix = mapped_arg.copy_modified(
888
+ prefix=Parameters(
889
+ mapped_arg.prefix.arg_types[len(prefix.arg_types) :],
890
+ mapped_arg.prefix.arg_kinds[len(prefix.arg_kinds) :],
891
+ mapped_arg.prefix.arg_names[len(prefix.arg_names) :],
892
+ )
893
+ )
894
+ res.append(Constraint(template_arg, SUBTYPE_OF, suffix))
895
+ res.append(Constraint(template_arg, SUPERTYPE_OF, suffix))
896
+ elif isinstance(tvar, TypeVarTupleType):
897
+ # Consider variadic type variables to be invariant.
898
+ res.extend(infer_constraints(template_arg, mapped_arg, SUBTYPE_OF))
899
+ res.extend(infer_constraints(template_arg, mapped_arg, SUPERTYPE_OF))
900
+ return res
901
+ if (
902
+ template.type.is_protocol
903
+ and self.direction == SUPERTYPE_OF
904
+ and
905
+ # We avoid infinite recursion for structural subtypes by checking
906
+ # whether this type already appeared in the inference chain.
907
+ # This is a conservative way to break the inference cycles.
908
+ # It never produces any "false" constraints but gives up soon
909
+ # on purely structural inference cycles, see #3829.
910
+ # Note that we use is_protocol_implementation instead of is_subtype
911
+ # because some type may be considered a subtype of a protocol
912
+ # due to _promote, but still not implement the protocol.
913
+ not any(template == t for t in reversed(template.type.inferring))
914
+ and mypy.subtypes.is_protocol_implementation(instance, erased, skip=["__call__"])
915
+ ):
916
+ template.type.inferring.append(template)
917
+ res.extend(
918
+ self.infer_constraints_from_protocol_members(
919
+ instance, template, original_actual, template
920
+ )
921
+ )
922
+ template.type.inferring.pop()
923
+ return res
924
+ elif (
925
+ instance.type.is_protocol
926
+ and self.direction == SUBTYPE_OF
927
+ and
928
+ # We avoid infinite recursion for structural subtypes also here.
929
+ not any(instance == i for i in reversed(instance.type.inferring))
930
+ and mypy.subtypes.is_protocol_implementation(erased, instance, skip=["__call__"])
931
+ ):
932
+ instance.type.inferring.append(instance)
933
+ res.extend(
934
+ self.infer_constraints_from_protocol_members(
935
+ instance, template, template, instance
936
+ )
937
+ )
938
+ instance.type.inferring.pop()
939
+ return res
940
+ if res:
941
+ return res
942
+
943
+ if isinstance(actual, AnyType):
944
+ return self.infer_against_any(template.args, actual)
945
+ if (
946
+ isinstance(actual, TupleType)
947
+ and is_named_instance(template, TUPLE_LIKE_INSTANCE_NAMES)
948
+ and self.direction == SUPERTYPE_OF
949
+ ):
950
+ for item in actual.items:
951
+ if isinstance(item, UnpackType):
952
+ unpacked = get_proper_type(item.type)
953
+ if isinstance(unpacked, TypeVarTupleType):
954
+ # Cannot infer anything for T from [T, ...] <: *Ts
955
+ continue
956
+ assert (
957
+ isinstance(unpacked, Instance)
958
+ and unpacked.type.fullname == "builtins.tuple"
959
+ )
960
+ item = unpacked.args[0]
961
+ cb = infer_constraints(template.args[0], item, SUPERTYPE_OF)
962
+ res.extend(cb)
963
+ return res
964
+ elif isinstance(actual, TupleType) and self.direction == SUPERTYPE_OF:
965
+ return infer_constraints(template, mypy.typeops.tuple_fallback(actual), self.direction)
966
+ elif isinstance(actual, TypeVarType):
967
+ if not actual.values and not actual.id.is_meta_var():
968
+ return infer_constraints(template, actual.upper_bound, self.direction)
969
+ return []
970
+ elif isinstance(actual, ParamSpecType):
971
+ return infer_constraints(template, actual.upper_bound, self.direction)
972
+ elif isinstance(actual, TypeVarTupleType):
973
+ raise NotImplementedError
974
+ else:
975
+ return []
976
+
977
+ def infer_constraints_from_protocol_members(
978
+ self,
979
+ instance: Instance,
980
+ template: Instance,
981
+ subtype: Type,
982
+ protocol: Instance,
983
+ class_obj: bool = False,
984
+ ) -> list[Constraint]:
985
+ """Infer constraints for situations where either 'template' or 'instance' is a protocol.
986
+
987
+ The 'protocol' is the one of two that is an instance of protocol type, 'subtype'
988
+ is the type used to bind self during inference. Currently, we just infer constrains for
989
+ every protocol member type (both ways for settable members).
990
+ """
991
+ res = []
992
+ for member in protocol.type.protocol_members:
993
+ inst = mypy.subtypes.find_member(member, instance, subtype, class_obj=class_obj)
994
+ temp = mypy.subtypes.find_member(member, template, subtype)
995
+ if inst is None or temp is None:
996
+ if member == "__call__":
997
+ continue
998
+ return [] # See #11020
999
+ # The above is safe since at this point we know that 'instance' is a subtype
1000
+ # of (erased) 'template', therefore it defines all protocol members
1001
+ res.extend(infer_constraints(temp, inst, self.direction))
1002
+ if mypy.subtypes.IS_SETTABLE in mypy.subtypes.get_member_flags(member, protocol):
1003
+ # Settable members are invariant, add opposite constraints
1004
+ res.extend(infer_constraints(temp, inst, neg_op(self.direction)))
1005
+ return res
1006
+
1007
+ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
1008
+ # Normalize callables before matching against each other.
1009
+ # Note that non-normalized callables can be created in annotations
1010
+ # using e.g. callback protocols.
1011
+ # TODO: check that callables match? Ideally we should not infer constraints
1012
+ # callables that can never be subtypes of one another in given direction.
1013
+ template = template.with_unpacked_kwargs()
1014
+ extra_tvars = False
1015
+ if isinstance(self.actual, CallableType):
1016
+ res: list[Constraint] = []
1017
+ cactual = self.actual.with_unpacked_kwargs()
1018
+ param_spec = template.param_spec()
1019
+
1020
+ template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type
1021
+ if template.type_guard is not None:
1022
+ template_ret_type = template.type_guard
1023
+ if cactual.type_guard is not None:
1024
+ cactual_ret_type = cactual.type_guard
1025
+ res.extend(infer_constraints(template_ret_type, cactual_ret_type, self.direction))
1026
+
1027
+ if param_spec is None:
1028
+ # TODO: Erase template variables if it is generic?
1029
+ if (
1030
+ type_state.infer_polymorphic
1031
+ and cactual.variables
1032
+ and not self.skip_neg_op
1033
+ # Technically, the correct inferred type for application of e.g.
1034
+ # Callable[..., T] -> Callable[..., T] (with literal ellipsis), to a generic
1035
+ # like U -> U, should be Callable[..., Any], but if U is a self-type, we can
1036
+ # allow it to leak, to be later bound to self. A bunch of existing code
1037
+ # depends on this old behaviour.
1038
+ and not any(tv.id.raw_id == 0 for tv in cactual.variables)
1039
+ ):
1040
+ # If the actual callable is generic, infer constraints in the opposite
1041
+ # direction, and indicate to the solver there are extra type variables
1042
+ # to solve for (see more details in mypy/solve.py).
1043
+ res.extend(
1044
+ infer_constraints(
1045
+ cactual, template, neg_op(self.direction), skip_neg_op=True
1046
+ )
1047
+ )
1048
+ extra_tvars = True
1049
+
1050
+ # We can't infer constraints from arguments if the template is Callable[..., T]
1051
+ # (with literal '...').
1052
+ if not template.is_ellipsis_args:
1053
+ unpack_present = find_unpack_in_list(template.arg_types)
1054
+ if unpack_present is not None:
1055
+ # We need to re-normalize args to the form they appear in tuples,
1056
+ # for callables we always pack the suffix inside another tuple.
1057
+ unpack = template.arg_types[unpack_present]
1058
+ assert isinstance(unpack, UnpackType)
1059
+ tuple_type = get_tuple_fallback_from_unpack(unpack)
1060
+ template_types = repack_callable_args(template, tuple_type)
1061
+ actual_types = repack_callable_args(cactual, tuple_type)
1062
+ # Now we can use the same general helper as for tuple types.
1063
+ unpack_constraints = build_constraints_for_simple_unpack(
1064
+ template_types, actual_types, neg_op(self.direction)
1065
+ )
1066
+ res.extend(unpack_constraints)
1067
+ else:
1068
+ # TODO: do we need some special-casing when unpack is present in actual
1069
+ # callable but not in template callable?
1070
+ res.extend(
1071
+ infer_callable_arguments_constraints(template, cactual, self.direction)
1072
+ )
1073
+ else:
1074
+ prefix = param_spec.prefix
1075
+ prefix_len = len(prefix.arg_types)
1076
+ cactual_ps = cactual.param_spec()
1077
+
1078
+ if type_state.infer_polymorphic and cactual.variables and not self.skip_neg_op:
1079
+ # Similar logic to the branch above.
1080
+ res.extend(
1081
+ infer_constraints(
1082
+ cactual, template, neg_op(self.direction), skip_neg_op=True
1083
+ )
1084
+ )
1085
+ extra_tvars = True
1086
+
1087
+ # Compare prefixes as well
1088
+ cactual_prefix = cactual.copy_modified(
1089
+ arg_types=cactual.arg_types[:prefix_len],
1090
+ arg_kinds=cactual.arg_kinds[:prefix_len],
1091
+ arg_names=cactual.arg_names[:prefix_len],
1092
+ )
1093
+ res.extend(
1094
+ infer_callable_arguments_constraints(prefix, cactual_prefix, self.direction)
1095
+ )
1096
+
1097
+ param_spec_target: Type | None = None
1098
+ if not cactual_ps:
1099
+ max_prefix_len = len([k for k in cactual.arg_kinds if k in (ARG_POS, ARG_OPT)])
1100
+ prefix_len = min(prefix_len, max_prefix_len)
1101
+ param_spec_target = Parameters(
1102
+ arg_types=cactual.arg_types[prefix_len:],
1103
+ arg_kinds=cactual.arg_kinds[prefix_len:],
1104
+ arg_names=cactual.arg_names[prefix_len:],
1105
+ variables=cactual.variables if not type_state.infer_polymorphic else [],
1106
+ imprecise_arg_kinds=cactual.imprecise_arg_kinds,
1107
+ )
1108
+ else:
1109
+ if len(param_spec.prefix.arg_types) <= len(cactual_ps.prefix.arg_types):
1110
+ param_spec_target = cactual_ps.copy_modified(
1111
+ prefix=Parameters(
1112
+ arg_types=cactual_ps.prefix.arg_types[prefix_len:],
1113
+ arg_kinds=cactual_ps.prefix.arg_kinds[prefix_len:],
1114
+ arg_names=cactual_ps.prefix.arg_names[prefix_len:],
1115
+ imprecise_arg_kinds=cactual_ps.prefix.imprecise_arg_kinds,
1116
+ )
1117
+ )
1118
+ if param_spec_target is not None:
1119
+ res.append(Constraint(param_spec, self.direction, param_spec_target))
1120
+ if extra_tvars:
1121
+ for c in res:
1122
+ c.extra_tvars += cactual.variables
1123
+ return res
1124
+ elif isinstance(self.actual, AnyType):
1125
+ param_spec = template.param_spec()
1126
+ any_type = AnyType(TypeOfAny.from_another_any, source_any=self.actual)
1127
+ if param_spec is None:
1128
+ # FIX what if generic
1129
+ res = self.infer_against_any(template.arg_types, self.actual)
1130
+ else:
1131
+ res = [
1132
+ Constraint(
1133
+ param_spec,
1134
+ SUBTYPE_OF,
1135
+ Parameters([any_type, any_type], [ARG_STAR, ARG_STAR2], [None, None]),
1136
+ )
1137
+ ]
1138
+ res.extend(infer_constraints(template.ret_type, any_type, self.direction))
1139
+ return res
1140
+ elif isinstance(self.actual, Overloaded):
1141
+ return self.infer_against_overloaded(self.actual, template)
1142
+ elif isinstance(self.actual, TypeType):
1143
+ return infer_constraints(template.ret_type, self.actual.item, self.direction)
1144
+ elif isinstance(self.actual, Instance):
1145
+ # Instances with __call__ method defined are considered structural
1146
+ # subtypes of Callable with a compatible signature.
1147
+ call = mypy.subtypes.find_member(
1148
+ "__call__", self.actual, self.actual, is_operator=True
1149
+ )
1150
+ if call:
1151
+ return infer_constraints(template, call, self.direction)
1152
+ else:
1153
+ return []
1154
+ else:
1155
+ return []
1156
+
1157
+ def infer_against_overloaded(
1158
+ self, overloaded: Overloaded, template: CallableType
1159
+ ) -> list[Constraint]:
1160
+ # Create constraints by matching an overloaded type against a template.
1161
+ # This is tricky to do in general. We cheat by only matching against
1162
+ # the first overload item that is callable compatible. This
1163
+ # seems to work somewhat well, but we should really use a more
1164
+ # reliable technique.
1165
+ item = find_matching_overload_item(overloaded, template)
1166
+ return infer_constraints(template, item, self.direction)
1167
+
1168
+ def visit_tuple_type(self, template: TupleType) -> list[Constraint]:
1169
+ actual = self.actual
1170
+ unpack_index = find_unpack_in_list(template.items)
1171
+ is_varlength_tuple = (
1172
+ isinstance(actual, Instance) and actual.type.fullname == "builtins.tuple"
1173
+ )
1174
+
1175
+ if isinstance(actual, TupleType) or is_varlength_tuple:
1176
+ res: list[Constraint] = []
1177
+ if unpack_index is not None:
1178
+ if is_varlength_tuple:
1179
+ # Variadic tuple can be only a supertype of a tuple type, but even if
1180
+ # direction is opposite, inferring something may give better error messages.
1181
+ unpack_type = template.items[unpack_index]
1182
+ assert isinstance(unpack_type, UnpackType)
1183
+ unpacked_type = get_proper_type(unpack_type.type)
1184
+ if isinstance(unpacked_type, TypeVarTupleType):
1185
+ res = [
1186
+ Constraint(type_var=unpacked_type, op=self.direction, target=actual)
1187
+ ]
1188
+ else:
1189
+ assert (
1190
+ isinstance(unpacked_type, Instance)
1191
+ and unpacked_type.type.fullname == "builtins.tuple"
1192
+ )
1193
+ res = infer_constraints(unpacked_type, actual, self.direction)
1194
+ assert isinstance(actual, Instance) # ensured by is_varlength_tuple == True
1195
+ for i, ti in enumerate(template.items):
1196
+ if i == unpack_index:
1197
+ # This one we just handled above.
1198
+ continue
1199
+ # For Tuple[T, *Ts, S] <: tuple[X, ...] infer also T <: X and S <: X.
1200
+ res.extend(infer_constraints(ti, actual.args[0], self.direction))
1201
+ return res
1202
+ else:
1203
+ assert isinstance(actual, TupleType)
1204
+ unpack_constraints = build_constraints_for_simple_unpack(
1205
+ template.items, actual.items, self.direction
1206
+ )
1207
+ actual_items: tuple[Type, ...] = ()
1208
+ template_items: tuple[Type, ...] = ()
1209
+ res.extend(unpack_constraints)
1210
+ elif isinstance(actual, TupleType):
1211
+ a_unpack_index = find_unpack_in_list(actual.items)
1212
+ if a_unpack_index is not None:
1213
+ # The case where template tuple doesn't have an unpack, but actual tuple
1214
+ # has an unpack. We can infer something if actual unpack is a variadic tuple.
1215
+ # Tuple[T, S, U] <: tuple[X, *tuple[Y, ...], Z] => T <: X, S <: Y, U <: Z.
1216
+ a_unpack = actual.items[a_unpack_index]
1217
+ assert isinstance(a_unpack, UnpackType)
1218
+ a_unpacked = get_proper_type(a_unpack.type)
1219
+ if len(actual.items) + 1 <= len(template.items):
1220
+ a_prefix_len = a_unpack_index
1221
+ a_suffix_len = len(actual.items) - a_unpack_index - 1
1222
+ t_prefix, t_middle, t_suffix = split_with_prefix_and_suffix(
1223
+ tuple(template.items), a_prefix_len, a_suffix_len
1224
+ )
1225
+ actual_items = tuple(actual.items[:a_prefix_len])
1226
+ if a_suffix_len:
1227
+ actual_items += tuple(actual.items[-a_suffix_len:])
1228
+ template_items = t_prefix + t_suffix
1229
+ if isinstance(a_unpacked, Instance):
1230
+ assert a_unpacked.type.fullname == "builtins.tuple"
1231
+ for tm in t_middle:
1232
+ res.extend(
1233
+ infer_constraints(tm, a_unpacked.args[0], self.direction)
1234
+ )
1235
+ else:
1236
+ actual_items = ()
1237
+ template_items = ()
1238
+ else:
1239
+ actual_items = tuple(actual.items)
1240
+ template_items = tuple(template.items)
1241
+ else:
1242
+ return res
1243
+
1244
+ # Cases above will return if actual wasn't a TupleType.
1245
+ assert isinstance(actual, TupleType)
1246
+ if len(actual_items) == len(template_items):
1247
+ if (
1248
+ actual.partial_fallback.type.is_named_tuple
1249
+ and template.partial_fallback.type.is_named_tuple
1250
+ ):
1251
+ # For named tuples using just the fallbacks usually gives better results.
1252
+ return res + infer_constraints(
1253
+ template.partial_fallback, actual.partial_fallback, self.direction
1254
+ )
1255
+ for i in range(len(template_items)):
1256
+ res.extend(
1257
+ infer_constraints(template_items[i], actual_items[i], self.direction)
1258
+ )
1259
+ return res
1260
+ elif isinstance(actual, AnyType):
1261
+ return self.infer_against_any(template.items, actual)
1262
+ else:
1263
+ return []
1264
+
1265
+ def visit_typeddict_type(self, template: TypedDictType) -> list[Constraint]:
1266
+ actual = self.actual
1267
+ if isinstance(actual, TypedDictType):
1268
+ res: list[Constraint] = []
1269
+ # NOTE: Non-matching keys are ignored. Compatibility is checked
1270
+ # elsewhere so this shouldn't be unsafe.
1271
+ for item_name, template_item_type, actual_item_type in template.zip(actual):
1272
+ res.extend(infer_constraints(template_item_type, actual_item_type, self.direction))
1273
+ return res
1274
+ elif isinstance(actual, AnyType):
1275
+ return self.infer_against_any(template.items.values(), actual)
1276
+ else:
1277
+ return []
1278
+
1279
+ def visit_union_type(self, template: UnionType) -> list[Constraint]:
1280
+ assert False, (
1281
+ "Unexpected UnionType in ConstraintBuilderVisitor"
1282
+ " (should have been handled in infer_constraints)"
1283
+ )
1284
+
1285
+ def visit_type_alias_type(self, template: TypeAliasType) -> list[Constraint]:
1286
+ assert False, f"This should be never called, got {template}"
1287
+
1288
+ def infer_against_any(self, types: Iterable[Type], any_type: AnyType) -> list[Constraint]:
1289
+ res: list[Constraint] = []
1290
+ for t in types:
1291
+ if isinstance(t, UnpackType):
1292
+ if isinstance(t.type, TypeVarTupleType):
1293
+ res.append(Constraint(t.type, self.direction, any_type))
1294
+ else:
1295
+ unpacked = get_proper_type(t.type)
1296
+ assert isinstance(unpacked, Instance)
1297
+ res.extend(infer_constraints(unpacked, any_type, self.direction))
1298
+ else:
1299
+ # Note that we ignore variance and simply always use the
1300
+ # original direction. This is because for Any targets direction is
1301
+ # irrelevant in most cases, see e.g. is_same_constraint().
1302
+ res.extend(infer_constraints(t, any_type, self.direction))
1303
+ return res
1304
+
1305
+ def visit_overloaded(self, template: Overloaded) -> list[Constraint]:
1306
+ if isinstance(self.actual, CallableType):
1307
+ items = find_matching_overload_items(template, self.actual)
1308
+ else:
1309
+ items = template.items
1310
+ res: list[Constraint] = []
1311
+ for t in items:
1312
+ res.extend(infer_constraints(t, self.actual, self.direction))
1313
+ return res
1314
+
1315
+ def visit_type_type(self, template: TypeType) -> list[Constraint]:
1316
+ if isinstance(self.actual, CallableType):
1317
+ return infer_constraints(template.item, self.actual.ret_type, self.direction)
1318
+ elif isinstance(self.actual, Overloaded):
1319
+ return infer_constraints(template.item, self.actual.items[0].ret_type, self.direction)
1320
+ elif isinstance(self.actual, TypeType):
1321
+ return infer_constraints(template.item, self.actual.item, self.direction)
1322
+ elif isinstance(self.actual, AnyType):
1323
+ return infer_constraints(template.item, self.actual, self.direction)
1324
+ else:
1325
+ return []
1326
+
1327
+
1328
+ def neg_op(op: int) -> int:
1329
+ """Map SubtypeOf to SupertypeOf and vice versa."""
1330
+
1331
+ if op == SUBTYPE_OF:
1332
+ return SUPERTYPE_OF
1333
+ elif op == SUPERTYPE_OF:
1334
+ return SUBTYPE_OF
1335
+ else:
1336
+ raise ValueError(f"Invalid operator {op}")
1337
+
1338
+
1339
+ def find_matching_overload_item(overloaded: Overloaded, template: CallableType) -> CallableType:
1340
+ """Disambiguate overload item against a template."""
1341
+ items = overloaded.items
1342
+ for item in items:
1343
+ # Return type may be indeterminate in the template, so ignore it when performing a
1344
+ # subtype check.
1345
+ if mypy.subtypes.is_callable_compatible(
1346
+ item,
1347
+ template,
1348
+ is_compat=mypy.subtypes.is_subtype,
1349
+ is_proper_subtype=False,
1350
+ ignore_return=True,
1351
+ ):
1352
+ return item
1353
+ # Fall back to the first item if we can't find a match. This is totally arbitrary --
1354
+ # maybe we should just bail out at this point.
1355
+ return items[0]
1356
+
1357
+
1358
+ def find_matching_overload_items(
1359
+ overloaded: Overloaded, template: CallableType
1360
+ ) -> list[CallableType]:
1361
+ """Like find_matching_overload_item, but return all matches, not just the first."""
1362
+ items = overloaded.items
1363
+ res = []
1364
+ for item in items:
1365
+ # Return type may be indeterminate in the template, so ignore it when performing a
1366
+ # subtype check.
1367
+ if mypy.subtypes.is_callable_compatible(
1368
+ item,
1369
+ template,
1370
+ is_compat=mypy.subtypes.is_subtype,
1371
+ is_proper_subtype=False,
1372
+ ignore_return=True,
1373
+ ):
1374
+ res.append(item)
1375
+ if not res:
1376
+ # Falling back to all items if we can't find a match is pretty arbitrary, but
1377
+ # it maintains backward compatibility.
1378
+ res = items.copy()
1379
+ return res
1380
+
1381
+
1382
+ def get_tuple_fallback_from_unpack(unpack: UnpackType) -> TypeInfo:
1383
+ """Get builtins.tuple type from available types to construct homogeneous tuples."""
1384
+ tp = get_proper_type(unpack.type)
1385
+ if isinstance(tp, Instance) and tp.type.fullname == "builtins.tuple":
1386
+ return tp.type
1387
+ if isinstance(tp, TypeVarTupleType):
1388
+ return tp.tuple_fallback.type
1389
+ if isinstance(tp, TupleType):
1390
+ for base in tp.partial_fallback.type.mro:
1391
+ if base.fullname == "builtins.tuple":
1392
+ return base
1393
+ assert False, "Invalid unpack type"
1394
+
1395
+
1396
+ def repack_callable_args(callable: CallableType, tuple_type: TypeInfo) -> list[Type]:
1397
+ """Present callable with star unpack in a normalized form.
1398
+
1399
+ Since positional arguments cannot follow star argument, they are packed in a suffix,
1400
+ while prefix is represented as individual positional args. We want to put all in a single
1401
+ list with unpack in the middle, and prefix/suffix on the sides (as they would appear
1402
+ in e.g. a TupleType).
1403
+ """
1404
+ if ARG_STAR not in callable.arg_kinds:
1405
+ return callable.arg_types
1406
+ star_index = callable.arg_kinds.index(ARG_STAR)
1407
+ arg_types = callable.arg_types[:star_index]
1408
+ star_type = callable.arg_types[star_index]
1409
+ suffix_types = []
1410
+ if not isinstance(star_type, UnpackType):
1411
+ # Re-normalize *args: X -> *args: *tuple[X, ...]
1412
+ star_type = UnpackType(Instance(tuple_type, [star_type]))
1413
+ else:
1414
+ tp = get_proper_type(star_type.type)
1415
+ if isinstance(tp, TupleType):
1416
+ assert isinstance(tp.items[0], UnpackType)
1417
+ star_type = tp.items[0]
1418
+ suffix_types = tp.items[1:]
1419
+ return arg_types + [star_type] + suffix_types
1420
+
1421
+
1422
+ def build_constraints_for_simple_unpack(
1423
+ template_args: list[Type], actual_args: list[Type], direction: int
1424
+ ) -> list[Constraint]:
1425
+ """Infer constraints between two lists of types with variadic items.
1426
+
1427
+ This function is only supposed to be called when a variadic item is present in templates.
1428
+ If there is no variadic item the actuals, we simply use split_with_prefix_and_suffix()
1429
+ and infer prefix <: prefix, suffix <: suffix, variadic <: middle. If there is a variadic
1430
+ item in the actuals we need to be more careful, only common prefix/suffix can generate
1431
+ constraints, also we can only infer constraints for variadic template item, if template
1432
+ prefix/suffix are shorter that actual ones, otherwise there may be partial overlap
1433
+ between variadic items, for example if template prefix is longer:
1434
+
1435
+ templates: T1, T2, Ts, Ts, Ts, ...
1436
+ actuals: A1, As, As, As, ...
1437
+
1438
+ Note: this function can only be called for builtin variadic constructors: Tuple and Callable.
1439
+ For instances, you should first find correct type argument mapping.
1440
+ """
1441
+ template_unpack = find_unpack_in_list(template_args)
1442
+ assert template_unpack is not None
1443
+ template_prefix = template_unpack
1444
+ template_suffix = len(template_args) - template_prefix - 1
1445
+
1446
+ t_unpack = None
1447
+ res = []
1448
+
1449
+ actual_unpack = find_unpack_in_list(actual_args)
1450
+ if actual_unpack is None:
1451
+ t_unpack = template_args[template_unpack]
1452
+ if template_prefix + template_suffix > len(actual_args):
1453
+ # These can't be subtypes of each-other, return fast.
1454
+ assert isinstance(t_unpack, UnpackType)
1455
+ if isinstance(t_unpack.type, TypeVarTupleType):
1456
+ # Set TypeVarTuple to empty to improve error messages.
1457
+ return [
1458
+ Constraint(
1459
+ t_unpack.type, direction, TupleType([], t_unpack.type.tuple_fallback)
1460
+ )
1461
+ ]
1462
+ else:
1463
+ return []
1464
+ common_prefix = template_prefix
1465
+ common_suffix = template_suffix
1466
+ else:
1467
+ actual_prefix = actual_unpack
1468
+ actual_suffix = len(actual_args) - actual_prefix - 1
1469
+ common_prefix = min(template_prefix, actual_prefix)
1470
+ common_suffix = min(template_suffix, actual_suffix)
1471
+ if actual_prefix >= template_prefix and actual_suffix >= template_suffix:
1472
+ # This is the only case where we can guarantee there will be no partial overlap
1473
+ # (note however partial overlap is OK for variadic tuples, it is handled below).
1474
+ t_unpack = template_args[template_unpack]
1475
+
1476
+ # Handle constraints from prefixes/suffixes first.
1477
+ start, middle, end = split_with_prefix_and_suffix(
1478
+ tuple(actual_args), common_prefix, common_suffix
1479
+ )
1480
+ for t, a in zip(template_args[:common_prefix], start):
1481
+ res.extend(infer_constraints(t, a, direction))
1482
+ if common_suffix:
1483
+ for t, a in zip(template_args[-common_suffix:], end):
1484
+ res.extend(infer_constraints(t, a, direction))
1485
+
1486
+ if t_unpack is not None:
1487
+ # Add constraint(s) for variadic item when possible.
1488
+ assert isinstance(t_unpack, UnpackType)
1489
+ tp = get_proper_type(t_unpack.type)
1490
+ if isinstance(tp, Instance) and tp.type.fullname == "builtins.tuple":
1491
+ # Homogeneous case *tuple[T, ...] <: [X, Y, Z, ...].
1492
+ for a in middle:
1493
+ # TODO: should we use union instead of join here?
1494
+ if not isinstance(a, UnpackType):
1495
+ res.extend(infer_constraints(tp.args[0], a, direction))
1496
+ else:
1497
+ a_tp = get_proper_type(a.type)
1498
+ # This is the case *tuple[T, ...] <: *tuple[A, ...].
1499
+ if isinstance(a_tp, Instance) and a_tp.type.fullname == "builtins.tuple":
1500
+ res.extend(infer_constraints(tp.args[0], a_tp.args[0], direction))
1501
+ elif isinstance(tp, TypeVarTupleType):
1502
+ res.append(Constraint(tp, direction, TupleType(list(middle), tp.tuple_fallback)))
1503
+ elif actual_unpack is not None:
1504
+ # A special case for a variadic tuple unpack, we simply infer T <: X from
1505
+ # Tuple[..., *tuple[T, ...], ...] <: Tuple[..., *tuple[X, ...], ...].
1506
+ actual_unpack_type = actual_args[actual_unpack]
1507
+ assert isinstance(actual_unpack_type, UnpackType)
1508
+ a_unpacked = get_proper_type(actual_unpack_type.type)
1509
+ if isinstance(a_unpacked, Instance) and a_unpacked.type.fullname == "builtins.tuple":
1510
+ t_unpack = template_args[template_unpack]
1511
+ assert isinstance(t_unpack, UnpackType)
1512
+ tp = get_proper_type(t_unpack.type)
1513
+ if isinstance(tp, Instance) and tp.type.fullname == "builtins.tuple":
1514
+ res.extend(infer_constraints(tp.args[0], a_unpacked.args[0], direction))
1515
+ return res
1516
+
1517
+
1518
+ def infer_directed_arg_constraints(left: Type, right: Type, direction: int) -> list[Constraint]:
1519
+ """Infer constraints between two arguments using direction between original callables."""
1520
+ if isinstance(left, (ParamSpecType, UnpackType)) or isinstance(
1521
+ right, (ParamSpecType, UnpackType)
1522
+ ):
1523
+ # This avoids bogus constraints like T <: P.args
1524
+ # TODO: can we infer something useful for *T vs P?
1525
+ return []
1526
+ if direction == SUBTYPE_OF:
1527
+ # We invert direction to account for argument contravariance.
1528
+ return infer_constraints(left, right, neg_op(direction))
1529
+ else:
1530
+ return infer_constraints(right, left, neg_op(direction))
1531
+
1532
+
1533
+ def infer_callable_arguments_constraints(
1534
+ template: NormalizedCallableType | Parameters,
1535
+ actual: NormalizedCallableType | Parameters,
1536
+ direction: int,
1537
+ ) -> list[Constraint]:
1538
+ """Infer constraints between argument types of two callables.
1539
+
1540
+ This function essentially extracts four steps from are_parameters_compatible() in
1541
+ subtypes.py that involve subtype checks between argument types. We keep the argument
1542
+ matching logic, but ignore various strictness flags present there, and checks that
1543
+ do not involve subtyping. Then in place of every subtype check we put an infer_constraints()
1544
+ call for the same types.
1545
+ """
1546
+ res = []
1547
+ if direction == SUBTYPE_OF:
1548
+ left, right = template, actual
1549
+ else:
1550
+ left, right = actual, template
1551
+ left_star = left.var_arg()
1552
+ left_star2 = left.kw_arg()
1553
+ right_star = right.var_arg()
1554
+ right_star2 = right.kw_arg()
1555
+
1556
+ # Numbering of steps below matches the one in are_parameters_compatible() for convenience.
1557
+ # Phase 1a: compare star vs star arguments.
1558
+ if left_star is not None and right_star is not None:
1559
+ res.extend(infer_directed_arg_constraints(left_star.typ, right_star.typ, direction))
1560
+ if left_star2 is not None and right_star2 is not None:
1561
+ res.extend(infer_directed_arg_constraints(left_star2.typ, right_star2.typ, direction))
1562
+
1563
+ # Phase 1b: compare left args with corresponding non-star right arguments.
1564
+ for right_arg in right.formal_arguments():
1565
+ left_arg = mypy.typeops.callable_corresponding_argument(left, right_arg)
1566
+ if left_arg is None:
1567
+ continue
1568
+ res.extend(infer_directed_arg_constraints(left_arg.typ, right_arg.typ, direction))
1569
+
1570
+ # Phase 1c: compare left args with right *args.
1571
+ if right_star is not None:
1572
+ right_by_position = right.try_synthesizing_arg_from_vararg(None)
1573
+ assert right_by_position is not None
1574
+ i = right_star.pos
1575
+ assert i is not None
1576
+ while i < len(left.arg_kinds) and left.arg_kinds[i].is_positional():
1577
+ left_by_position = left.argument_by_position(i)
1578
+ assert left_by_position is not None
1579
+ res.extend(
1580
+ infer_directed_arg_constraints(
1581
+ left_by_position.typ, right_by_position.typ, direction
1582
+ )
1583
+ )
1584
+ i += 1
1585
+
1586
+ # Phase 1d: compare left args with right **kwargs.
1587
+ if right_star2 is not None:
1588
+ right_names = {name for name in right.arg_names if name is not None}
1589
+ left_only_names = set()
1590
+ for name, kind in zip(left.arg_names, left.arg_kinds):
1591
+ if name is None or kind.is_star() or name in right_names:
1592
+ continue
1593
+ left_only_names.add(name)
1594
+
1595
+ right_by_name = right.try_synthesizing_arg_from_kwarg(None)
1596
+ assert right_by_name is not None
1597
+ for name in left_only_names:
1598
+ left_by_name = left.argument_by_name(name)
1599
+ assert left_by_name is not None
1600
+ res.extend(
1601
+ infer_directed_arg_constraints(left_by_name.typ, right_by_name.typ, direction)
1602
+ )
1603
+ return res
1604
+
1605
+
1606
+ def filter_imprecise_kinds(cs: list[Constraint]) -> list[Constraint]:
1607
+ """For each ParamSpec remove all imprecise constraints, if at least one precise available."""
1608
+ have_precise = set()
1609
+ for c in cs:
1610
+ if not isinstance(c.origin_type_var, ParamSpecType):
1611
+ continue
1612
+ if (
1613
+ isinstance(c.target, ParamSpecType)
1614
+ or isinstance(c.target, Parameters)
1615
+ and not c.target.imprecise_arg_kinds
1616
+ ):
1617
+ have_precise.add(c.type_var)
1618
+ new_cs = []
1619
+ for c in cs:
1620
+ if not isinstance(c.origin_type_var, ParamSpecType) or c.type_var not in have_precise:
1621
+ new_cs.append(c)
1622
+ if not isinstance(c.target, Parameters) or not c.target.imprecise_arg_kinds:
1623
+ new_cs.append(c)
1624
+ return new_cs