messages.py 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130
  1. """Facilities for generating error messages during type checking.
  2. Don't add any non-trivial message construction logic to the type
  3. checker, as it can compromise clarity and make messages less
  4. consistent. Add such logic to this module instead. Literal messages, including those
  5. with format args, should be defined as constants in mypy.message_registry.
  6. Historically we tried to avoid all message string literals in the type
  7. checker but we are moving away from this convention.
  8. """
  9. from __future__ import annotations
  10. import difflib
  11. import itertools
  12. import re
  13. from contextlib import contextmanager
  14. from textwrap import dedent
  15. from typing import Any, Callable, Collection, Final, Iterable, Iterator, List, Sequence, cast
  16. import mypy.typeops
  17. from mypy import errorcodes as codes, message_registry
  18. from mypy.erasetype import erase_type
  19. from mypy.errorcodes import ErrorCode
  20. from mypy.errors import ErrorInfo, Errors, ErrorWatcher
  21. from mypy.nodes import (
  22. ARG_NAMED,
  23. ARG_NAMED_OPT,
  24. ARG_OPT,
  25. ARG_POS,
  26. ARG_STAR,
  27. ARG_STAR2,
  28. CONTRAVARIANT,
  29. COVARIANT,
  30. SYMBOL_FUNCBASE_TYPES,
  31. ArgKind,
  32. CallExpr,
  33. ClassDef,
  34. Context,
  35. Expression,
  36. FuncDef,
  37. IndexExpr,
  38. MypyFile,
  39. NameExpr,
  40. ReturnStmt,
  41. StrExpr,
  42. SymbolNode,
  43. SymbolTable,
  44. TypeInfo,
  45. Var,
  46. reverse_builtin_aliases,
  47. )
  48. from mypy.operators import op_methods, op_methods_to_symbols
  49. from mypy.options import Options
  50. from mypy.subtypes import (
  51. IS_CLASS_OR_STATIC,
  52. IS_CLASSVAR,
  53. IS_SETTABLE,
  54. IS_VAR,
  55. find_member,
  56. get_member_flags,
  57. is_same_type,
  58. is_subtype,
  59. )
  60. from mypy.typeops import separate_union_literals
  61. from mypy.types import (
  62. AnyType,
  63. CallableType,
  64. DeletedType,
  65. FunctionLike,
  66. Instance,
  67. LiteralType,
  68. NoneType,
  69. Overloaded,
  70. Parameters,
  71. ParamSpecType,
  72. PartialType,
  73. ProperType,
  74. TupleType,
  75. Type,
  76. TypeAliasType,
  77. TypedDictType,
  78. TypeOfAny,
  79. TypeStrVisitor,
  80. TypeType,
  81. TypeVarTupleType,
  82. TypeVarType,
  83. UnboundType,
  84. UninhabitedType,
  85. UnionType,
  86. UnpackType,
  87. get_proper_type,
  88. get_proper_types,
  89. )
  90. from mypy.typetraverser import TypeTraverserVisitor
  91. from mypy.util import plural_s, unmangle
  92. TYPES_FOR_UNIMPORTED_HINTS: Final = {
  93. "typing.Any",
  94. "typing.Callable",
  95. "typing.Dict",
  96. "typing.Iterable",
  97. "typing.Iterator",
  98. "typing.List",
  99. "typing.Optional",
  100. "typing.Set",
  101. "typing.Tuple",
  102. "typing.TypeVar",
  103. "typing.Union",
  104. "typing.cast",
  105. }
  106. ARG_CONSTRUCTOR_NAMES: Final = {
  107. ARG_POS: "Arg",
  108. ARG_OPT: "DefaultArg",
  109. ARG_NAMED: "NamedArg",
  110. ARG_NAMED_OPT: "DefaultNamedArg",
  111. ARG_STAR: "VarArg",
  112. ARG_STAR2: "KwArg",
  113. }
  114. # Map from the full name of a missing definition to the test fixture (under
  115. # test-data/unit/fixtures/) that provides the definition. This is used for
  116. # generating better error messages when running mypy tests only.
  117. SUGGESTED_TEST_FIXTURES: Final = {
  118. "builtins.set": "set.pyi",
  119. "builtins.tuple": "tuple.pyi",
  120. "builtins.bool": "bool.pyi",
  121. "builtins.Exception": "exception.pyi",
  122. "builtins.BaseException": "exception.pyi",
  123. "builtins.isinstance": "isinstancelist.pyi",
  124. "builtins.property": "property.pyi",
  125. "builtins.classmethod": "classmethod.pyi",
  126. "typing._SpecialForm": "typing-medium.pyi",
  127. }
  128. UNSUPPORTED_NUMBERS_TYPES: Final = {
  129. "numbers.Number",
  130. "numbers.Complex",
  131. "numbers.Real",
  132. "numbers.Rational",
  133. "numbers.Integral",
  134. }
  135. class MessageBuilder:
  136. """Helper class for reporting type checker error messages with parameters.
  137. The methods of this class need to be provided with the context within a
  138. file; the errors member manages the wider context.
  139. IDEA: Support a 'verbose mode' that includes full information about types
  140. in error messages and that may otherwise produce more detailed error
  141. messages.
  142. """
  143. # Report errors using this instance. It knows about the current file and
  144. # import context.
  145. errors: Errors
  146. modules: dict[str, MypyFile]
  147. # Hack to deduplicate error messages from union types
  148. _disable_type_names: list[bool]
  149. def __init__(self, errors: Errors, modules: dict[str, MypyFile]) -> None:
  150. self.errors = errors
  151. self.options = errors.options
  152. self.modules = modules
  153. self._disable_type_names = []
  154. #
  155. # Helpers
  156. #
  157. def filter_errors(
  158. self,
  159. *,
  160. filter_errors: bool | Callable[[str, ErrorInfo], bool] = True,
  161. save_filtered_errors: bool = False,
  162. ) -> ErrorWatcher:
  163. return ErrorWatcher(
  164. self.errors, filter_errors=filter_errors, save_filtered_errors=save_filtered_errors
  165. )
  166. def add_errors(self, errors: list[ErrorInfo]) -> None:
  167. """Add errors in messages to this builder."""
  168. for info in errors:
  169. self.errors.add_error_info(info)
  170. @contextmanager
  171. def disable_type_names(self) -> Iterator[None]:
  172. self._disable_type_names.append(True)
  173. try:
  174. yield
  175. finally:
  176. self._disable_type_names.pop()
  177. def are_type_names_disabled(self) -> bool:
  178. return len(self._disable_type_names) > 0 and self._disable_type_names[-1]
  179. def prefer_simple_messages(self) -> bool:
  180. """Should we generate simple/fast error messages?
  181. If errors aren't shown to the user, we don't want to waste cyles producing
  182. complex error messages.
  183. """
  184. return self.errors.prefer_simple_messages()
  185. def report(
  186. self,
  187. msg: str,
  188. context: Context | None,
  189. severity: str,
  190. *,
  191. code: ErrorCode | None = None,
  192. file: str | None = None,
  193. origin: Context | None = None,
  194. offset: int = 0,
  195. allow_dups: bool = False,
  196. secondary_context: Context | None = None,
  197. ) -> None:
  198. """Report an error or note (unless disabled).
  199. Note that context controls where error is reported, while origin controls
  200. where # type: ignore comments have effect.
  201. """
  202. def span_from_context(ctx: Context) -> Iterable[int]:
  203. """This determines where a type: ignore for a given context has effect.
  204. Current logic is a bit tricky, to keep as much backwards compatibility as
  205. possible. We may reconsider this to always be a single line (or otherwise
  206. simplify it) when we drop Python 3.7.
  207. TODO: address this in follow up PR
  208. """
  209. if isinstance(ctx, (ClassDef, FuncDef)):
  210. return range(ctx.deco_line or ctx.line, ctx.line + 1)
  211. elif not isinstance(ctx, Expression):
  212. return [ctx.line]
  213. else:
  214. return range(ctx.line, (ctx.end_line or ctx.line) + 1)
  215. origin_span: Iterable[int] | None
  216. if origin is not None:
  217. origin_span = span_from_context(origin)
  218. elif context is not None:
  219. origin_span = span_from_context(context)
  220. else:
  221. origin_span = None
  222. if secondary_context is not None:
  223. assert origin_span is not None
  224. origin_span = itertools.chain(origin_span, span_from_context(secondary_context))
  225. self.errors.report(
  226. context.line if context else -1,
  227. context.column if context else -1,
  228. msg,
  229. severity=severity,
  230. file=file,
  231. offset=offset,
  232. origin_span=origin_span,
  233. end_line=context.end_line if context else -1,
  234. end_column=context.end_column if context else -1,
  235. code=code,
  236. allow_dups=allow_dups,
  237. )
  238. def fail(
  239. self,
  240. msg: str,
  241. context: Context | None,
  242. *,
  243. code: ErrorCode | None = None,
  244. file: str | None = None,
  245. allow_dups: bool = False,
  246. secondary_context: Context | None = None,
  247. ) -> None:
  248. """Report an error message (unless disabled)."""
  249. self.report(
  250. msg,
  251. context,
  252. "error",
  253. code=code,
  254. file=file,
  255. allow_dups=allow_dups,
  256. secondary_context=secondary_context,
  257. )
  258. def note(
  259. self,
  260. msg: str,
  261. context: Context,
  262. file: str | None = None,
  263. origin: Context | None = None,
  264. offset: int = 0,
  265. allow_dups: bool = False,
  266. *,
  267. code: ErrorCode | None = None,
  268. secondary_context: Context | None = None,
  269. ) -> None:
  270. """Report a note (unless disabled)."""
  271. self.report(
  272. msg,
  273. context,
  274. "note",
  275. file=file,
  276. origin=origin,
  277. offset=offset,
  278. allow_dups=allow_dups,
  279. code=code,
  280. secondary_context=secondary_context,
  281. )
  282. def note_multiline(
  283. self,
  284. messages: str,
  285. context: Context,
  286. file: str | None = None,
  287. offset: int = 0,
  288. allow_dups: bool = False,
  289. code: ErrorCode | None = None,
  290. *,
  291. secondary_context: Context | None = None,
  292. ) -> None:
  293. """Report as many notes as lines in the message (unless disabled)."""
  294. for msg in messages.splitlines():
  295. self.report(
  296. msg,
  297. context,
  298. "note",
  299. file=file,
  300. offset=offset,
  301. allow_dups=allow_dups,
  302. code=code,
  303. secondary_context=secondary_context,
  304. )
  305. #
  306. # Specific operations
  307. #
  308. # The following operations are for generating specific error messages. They
  309. # get some information as arguments, and they build an error message based
  310. # on them.
  311. def has_no_attr(
  312. self,
  313. original_type: Type,
  314. typ: Type,
  315. member: str,
  316. context: Context,
  317. module_symbol_table: SymbolTable | None = None,
  318. ) -> Type:
  319. """Report a missing or non-accessible member.
  320. original_type is the top-level type on which the error occurred.
  321. typ is the actual type that is missing the member. These can be
  322. different, e.g., in a union, original_type will be the union and typ
  323. will be the specific item in the union that does not have the member
  324. attribute.
  325. 'module_symbol_table' is passed to this function if the type for which we
  326. are trying to get a member was originally a module. The SymbolTable allows
  327. us to look up and suggests attributes of the module since they are not
  328. directly available on original_type
  329. If member corresponds to an operator, use the corresponding operator
  330. name in the messages. Return type Any.
  331. """
  332. original_type = get_proper_type(original_type)
  333. typ = get_proper_type(typ)
  334. if isinstance(original_type, Instance) and original_type.type.has_readable_member(member):
  335. self.fail(f'Member "{member}" is not assignable', context)
  336. elif member == "__contains__":
  337. self.fail(
  338. f"Unsupported right operand type for in ({format_type(original_type, self.options)})",
  339. context,
  340. code=codes.OPERATOR,
  341. )
  342. elif member in op_methods.values():
  343. # Access to a binary operator member (e.g. _add). This case does
  344. # not handle indexing operations.
  345. for op, method in op_methods.items():
  346. if method == member:
  347. self.unsupported_left_operand(op, original_type, context)
  348. break
  349. elif member == "__neg__":
  350. self.fail(
  351. f"Unsupported operand type for unary - ({format_type(original_type, self.options)})",
  352. context,
  353. code=codes.OPERATOR,
  354. )
  355. elif member == "__pos__":
  356. self.fail(
  357. f"Unsupported operand type for unary + ({format_type(original_type, self.options)})",
  358. context,
  359. code=codes.OPERATOR,
  360. )
  361. elif member == "__invert__":
  362. self.fail(
  363. f"Unsupported operand type for ~ ({format_type(original_type, self.options)})",
  364. context,
  365. code=codes.OPERATOR,
  366. )
  367. elif member == "__getitem__":
  368. # Indexed get.
  369. # TODO: Fix this consistently in format_type
  370. if isinstance(original_type, CallableType) and original_type.is_type_obj():
  371. self.fail(
  372. "The type {} is not generic and not indexable".format(
  373. format_type(original_type, self.options)
  374. ),
  375. context,
  376. )
  377. else:
  378. self.fail(
  379. f"Value of type {format_type(original_type, self.options)} is not indexable",
  380. context,
  381. code=codes.INDEX,
  382. )
  383. elif member == "__setitem__":
  384. # Indexed set.
  385. self.fail(
  386. "Unsupported target for indexed assignment ({})".format(
  387. format_type(original_type, self.options)
  388. ),
  389. context,
  390. code=codes.INDEX,
  391. )
  392. elif member == "__call__":
  393. if isinstance(original_type, Instance) and (
  394. original_type.type.fullname == "builtins.function"
  395. ):
  396. # "'function' not callable" is a confusing error message.
  397. # Explain that the problem is that the type of the function is not known.
  398. self.fail("Cannot call function of unknown type", context, code=codes.OPERATOR)
  399. else:
  400. self.fail(
  401. message_registry.NOT_CALLABLE.format(format_type(original_type, self.options)),
  402. context,
  403. code=codes.OPERATOR,
  404. )
  405. else:
  406. # The non-special case: a missing ordinary attribute.
  407. extra = ""
  408. if member == "__iter__":
  409. extra = " (not iterable)"
  410. elif member == "__aiter__":
  411. extra = " (not async iterable)"
  412. if not self.are_type_names_disabled():
  413. failed = False
  414. if isinstance(original_type, Instance) and original_type.type.names:
  415. if (
  416. module_symbol_table is not None
  417. and member in module_symbol_table
  418. and not module_symbol_table[member].module_public
  419. ):
  420. self.fail(
  421. f"{format_type(original_type, self.options, module_names=True)} does not "
  422. f'explicitly export attribute "{member}"',
  423. context,
  424. code=codes.ATTR_DEFINED,
  425. )
  426. failed = True
  427. else:
  428. alternatives = set(original_type.type.names.keys())
  429. if module_symbol_table is not None:
  430. alternatives |= {
  431. k for k, v in module_symbol_table.items() if v.module_public
  432. }
  433. # Rare but possible, see e.g. testNewAnalyzerCyclicDefinitionCrossModule
  434. alternatives.discard(member)
  435. matches = [m for m in COMMON_MISTAKES.get(member, []) if m in alternatives]
  436. matches.extend(best_matches(member, alternatives, n=3))
  437. if member == "__aiter__" and matches == ["__iter__"]:
  438. matches = [] # Avoid misleading suggestion
  439. if matches:
  440. self.fail(
  441. '{} has no attribute "{}"; maybe {}?{}'.format(
  442. format_type(original_type, self.options),
  443. member,
  444. pretty_seq(matches, "or"),
  445. extra,
  446. ),
  447. context,
  448. code=codes.ATTR_DEFINED,
  449. )
  450. failed = True
  451. if not failed:
  452. self.fail(
  453. '{} has no attribute "{}"{}'.format(
  454. format_type(original_type, self.options), member, extra
  455. ),
  456. context,
  457. code=codes.ATTR_DEFINED,
  458. )
  459. elif isinstance(original_type, UnionType):
  460. # The checker passes "object" in lieu of "None" for attribute
  461. # checks, so we manually convert it back.
  462. typ_format, orig_type_format = format_type_distinctly(
  463. typ, original_type, options=self.options
  464. )
  465. if typ_format == '"object"' and any(
  466. type(item) == NoneType for item in original_type.items
  467. ):
  468. typ_format = '"None"'
  469. self.fail(
  470. 'Item {} of {} has no attribute "{}"{}'.format(
  471. typ_format, orig_type_format, member, extra
  472. ),
  473. context,
  474. code=codes.UNION_ATTR,
  475. )
  476. elif isinstance(original_type, TypeVarType):
  477. bound = get_proper_type(original_type.upper_bound)
  478. if isinstance(bound, UnionType):
  479. typ_fmt, bound_fmt = format_type_distinctly(typ, bound, options=self.options)
  480. original_type_fmt = format_type(original_type, self.options)
  481. self.fail(
  482. "Item {} of the upper bound {} of type variable {} has no "
  483. 'attribute "{}"{}'.format(
  484. typ_fmt, bound_fmt, original_type_fmt, member, extra
  485. ),
  486. context,
  487. code=codes.UNION_ATTR,
  488. )
  489. else:
  490. self.fail(
  491. '{} has no attribute "{}"{}'.format(
  492. format_type(original_type, self.options), member, extra
  493. ),
  494. context,
  495. code=codes.ATTR_DEFINED,
  496. )
  497. return AnyType(TypeOfAny.from_error)
  498. def unsupported_operand_types(
  499. self,
  500. op: str,
  501. left_type: Any,
  502. right_type: Any,
  503. context: Context,
  504. *,
  505. code: ErrorCode = codes.OPERATOR,
  506. ) -> None:
  507. """Report unsupported operand types for a binary operation.
  508. Types can be Type objects or strings.
  509. """
  510. left_str = ""
  511. if isinstance(left_type, str):
  512. left_str = left_type
  513. else:
  514. left_str = format_type(left_type, self.options)
  515. right_str = ""
  516. if isinstance(right_type, str):
  517. right_str = right_type
  518. else:
  519. right_str = format_type(right_type, self.options)
  520. if self.are_type_names_disabled():
  521. msg = f"Unsupported operand types for {op} (likely involving Union)"
  522. else:
  523. msg = f"Unsupported operand types for {op} ({left_str} and {right_str})"
  524. self.fail(msg, context, code=code)
  525. def unsupported_left_operand(self, op: str, typ: Type, context: Context) -> None:
  526. if self.are_type_names_disabled():
  527. msg = f"Unsupported left operand type for {op} (some union)"
  528. else:
  529. msg = f"Unsupported left operand type for {op} ({format_type(typ, self.options)})"
  530. self.fail(msg, context, code=codes.OPERATOR)
  531. def not_callable(self, typ: Type, context: Context) -> Type:
  532. self.fail(message_registry.NOT_CALLABLE.format(format_type(typ, self.options)), context)
  533. return AnyType(TypeOfAny.from_error)
  534. def untyped_function_call(self, callee: CallableType, context: Context) -> Type:
  535. name = callable_name(callee) or "(unknown)"
  536. self.fail(
  537. f"Call to untyped function {name} in typed context",
  538. context,
  539. code=codes.NO_UNTYPED_CALL,
  540. )
  541. return AnyType(TypeOfAny.from_error)
  542. def incompatible_argument(
  543. self,
  544. n: int,
  545. m: int,
  546. callee: CallableType,
  547. arg_type: Type,
  548. arg_kind: ArgKind,
  549. object_type: Type | None,
  550. context: Context,
  551. outer_context: Context,
  552. ) -> ErrorCode | None:
  553. """Report an error about an incompatible argument type.
  554. The argument type is arg_type, argument number is n and the
  555. callee type is 'callee'. If the callee represents a method
  556. that corresponds to an operator, use the corresponding
  557. operator name in the messages.
  558. Return the error code that used for the argument (multiple error
  559. codes are possible).
  560. """
  561. arg_type = get_proper_type(arg_type)
  562. target = ""
  563. callee_name = callable_name(callee)
  564. if callee_name is not None:
  565. name = callee_name
  566. if callee.bound_args and callee.bound_args[0] is not None:
  567. base = format_type(callee.bound_args[0], self.options)
  568. else:
  569. base = extract_type(name)
  570. for method, op in op_methods_to_symbols.items():
  571. for variant in method, "__r" + method[2:]:
  572. # FIX: do not rely on textual formatting
  573. if name.startswith(f'"{variant}" of'):
  574. if op == "in" or variant != method:
  575. # Reversed order of base/argument.
  576. self.unsupported_operand_types(
  577. op, arg_type, base, context, code=codes.OPERATOR
  578. )
  579. else:
  580. self.unsupported_operand_types(
  581. op, base, arg_type, context, code=codes.OPERATOR
  582. )
  583. return codes.OPERATOR
  584. if name.startswith('"__getitem__" of'):
  585. self.invalid_index_type(
  586. arg_type, callee.arg_types[n - 1], base, context, code=codes.INDEX
  587. )
  588. return codes.INDEX
  589. if name.startswith('"__setitem__" of'):
  590. if n == 1:
  591. self.invalid_index_type(
  592. arg_type, callee.arg_types[n - 1], base, context, code=codes.INDEX
  593. )
  594. return codes.INDEX
  595. else:
  596. arg_type_str, callee_type_str = format_type_distinctly(
  597. arg_type, callee.arg_types[n - 1], options=self.options
  598. )
  599. info = (
  600. f" (expression has type {arg_type_str}, "
  601. f"target has type {callee_type_str})"
  602. )
  603. error_msg = (
  604. message_registry.INCOMPATIBLE_TYPES_IN_ASSIGNMENT.with_additional_msg(info)
  605. )
  606. self.fail(error_msg.value, context, code=error_msg.code)
  607. return error_msg.code
  608. target = f"to {name} "
  609. msg = ""
  610. code = codes.MISC
  611. notes: list[str] = []
  612. if callee_name == "<list>":
  613. name = callee_name[1:-1]
  614. n -= 1
  615. actual_type_str, expected_type_str = format_type_distinctly(
  616. arg_type, callee.arg_types[0], options=self.options
  617. )
  618. msg = "{} item {} has incompatible type {}; expected {}".format(
  619. name.title(), n, actual_type_str, expected_type_str
  620. )
  621. code = codes.LIST_ITEM
  622. elif callee_name == "<dict>" and isinstance(
  623. get_proper_type(callee.arg_types[n - 1]), TupleType
  624. ):
  625. name = callee_name[1:-1]
  626. n -= 1
  627. key_type, value_type = cast(TupleType, arg_type).items
  628. expected_key_type, expected_value_type = cast(TupleType, callee.arg_types[n]).items
  629. # don't increase verbosity unless there is need to do so
  630. if is_subtype(key_type, expected_key_type):
  631. key_type_str = format_type(key_type, self.options)
  632. expected_key_type_str = format_type(expected_key_type, self.options)
  633. else:
  634. key_type_str, expected_key_type_str = format_type_distinctly(
  635. key_type, expected_key_type, options=self.options
  636. )
  637. if is_subtype(value_type, expected_value_type):
  638. value_type_str = format_type(value_type, self.options)
  639. expected_value_type_str = format_type(expected_value_type, self.options)
  640. else:
  641. value_type_str, expected_value_type_str = format_type_distinctly(
  642. value_type, expected_value_type, options=self.options
  643. )
  644. msg = "{} entry {} has incompatible type {}: {}; expected {}: {}".format(
  645. name.title(),
  646. n,
  647. key_type_str,
  648. value_type_str,
  649. expected_key_type_str,
  650. expected_value_type_str,
  651. )
  652. code = codes.DICT_ITEM
  653. elif callee_name == "<dict>":
  654. value_type_str, expected_value_type_str = format_type_distinctly(
  655. arg_type, callee.arg_types[n - 1], options=self.options
  656. )
  657. msg = "Unpacked dict entry {} has incompatible type {}; expected {}".format(
  658. n - 1, value_type_str, expected_value_type_str
  659. )
  660. code = codes.DICT_ITEM
  661. elif callee_name == "<list-comprehension>":
  662. actual_type_str, expected_type_str = map(
  663. strip_quotes,
  664. format_type_distinctly(arg_type, callee.arg_types[0], options=self.options),
  665. )
  666. msg = "List comprehension has incompatible type List[{}]; expected List[{}]".format(
  667. actual_type_str, expected_type_str
  668. )
  669. elif callee_name == "<set-comprehension>":
  670. actual_type_str, expected_type_str = map(
  671. strip_quotes,
  672. format_type_distinctly(arg_type, callee.arg_types[0], options=self.options),
  673. )
  674. msg = "Set comprehension has incompatible type Set[{}]; expected Set[{}]".format(
  675. actual_type_str, expected_type_str
  676. )
  677. elif callee_name == "<dictionary-comprehension>":
  678. actual_type_str, expected_type_str = format_type_distinctly(
  679. arg_type, callee.arg_types[n - 1], options=self.options
  680. )
  681. msg = (
  682. "{} expression in dictionary comprehension has incompatible type {}; "
  683. "expected type {}"
  684. ).format("Key" if n == 1 else "Value", actual_type_str, expected_type_str)
  685. elif callee_name == "<generator>":
  686. actual_type_str, expected_type_str = format_type_distinctly(
  687. arg_type, callee.arg_types[0], options=self.options
  688. )
  689. msg = "Generator has incompatible item type {}; expected {}".format(
  690. actual_type_str, expected_type_str
  691. )
  692. else:
  693. if self.prefer_simple_messages():
  694. msg = "Argument has incompatible type"
  695. else:
  696. try:
  697. expected_type = callee.arg_types[m - 1]
  698. except IndexError: # Varargs callees
  699. expected_type = callee.arg_types[-1]
  700. arg_type_str, expected_type_str = format_type_distinctly(
  701. arg_type, expected_type, bare=True, options=self.options
  702. )
  703. if arg_kind == ARG_STAR:
  704. arg_type_str = "*" + arg_type_str
  705. elif arg_kind == ARG_STAR2:
  706. arg_type_str = "**" + arg_type_str
  707. # For function calls with keyword arguments, display the argument name rather
  708. # than the number.
  709. arg_label = str(n)
  710. if isinstance(outer_context, CallExpr) and len(outer_context.arg_names) >= n:
  711. arg_name = outer_context.arg_names[n - 1]
  712. if arg_name is not None:
  713. arg_label = f'"{arg_name}"'
  714. if (
  715. arg_kind == ARG_STAR2
  716. and isinstance(arg_type, TypedDictType)
  717. and m <= len(callee.arg_names)
  718. and callee.arg_names[m - 1] is not None
  719. and callee.arg_kinds[m - 1] != ARG_STAR2
  720. ):
  721. arg_name = callee.arg_names[m - 1]
  722. assert arg_name is not None
  723. arg_type_str, expected_type_str = format_type_distinctly(
  724. arg_type.items[arg_name], expected_type, bare=True, options=self.options
  725. )
  726. arg_label = f'"{arg_name}"'
  727. if isinstance(outer_context, IndexExpr) and isinstance(
  728. outer_context.index, StrExpr
  729. ):
  730. msg = 'Value of "{}" has incompatible type {}; expected {}'.format(
  731. outer_context.index.value,
  732. quote_type_string(arg_type_str),
  733. quote_type_string(expected_type_str),
  734. )
  735. else:
  736. msg = "Argument {} {}has incompatible type {}; expected {}".format(
  737. arg_label,
  738. target,
  739. quote_type_string(arg_type_str),
  740. quote_type_string(expected_type_str),
  741. )
  742. expected_type = get_proper_type(expected_type)
  743. if isinstance(expected_type, UnionType):
  744. expected_types = list(expected_type.items)
  745. else:
  746. expected_types = [expected_type]
  747. for type in get_proper_types(expected_types):
  748. if isinstance(arg_type, Instance) and isinstance(type, Instance):
  749. notes = append_invariance_notes(notes, arg_type, type)
  750. notes = append_numbers_notes(notes, arg_type, type)
  751. object_type = get_proper_type(object_type)
  752. if isinstance(object_type, TypedDictType):
  753. code = codes.TYPEDDICT_ITEM
  754. else:
  755. code = codes.ARG_TYPE
  756. self.fail(msg, context, code=code)
  757. if notes:
  758. for note_msg in notes:
  759. self.note(note_msg, context, code=code)
  760. return code
  761. def incompatible_argument_note(
  762. self,
  763. original_caller_type: ProperType,
  764. callee_type: ProperType,
  765. context: Context,
  766. code: ErrorCode | None,
  767. ) -> None:
  768. if self.prefer_simple_messages():
  769. return
  770. if isinstance(
  771. original_caller_type, (Instance, TupleType, TypedDictType, TypeType, CallableType)
  772. ):
  773. if isinstance(callee_type, Instance) and callee_type.type.is_protocol:
  774. self.report_protocol_problems(
  775. original_caller_type, callee_type, context, code=code
  776. )
  777. if isinstance(callee_type, UnionType):
  778. for item in callee_type.items:
  779. item = get_proper_type(item)
  780. if isinstance(item, Instance) and item.type.is_protocol:
  781. self.report_protocol_problems(
  782. original_caller_type, item, context, code=code
  783. )
  784. if isinstance(callee_type, CallableType) and isinstance(original_caller_type, Instance):
  785. call = find_member(
  786. "__call__", original_caller_type, original_caller_type, is_operator=True
  787. )
  788. if call:
  789. self.note_call(original_caller_type, call, context, code=code)
  790. self.maybe_note_concatenate_pos_args(original_caller_type, callee_type, context, code)
  791. def maybe_note_concatenate_pos_args(
  792. self,
  793. original_caller_type: ProperType,
  794. callee_type: ProperType,
  795. context: Context,
  796. code: ErrorCode | None = None,
  797. ) -> None:
  798. # pos-only vs positional can be confusing, with Concatenate
  799. if (
  800. isinstance(callee_type, CallableType)
  801. and isinstance(original_caller_type, CallableType)
  802. and (original_caller_type.from_concatenate or callee_type.from_concatenate)
  803. ):
  804. names: list[str] = []
  805. for c, o in zip(
  806. callee_type.formal_arguments(), original_caller_type.formal_arguments()
  807. ):
  808. if None in (c.pos, o.pos):
  809. # non-positional
  810. continue
  811. if c.name != o.name and c.name is None and o.name is not None:
  812. names.append(o.name)
  813. if names:
  814. missing_arguments = '"' + '", "'.join(names) + '"'
  815. self.note(
  816. f'This is likely because "{original_caller_type.name}" has named arguments: '
  817. f"{missing_arguments}. Consider marking them positional-only",
  818. context,
  819. code=code,
  820. )
  821. def invalid_index_type(
  822. self,
  823. index_type: Type,
  824. expected_type: Type,
  825. base_str: str,
  826. context: Context,
  827. *,
  828. code: ErrorCode,
  829. ) -> None:
  830. index_str, expected_str = format_type_distinctly(
  831. index_type, expected_type, options=self.options
  832. )
  833. self.fail(
  834. "Invalid index type {} for {}; expected type {}".format(
  835. index_str, base_str, expected_str
  836. ),
  837. context,
  838. code=code,
  839. )
  840. def too_few_arguments(
  841. self, callee: CallableType, context: Context, argument_names: Sequence[str | None] | None
  842. ) -> None:
  843. if self.prefer_simple_messages():
  844. msg = "Too few arguments"
  845. elif argument_names is not None:
  846. num_positional_args = sum(k is None for k in argument_names)
  847. arguments_left = callee.arg_names[num_positional_args : callee.min_args]
  848. diff = [k for k in arguments_left if k not in argument_names]
  849. if len(diff) == 1:
  850. msg = "Missing positional argument"
  851. else:
  852. msg = "Missing positional arguments"
  853. callee_name = callable_name(callee)
  854. if callee_name is not None and diff and all(d is not None for d in diff):
  855. args = '", "'.join(cast(List[str], diff))
  856. msg += f' "{args}" in call to {callee_name}'
  857. else:
  858. msg = "Too few arguments" + for_function(callee)
  859. else:
  860. msg = "Too few arguments" + for_function(callee)
  861. self.fail(msg, context, code=codes.CALL_ARG)
  862. def missing_named_argument(self, callee: CallableType, context: Context, name: str) -> None:
  863. msg = f'Missing named argument "{name}"' + for_function(callee)
  864. self.fail(msg, context, code=codes.CALL_ARG)
  865. def too_many_arguments(self, callee: CallableType, context: Context) -> None:
  866. if self.prefer_simple_messages():
  867. msg = "Too many arguments"
  868. else:
  869. msg = "Too many arguments" + for_function(callee)
  870. self.fail(msg, context, code=codes.CALL_ARG)
  871. self.maybe_note_about_special_args(callee, context)
  872. def too_many_arguments_from_typed_dict(
  873. self, callee: CallableType, arg_type: TypedDictType, context: Context
  874. ) -> None:
  875. # Try to determine the name of the extra argument.
  876. for key in arg_type.items:
  877. if key not in callee.arg_names:
  878. msg = f'Extra argument "{key}" from **args' + for_function(callee)
  879. break
  880. else:
  881. self.too_many_arguments(callee, context)
  882. return
  883. self.fail(msg, context)
  884. def too_many_positional_arguments(self, callee: CallableType, context: Context) -> None:
  885. if self.prefer_simple_messages():
  886. msg = "Too many positional arguments"
  887. else:
  888. msg = "Too many positional arguments" + for_function(callee)
  889. self.fail(msg, context)
  890. self.maybe_note_about_special_args(callee, context)
  891. def maybe_note_about_special_args(self, callee: CallableType, context: Context) -> None:
  892. if self.prefer_simple_messages():
  893. return
  894. # https://github.com/python/mypy/issues/11309
  895. first_arg = callee.def_extras.get("first_arg")
  896. if first_arg and first_arg not in {"self", "cls", "mcs"}:
  897. self.note(
  898. "Looks like the first special argument in a method "
  899. 'is not named "self", "cls", or "mcs", '
  900. "maybe it is missing?",
  901. context,
  902. )
  903. def unexpected_keyword_argument(
  904. self, callee: CallableType, name: str, arg_type: Type, context: Context
  905. ) -> None:
  906. msg = f'Unexpected keyword argument "{name}"' + for_function(callee)
  907. # Suggest intended keyword, look for type match else fallback on any match.
  908. matching_type_args = []
  909. not_matching_type_args = []
  910. for i, kwarg_type in enumerate(callee.arg_types):
  911. callee_arg_name = callee.arg_names[i]
  912. if callee_arg_name is not None and callee.arg_kinds[i] != ARG_STAR:
  913. if is_subtype(arg_type, kwarg_type):
  914. matching_type_args.append(callee_arg_name)
  915. else:
  916. not_matching_type_args.append(callee_arg_name)
  917. matches = best_matches(name, matching_type_args, n=3)
  918. if not matches:
  919. matches = best_matches(name, not_matching_type_args, n=3)
  920. if matches:
  921. msg += f"; did you mean {pretty_seq(matches, 'or')}?"
  922. self.fail(msg, context, code=codes.CALL_ARG)
  923. module = find_defining_module(self.modules, callee)
  924. if module:
  925. assert callee.definition is not None
  926. fname = callable_name(callee)
  927. if not fname: # an alias to function with a different name
  928. fname = "Called function"
  929. self.note(
  930. f"{fname} defined here",
  931. callee.definition,
  932. file=module.path,
  933. origin=context,
  934. code=codes.CALL_ARG,
  935. )
  936. def duplicate_argument_value(self, callee: CallableType, index: int, context: Context) -> None:
  937. self.fail(
  938. '{} gets multiple values for keyword argument "{}"'.format(
  939. callable_name(callee) or "Function", callee.arg_names[index]
  940. ),
  941. context,
  942. )
  943. def does_not_return_value(self, callee_type: Type | None, context: Context) -> None:
  944. """Report an error about use of an unusable type."""
  945. name: str | None = None
  946. callee_type = get_proper_type(callee_type)
  947. if isinstance(callee_type, FunctionLike):
  948. name = callable_name(callee_type)
  949. if name is not None:
  950. self.fail(
  951. f"{capitalize(name)} does not return a value",
  952. context,
  953. code=codes.FUNC_RETURNS_VALUE,
  954. )
  955. else:
  956. self.fail("Function does not return a value", context, code=codes.FUNC_RETURNS_VALUE)
  957. def deleted_as_rvalue(self, typ: DeletedType, context: Context) -> None:
  958. """Report an error about using an deleted type as an rvalue."""
  959. if typ.source is None:
  960. s = ""
  961. else:
  962. s = f' "{typ.source}"'
  963. self.fail(f"Trying to read deleted variable{s}", context)
  964. def deleted_as_lvalue(self, typ: DeletedType, context: Context) -> None:
  965. """Report an error about using an deleted type as an lvalue.
  966. Currently, this only occurs when trying to assign to an
  967. exception variable outside the local except: blocks.
  968. """
  969. if typ.source is None:
  970. s = ""
  971. else:
  972. s = f' "{typ.source}"'
  973. self.fail(f"Assignment to variable{s} outside except: block", context)
  974. def no_variant_matches_arguments(
  975. self,
  976. overload: Overloaded,
  977. arg_types: list[Type],
  978. context: Context,
  979. *,
  980. code: ErrorCode | None = None,
  981. ) -> None:
  982. code = code or codes.CALL_OVERLOAD
  983. name = callable_name(overload)
  984. if name:
  985. name_str = f" of {name}"
  986. else:
  987. name_str = ""
  988. arg_types_str = ", ".join(format_type(arg, self.options) for arg in arg_types)
  989. num_args = len(arg_types)
  990. if num_args == 0:
  991. self.fail(
  992. f"All overload variants{name_str} require at least one argument",
  993. context,
  994. code=code,
  995. )
  996. elif num_args == 1:
  997. self.fail(
  998. f"No overload variant{name_str} matches argument type {arg_types_str}",
  999. context,
  1000. code=code,
  1001. )
  1002. else:
  1003. self.fail(
  1004. f"No overload variant{name_str} matches argument types {arg_types_str}",
  1005. context,
  1006. code=code,
  1007. )
  1008. self.note(f"Possible overload variant{plural_s(len(overload.items))}:", context, code=code)
  1009. for item in overload.items:
  1010. self.note(pretty_callable(item, self.options), context, offset=4, code=code)
  1011. def wrong_number_values_to_unpack(
  1012. self, provided: int, expected: int, context: Context
  1013. ) -> None:
  1014. if provided < expected:
  1015. if provided == 1:
  1016. self.fail(f"Need more than 1 value to unpack ({expected} expected)", context)
  1017. else:
  1018. self.fail(
  1019. f"Need more than {provided} values to unpack ({expected} expected)", context
  1020. )
  1021. elif provided > expected:
  1022. self.fail(
  1023. f"Too many values to unpack ({expected} expected, {provided} provided)", context
  1024. )
  1025. def unpacking_strings_disallowed(self, context: Context) -> None:
  1026. self.fail("Unpacking a string is disallowed", context)
  1027. def type_not_iterable(self, type: Type, context: Context) -> None:
  1028. self.fail(f"{format_type(type, self.options)} object is not iterable", context)
  1029. def possible_missing_await(self, context: Context) -> None:
  1030. self.note('Maybe you forgot to use "await"?', context)
  1031. def incompatible_operator_assignment(self, op: str, context: Context) -> None:
  1032. self.fail(f"Result type of {op} incompatible in assignment", context)
  1033. def overload_signature_incompatible_with_supertype(
  1034. self, name: str, name_in_super: str, supertype: str, context: Context
  1035. ) -> None:
  1036. target = self.override_target(name, name_in_super, supertype)
  1037. self.fail(
  1038. f'Signature of "{name}" incompatible with {target}', context, code=codes.OVERRIDE
  1039. )
  1040. note_template = 'Overload variants must be defined in the same order as they are in "{}"'
  1041. self.note(note_template.format(supertype), context, code=codes.OVERRIDE)
  1042. def signature_incompatible_with_supertype(
  1043. self,
  1044. name: str,
  1045. name_in_super: str,
  1046. supertype: str,
  1047. context: Context,
  1048. *,
  1049. original: ProperType,
  1050. override: ProperType,
  1051. ) -> None:
  1052. code = codes.OVERRIDE
  1053. target = self.override_target(name, name_in_super, supertype)
  1054. self.fail(f'Signature of "{name}" incompatible with {target}', context, code=code)
  1055. original_str, override_str = format_type_distinctly(
  1056. original, override, options=self.options, bare=True
  1057. )
  1058. INCLUDE_DECORATOR = True # Include @classmethod and @staticmethod decorators, if any
  1059. ALLOW_DUPS = True # Allow duplicate notes, needed when signatures are duplicates
  1060. ALIGN_OFFSET = 1 # One space, to account for the difference between error and note
  1061. OFFSET = 4 # Four spaces, so that notes will look like this:
  1062. # error: Signature of "f" incompatible with supertype "A"
  1063. # note: Superclass:
  1064. # note: def f(self) -> str
  1065. # note: Subclass:
  1066. # note: def f(self, x: str) -> None
  1067. self.note(
  1068. "Superclass:", context, offset=ALIGN_OFFSET + OFFSET, allow_dups=ALLOW_DUPS, code=code
  1069. )
  1070. if isinstance(original, (CallableType, Overloaded)):
  1071. self.pretty_callable_or_overload(
  1072. original,
  1073. context,
  1074. offset=ALIGN_OFFSET + 2 * OFFSET,
  1075. add_class_or_static_decorator=INCLUDE_DECORATOR,
  1076. allow_dups=ALLOW_DUPS,
  1077. code=code,
  1078. )
  1079. else:
  1080. self.note(
  1081. original_str,
  1082. context,
  1083. offset=ALIGN_OFFSET + 2 * OFFSET,
  1084. allow_dups=ALLOW_DUPS,
  1085. code=code,
  1086. )
  1087. self.note(
  1088. "Subclass:", context, offset=ALIGN_OFFSET + OFFSET, allow_dups=ALLOW_DUPS, code=code
  1089. )
  1090. if isinstance(override, (CallableType, Overloaded)):
  1091. self.pretty_callable_or_overload(
  1092. override,
  1093. context,
  1094. offset=ALIGN_OFFSET + 2 * OFFSET,
  1095. add_class_or_static_decorator=INCLUDE_DECORATOR,
  1096. allow_dups=ALLOW_DUPS,
  1097. code=code,
  1098. )
  1099. else:
  1100. self.note(
  1101. override_str,
  1102. context,
  1103. offset=ALIGN_OFFSET + 2 * OFFSET,
  1104. allow_dups=ALLOW_DUPS,
  1105. code=code,
  1106. )
  1107. def pretty_callable_or_overload(
  1108. self,
  1109. tp: CallableType | Overloaded,
  1110. context: Context,
  1111. *,
  1112. offset: int = 0,
  1113. add_class_or_static_decorator: bool = False,
  1114. allow_dups: bool = False,
  1115. code: ErrorCode | None = None,
  1116. ) -> None:
  1117. if isinstance(tp, CallableType):
  1118. if add_class_or_static_decorator:
  1119. decorator = pretty_class_or_static_decorator(tp)
  1120. if decorator is not None:
  1121. self.note(decorator, context, offset=offset, allow_dups=allow_dups, code=code)
  1122. self.note(
  1123. pretty_callable(tp, self.options),
  1124. context,
  1125. offset=offset,
  1126. allow_dups=allow_dups,
  1127. code=code,
  1128. )
  1129. elif isinstance(tp, Overloaded):
  1130. self.pretty_overload(
  1131. tp,
  1132. context,
  1133. offset,
  1134. add_class_or_static_decorator=add_class_or_static_decorator,
  1135. allow_dups=allow_dups,
  1136. code=code,
  1137. )
  1138. def argument_incompatible_with_supertype(
  1139. self,
  1140. arg_num: int,
  1141. name: str,
  1142. type_name: str | None,
  1143. name_in_supertype: str,
  1144. arg_type_in_supertype: Type,
  1145. supertype: str,
  1146. context: Context,
  1147. secondary_context: Context,
  1148. ) -> None:
  1149. target = self.override_target(name, name_in_supertype, supertype)
  1150. arg_type_in_supertype_f = format_type_bare(arg_type_in_supertype, self.options)
  1151. self.fail(
  1152. 'Argument {} of "{}" is incompatible with {}; '
  1153. 'supertype defines the argument type as "{}"'.format(
  1154. arg_num, name, target, arg_type_in_supertype_f
  1155. ),
  1156. context,
  1157. code=codes.OVERRIDE,
  1158. secondary_context=secondary_context,
  1159. )
  1160. if name != "__post_init__":
  1161. # `__post_init__` is special, it can be incompatible by design.
  1162. # So, this note is misleading.
  1163. self.note(
  1164. "This violates the Liskov substitution principle",
  1165. context,
  1166. code=codes.OVERRIDE,
  1167. secondary_context=secondary_context,
  1168. )
  1169. self.note(
  1170. "See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides",
  1171. context,
  1172. code=codes.OVERRIDE,
  1173. secondary_context=secondary_context,
  1174. )
  1175. if name == "__eq__" and type_name:
  1176. multiline_msg = self.comparison_method_example_msg(class_name=type_name)
  1177. self.note_multiline(
  1178. multiline_msg, context, code=codes.OVERRIDE, secondary_context=secondary_context
  1179. )
  1180. def comparison_method_example_msg(self, class_name: str) -> str:
  1181. return dedent(
  1182. """\
  1183. It is recommended for "__eq__" to work with arbitrary objects, for example:
  1184. def __eq__(self, other: object) -> bool:
  1185. if not isinstance(other, {class_name}):
  1186. return NotImplemented
  1187. return <logic to compare two {class_name} instances>
  1188. """.format(
  1189. class_name=class_name
  1190. )
  1191. )
  1192. def return_type_incompatible_with_supertype(
  1193. self,
  1194. name: str,
  1195. name_in_supertype: str,
  1196. supertype: str,
  1197. original: Type,
  1198. override: Type,
  1199. context: Context,
  1200. ) -> None:
  1201. target = self.override_target(name, name_in_supertype, supertype)
  1202. override_str, original_str = format_type_distinctly(
  1203. override, original, options=self.options
  1204. )
  1205. self.fail(
  1206. 'Return type {} of "{}" incompatible with return type {} in {}'.format(
  1207. override_str, name, original_str, target
  1208. ),
  1209. context,
  1210. code=codes.OVERRIDE,
  1211. )
  1212. def override_target(self, name: str, name_in_super: str, supertype: str) -> str:
  1213. target = f'supertype "{supertype}"'
  1214. if name_in_super != name:
  1215. target = f'"{name_in_super}" of {target}'
  1216. return target
  1217. def incompatible_type_application(
  1218. self, expected_arg_count: int, actual_arg_count: int, context: Context
  1219. ) -> None:
  1220. if expected_arg_count == 0:
  1221. self.fail("Type application targets a non-generic function or class", context)
  1222. elif actual_arg_count > expected_arg_count:
  1223. self.fail(
  1224. f"Type application has too many types ({expected_arg_count} expected)", context
  1225. )
  1226. else:
  1227. self.fail(
  1228. f"Type application has too few types ({expected_arg_count} expected)", context
  1229. )
  1230. def could_not_infer_type_arguments(
  1231. self, callee_type: CallableType, n: int, context: Context
  1232. ) -> None:
  1233. callee_name = callable_name(callee_type)
  1234. if callee_name is not None and n > 0:
  1235. self.fail(f"Cannot infer type argument {n} of {callee_name}", context)
  1236. if callee_name == "<dict>":
  1237. # Invariance in key type causes more of these errors than we would want.
  1238. self.note(
  1239. "Try assigning the literal to a variable annotated as dict[<key>, <val>]",
  1240. context,
  1241. )
  1242. else:
  1243. self.fail("Cannot infer function type argument", context)
  1244. def invalid_var_arg(self, typ: Type, context: Context) -> None:
  1245. self.fail("List or tuple expected as variadic arguments", context)
  1246. def invalid_keyword_var_arg(self, typ: Type, is_mapping: bool, context: Context) -> None:
  1247. typ = get_proper_type(typ)
  1248. if isinstance(typ, Instance) and is_mapping:
  1249. self.fail("Keywords must be strings", context)
  1250. else:
  1251. self.fail(
  1252. f"Argument after ** must be a mapping, not {format_type(typ, self.options)}",
  1253. context,
  1254. code=codes.ARG_TYPE,
  1255. )
  1256. def undefined_in_superclass(self, member: str, context: Context) -> None:
  1257. self.fail(f'"{member}" undefined in superclass', context)
  1258. def variable_may_be_undefined(self, name: str, context: Context) -> None:
  1259. self.fail(f'Name "{name}" may be undefined', context, code=codes.POSSIBLY_UNDEFINED)
  1260. def var_used_before_def(self, name: str, context: Context) -> None:
  1261. self.fail(f'Name "{name}" is used before definition', context, code=codes.USED_BEFORE_DEF)
  1262. def first_argument_for_super_must_be_type(self, actual: Type, context: Context) -> None:
  1263. actual = get_proper_type(actual)
  1264. if isinstance(actual, Instance):
  1265. # Don't include type of instance, because it can look confusingly like a type
  1266. # object.
  1267. type_str = "a non-type instance"
  1268. else:
  1269. type_str = format_type(actual, self.options)
  1270. self.fail(
  1271. f'Argument 1 for "super" must be a type object; got {type_str}',
  1272. context,
  1273. code=codes.ARG_TYPE,
  1274. )
  1275. def unsafe_super(self, method: str, cls: str, ctx: Context) -> None:
  1276. self.fail(
  1277. 'Call to abstract method "{}" of "{}" with trivial body'
  1278. " via super() is unsafe".format(method, cls),
  1279. ctx,
  1280. code=codes.SAFE_SUPER,
  1281. )
  1282. def too_few_string_formatting_arguments(self, context: Context) -> None:
  1283. self.fail("Not enough arguments for format string", context, code=codes.STRING_FORMATTING)
  1284. def too_many_string_formatting_arguments(self, context: Context) -> None:
  1285. self.fail(
  1286. "Not all arguments converted during string formatting",
  1287. context,
  1288. code=codes.STRING_FORMATTING,
  1289. )
  1290. def unsupported_placeholder(self, placeholder: str, context: Context) -> None:
  1291. self.fail(
  1292. f'Unsupported format character "{placeholder}"', context, code=codes.STRING_FORMATTING
  1293. )
  1294. def string_interpolation_with_star_and_key(self, context: Context) -> None:
  1295. self.fail(
  1296. "String interpolation contains both stars and mapping keys",
  1297. context,
  1298. code=codes.STRING_FORMATTING,
  1299. )
  1300. def requires_int_or_single_byte(self, context: Context, format_call: bool = False) -> None:
  1301. self.fail(
  1302. '"{}c" requires an integer in range(256) or a single byte'.format(
  1303. ":" if format_call else "%"
  1304. ),
  1305. context,
  1306. code=codes.STRING_FORMATTING,
  1307. )
  1308. def requires_int_or_char(self, context: Context, format_call: bool = False) -> None:
  1309. self.fail(
  1310. '"{}c" requires int or char'.format(":" if format_call else "%"),
  1311. context,
  1312. code=codes.STRING_FORMATTING,
  1313. )
  1314. def key_not_in_mapping(self, key: str, context: Context) -> None:
  1315. self.fail(f'Key "{key}" not found in mapping', context, code=codes.STRING_FORMATTING)
  1316. def string_interpolation_mixing_key_and_non_keys(self, context: Context) -> None:
  1317. self.fail(
  1318. "String interpolation mixes specifier with and without mapping keys",
  1319. context,
  1320. code=codes.STRING_FORMATTING,
  1321. )
  1322. def cannot_determine_type(self, name: str, context: Context) -> None:
  1323. self.fail(f'Cannot determine type of "{name}"', context, code=codes.HAS_TYPE)
  1324. def cannot_determine_type_in_base(self, name: str, base: str, context: Context) -> None:
  1325. self.fail(f'Cannot determine type of "{name}" in base class "{base}"', context)
  1326. def no_formal_self(self, name: str, item: CallableType, context: Context) -> None:
  1327. self.fail(
  1328. 'Attribute function "%s" with type %s does not accept self argument'
  1329. % (name, format_type(item, self.options)),
  1330. context,
  1331. )
  1332. def incompatible_self_argument(
  1333. self, name: str, arg: Type, sig: CallableType, is_classmethod: bool, context: Context
  1334. ) -> None:
  1335. kind = "class attribute function" if is_classmethod else "attribute function"
  1336. self.fail(
  1337. 'Invalid self argument %s to %s "%s" with type %s'
  1338. % (format_type(arg, self.options), kind, name, format_type(sig, self.options)),
  1339. context,
  1340. )
  1341. def incompatible_conditional_function_def(
  1342. self, defn: FuncDef, old_type: FunctionLike, new_type: FunctionLike
  1343. ) -> None:
  1344. self.fail("All conditional function variants must have identical signatures", defn)
  1345. if isinstance(old_type, (CallableType, Overloaded)) and isinstance(
  1346. new_type, (CallableType, Overloaded)
  1347. ):
  1348. self.note("Original:", defn)
  1349. self.pretty_callable_or_overload(old_type, defn, offset=4)
  1350. self.note("Redefinition:", defn)
  1351. self.pretty_callable_or_overload(new_type, defn, offset=4)
  1352. def cannot_instantiate_abstract_class(
  1353. self, class_name: str, abstract_attributes: dict[str, bool], context: Context
  1354. ) -> None:
  1355. attrs = format_string_list([f'"{a}"' for a in abstract_attributes])
  1356. self.fail(
  1357. 'Cannot instantiate abstract class "%s" with abstract '
  1358. "attribute%s %s" % (class_name, plural_s(abstract_attributes), attrs),
  1359. context,
  1360. code=codes.ABSTRACT,
  1361. )
  1362. attrs_with_none = [
  1363. f'"{a}"'
  1364. for a, implicit_and_can_return_none in abstract_attributes.items()
  1365. if implicit_and_can_return_none
  1366. ]
  1367. if not attrs_with_none:
  1368. return
  1369. if len(attrs_with_none) == 1:
  1370. note = (
  1371. f"{attrs_with_none[0]} is implicitly abstract because it has an empty function "
  1372. "body. If it is not meant to be abstract, explicitly `return` or `return None`."
  1373. )
  1374. else:
  1375. note = (
  1376. "The following methods were marked implicitly abstract because they have empty "
  1377. f"function bodies: {format_string_list(attrs_with_none)}. "
  1378. "If they are not meant to be abstract, explicitly `return` or `return None`."
  1379. )
  1380. self.note(note, context, code=codes.ABSTRACT)
  1381. def base_class_definitions_incompatible(
  1382. self, name: str, base1: TypeInfo, base2: TypeInfo, context: Context
  1383. ) -> None:
  1384. self.fail(
  1385. 'Definition of "{}" in base class "{}" is incompatible '
  1386. 'with definition in base class "{}"'.format(name, base1.name, base2.name),
  1387. context,
  1388. )
  1389. def cant_assign_to_method(self, context: Context) -> None:
  1390. self.fail(message_registry.CANNOT_ASSIGN_TO_METHOD, context, code=codes.METHOD_ASSIGN)
  1391. def cant_assign_to_classvar(self, name: str, context: Context) -> None:
  1392. self.fail(f'Cannot assign to class variable "{name}" via instance', context)
  1393. def no_overridable_method(self, name: str, context: Context) -> None:
  1394. self.fail(
  1395. f'Method "{name}" is marked as an override, '
  1396. "but no base method was found with this name",
  1397. context,
  1398. )
  1399. def explicit_override_decorator_missing(
  1400. self, name: str, base_name: str, context: Context
  1401. ) -> None:
  1402. self.fail(
  1403. f'Method "{name}" is not using @override '
  1404. f'but is overriding a method in class "{base_name}"',
  1405. context,
  1406. code=codes.EXPLICIT_OVERRIDE_REQUIRED,
  1407. )
  1408. def final_cant_override_writable(self, name: str, ctx: Context) -> None:
  1409. self.fail(f'Cannot override writable attribute "{name}" with a final one', ctx)
  1410. def cant_override_final(self, name: str, base_name: str, ctx: Context) -> None:
  1411. self.fail(
  1412. 'Cannot override final attribute "{}"'
  1413. ' (previously declared in base class "{}")'.format(name, base_name),
  1414. ctx,
  1415. )
  1416. def cant_assign_to_final(self, name: str, attr_assign: bool, ctx: Context) -> None:
  1417. """Warn about a prohibited assignment to a final attribute.
  1418. Pass `attr_assign=True` if the assignment assigns to an attribute.
  1419. """
  1420. kind = "attribute" if attr_assign else "name"
  1421. self.fail(f'Cannot assign to final {kind} "{unmangle(name)}"', ctx)
  1422. def protocol_members_cant_be_final(self, ctx: Context) -> None:
  1423. self.fail("Protocol member cannot be final", ctx)
  1424. def final_without_value(self, ctx: Context) -> None:
  1425. self.fail("Final name must be initialized with a value", ctx)
  1426. def read_only_property(self, name: str, type: TypeInfo, context: Context) -> None:
  1427. self.fail(f'Property "{name}" defined in "{type.name}" is read-only', context)
  1428. def incompatible_typevar_value(
  1429. self, callee: CallableType, typ: Type, typevar_name: str, context: Context
  1430. ) -> None:
  1431. self.fail(
  1432. message_registry.INCOMPATIBLE_TYPEVAR_VALUE.format(
  1433. typevar_name, callable_name(callee) or "function", format_type(typ, self.options)
  1434. ),
  1435. context,
  1436. code=codes.TYPE_VAR,
  1437. )
  1438. def dangerous_comparison(self, left: Type, right: Type, kind: str, ctx: Context) -> None:
  1439. left_str = "element" if kind == "container" else "left operand"
  1440. right_str = "container item" if kind == "container" else "right operand"
  1441. message = "Non-overlapping {} check ({} type: {}, {} type: {})"
  1442. left_typ, right_typ = format_type_distinctly(left, right, options=self.options)
  1443. self.fail(
  1444. message.format(kind, left_str, left_typ, right_str, right_typ),
  1445. ctx,
  1446. code=codes.COMPARISON_OVERLAP,
  1447. )
  1448. def overload_inconsistently_applies_decorator(self, decorator: str, context: Context) -> None:
  1449. self.fail(
  1450. f'Overload does not consistently use the "@{decorator}" '
  1451. + "decorator on all function signatures.",
  1452. context,
  1453. )
  1454. def overloaded_signatures_overlap(self, index1: int, index2: int, context: Context) -> None:
  1455. self.fail(
  1456. "Overloaded function signatures {} and {} overlap with "
  1457. "incompatible return types".format(index1, index2),
  1458. context,
  1459. )
  1460. def overloaded_signature_will_never_match(
  1461. self, index1: int, index2: int, context: Context
  1462. ) -> None:
  1463. self.fail(
  1464. "Overloaded function signature {index2} will never be matched: "
  1465. "signature {index1}'s parameter type(s) are the same or broader".format(
  1466. index1=index1, index2=index2
  1467. ),
  1468. context,
  1469. )
  1470. def overloaded_signatures_typevar_specific(self, index: int, context: Context) -> None:
  1471. self.fail(
  1472. f"Overloaded function implementation cannot satisfy signature {index} "
  1473. + "due to inconsistencies in how they use type variables",
  1474. context,
  1475. )
  1476. def overloaded_signatures_arg_specific(self, index: int, context: Context) -> None:
  1477. self.fail(
  1478. "Overloaded function implementation does not accept all possible arguments "
  1479. "of signature {}".format(index),
  1480. context,
  1481. )
  1482. def overloaded_signatures_ret_specific(self, index: int, context: Context) -> None:
  1483. self.fail(
  1484. "Overloaded function implementation cannot produce return type "
  1485. "of signature {}".format(index),
  1486. context,
  1487. )
  1488. def warn_both_operands_are_from_unions(self, context: Context) -> None:
  1489. self.note("Both left and right operands are unions", context, code=codes.OPERATOR)
  1490. def warn_operand_was_from_union(self, side: str, original: Type, context: Context) -> None:
  1491. self.note(
  1492. f"{side} operand is of type {format_type(original, self.options)}",
  1493. context,
  1494. code=codes.OPERATOR,
  1495. )
  1496. def operator_method_signatures_overlap(
  1497. self,
  1498. reverse_class: TypeInfo,
  1499. reverse_method: str,
  1500. forward_class: Type,
  1501. forward_method: str,
  1502. context: Context,
  1503. ) -> None:
  1504. self.fail(
  1505. 'Signatures of "{}" of "{}" and "{}" of {} '
  1506. "are unsafely overlapping".format(
  1507. reverse_method,
  1508. reverse_class.name,
  1509. forward_method,
  1510. format_type(forward_class, self.options),
  1511. ),
  1512. context,
  1513. )
  1514. def forward_operator_not_callable(self, forward_method: str, context: Context) -> None:
  1515. self.fail(f'Forward operator "{forward_method}" is not callable', context)
  1516. def signatures_incompatible(self, method: str, other_method: str, context: Context) -> None:
  1517. self.fail(f'Signatures of "{method}" and "{other_method}" are incompatible', context)
  1518. def yield_from_invalid_operand_type(self, expr: Type, context: Context) -> Type:
  1519. text = (
  1520. format_type(expr, self.options)
  1521. if format_type(expr, self.options) != "object"
  1522. else expr
  1523. )
  1524. self.fail(f'"yield from" can\'t be applied to {text}', context)
  1525. return AnyType(TypeOfAny.from_error)
  1526. def invalid_signature(self, func_type: Type, context: Context) -> None:
  1527. self.fail(f"Invalid signature {format_type(func_type, self.options)}", context)
  1528. def invalid_signature_for_special_method(
  1529. self, func_type: Type, context: Context, method_name: str
  1530. ) -> None:
  1531. self.fail(
  1532. f'Invalid signature {format_type(func_type, self.options)} for "{method_name}"',
  1533. context,
  1534. )
  1535. def reveal_type(self, typ: Type, context: Context) -> None:
  1536. visitor = TypeStrVisitor(options=self.options)
  1537. self.note(f'Revealed type is "{typ.accept(visitor)}"', context)
  1538. def reveal_locals(self, type_map: dict[str, Type | None], context: Context) -> None:
  1539. # To ensure that the output is predictable on Python < 3.6,
  1540. # use an ordered dictionary sorted by variable name
  1541. sorted_locals = dict(sorted(type_map.items(), key=lambda t: t[0]))
  1542. if sorted_locals:
  1543. self.note("Revealed local types are:", context)
  1544. for k, v in sorted_locals.items():
  1545. visitor = TypeStrVisitor(options=self.options)
  1546. self.note(f" {k}: {v.accept(visitor) if v is not None else None}", context)
  1547. else:
  1548. self.note("There are no locals to reveal", context)
  1549. def unsupported_type_type(self, item: Type, context: Context) -> None:
  1550. self.fail(
  1551. f'Cannot instantiate type "Type[{format_type_bare(item, self.options)}]"', context
  1552. )
  1553. def redundant_cast(self, typ: Type, context: Context) -> None:
  1554. self.fail(
  1555. f"Redundant cast to {format_type(typ, self.options)}",
  1556. context,
  1557. code=codes.REDUNDANT_CAST,
  1558. )
  1559. def assert_type_fail(self, source_type: Type, target_type: Type, context: Context) -> None:
  1560. (source, target) = format_type_distinctly(source_type, target_type, options=self.options)
  1561. self.fail(f"Expression is of type {source}, not {target}", context, code=codes.ASSERT_TYPE)
  1562. def unimported_type_becomes_any(self, prefix: str, typ: Type, ctx: Context) -> None:
  1563. self.fail(
  1564. f"{prefix} becomes {format_type(typ, self.options)} due to an unfollowed import",
  1565. ctx,
  1566. code=codes.NO_ANY_UNIMPORTED,
  1567. )
  1568. def need_annotation_for_var(
  1569. self, node: SymbolNode, context: Context, python_version: tuple[int, int] | None = None
  1570. ) -> None:
  1571. hint = ""
  1572. has_variable_annotations = not python_version or python_version >= (3, 6)
  1573. pep604_supported = not python_version or python_version >= (3, 10)
  1574. # type to recommend the user adds
  1575. recommended_type = None
  1576. # Only gives hint if it's a variable declaration and the partial type is a builtin type
  1577. if python_version and isinstance(node, Var) and isinstance(node.type, PartialType):
  1578. type_dec = "<type>"
  1579. if not node.type.type:
  1580. # partial None
  1581. if pep604_supported:
  1582. recommended_type = f"{type_dec} | None"
  1583. else:
  1584. recommended_type = f"Optional[{type_dec}]"
  1585. elif node.type.type.fullname in reverse_builtin_aliases:
  1586. # partial types other than partial None
  1587. alias = reverse_builtin_aliases[node.type.type.fullname]
  1588. alias = alias.split(".")[-1]
  1589. if alias == "Dict":
  1590. type_dec = f"{type_dec}, {type_dec}"
  1591. recommended_type = f"{alias}[{type_dec}]"
  1592. if recommended_type is not None:
  1593. if has_variable_annotations:
  1594. hint = f' (hint: "{node.name}: {recommended_type} = ...")'
  1595. else:
  1596. hint = f' (hint: "{node.name} = ... # type: {recommended_type}")'
  1597. if has_variable_annotations:
  1598. needed = "annotation"
  1599. else:
  1600. needed = "comment"
  1601. self.fail(
  1602. f'Need type {needed} for "{unmangle(node.name)}"{hint}',
  1603. context,
  1604. code=codes.VAR_ANNOTATED,
  1605. )
  1606. def explicit_any(self, ctx: Context) -> None:
  1607. self.fail('Explicit "Any" is not allowed', ctx)
  1608. def unsupported_target_for_star_typeddict(self, typ: Type, ctx: Context) -> None:
  1609. self.fail(
  1610. "Unsupported type {} for ** expansion in TypedDict".format(
  1611. format_type(typ, self.options)
  1612. ),
  1613. ctx,
  1614. code=codes.TYPEDDICT_ITEM,
  1615. )
  1616. def non_required_keys_absent_with_star(self, keys: list[str], ctx: Context) -> None:
  1617. self.fail(
  1618. "Non-required {} not explicitly found in any ** item".format(
  1619. format_key_list(keys, short=True)
  1620. ),
  1621. ctx,
  1622. code=codes.TYPEDDICT_ITEM,
  1623. )
  1624. def unexpected_typeddict_keys(
  1625. self,
  1626. typ: TypedDictType,
  1627. expected_keys: list[str],
  1628. actual_keys: list[str],
  1629. context: Context,
  1630. ) -> None:
  1631. actual_set = set(actual_keys)
  1632. expected_set = set(expected_keys)
  1633. if not typ.is_anonymous():
  1634. # Generate simpler messages for some common special cases.
  1635. # Use list comprehension instead of set operations to preserve order.
  1636. missing = [key for key in expected_keys if key not in actual_set]
  1637. if missing:
  1638. self.fail(
  1639. "Missing {} for TypedDict {}".format(
  1640. format_key_list(missing, short=True), format_type(typ, self.options)
  1641. ),
  1642. context,
  1643. code=codes.TYPEDDICT_ITEM,
  1644. )
  1645. extra = [key for key in actual_keys if key not in expected_set]
  1646. if extra:
  1647. self.fail(
  1648. "Extra {} for TypedDict {}".format(
  1649. format_key_list(extra, short=True), format_type(typ, self.options)
  1650. ),
  1651. context,
  1652. code=codes.TYPEDDICT_UNKNOWN_KEY,
  1653. )
  1654. if missing or extra:
  1655. # No need to check for further errors
  1656. return
  1657. found = format_key_list(actual_keys, short=True)
  1658. if not expected_keys:
  1659. self.fail(f"Unexpected TypedDict {found}", context)
  1660. return
  1661. expected = format_key_list(expected_keys)
  1662. if actual_keys and actual_set < expected_set:
  1663. found = f"only {found}"
  1664. self.fail(f"Expected {expected} but found {found}", context, code=codes.TYPEDDICT_ITEM)
  1665. def typeddict_key_must_be_string_literal(self, typ: TypedDictType, context: Context) -> None:
  1666. self.fail(
  1667. "TypedDict key must be a string literal; expected one of {}".format(
  1668. format_item_name_list(typ.items.keys())
  1669. ),
  1670. context,
  1671. code=codes.LITERAL_REQ,
  1672. )
  1673. def typeddict_key_not_found(
  1674. self, typ: TypedDictType, item_name: str, context: Context, setitem: bool = False
  1675. ) -> None:
  1676. """Handle error messages for TypedDicts that have unknown keys.
  1677. Note, that we differentiate in between reading a value and setting a
  1678. value.
  1679. Setting a value on a TypedDict is an 'unknown-key' error, whereas
  1680. reading it is the more serious/general 'item' error.
  1681. """
  1682. if typ.is_anonymous():
  1683. self.fail(
  1684. '"{}" is not a valid TypedDict key; expected one of {}'.format(
  1685. item_name, format_item_name_list(typ.items.keys())
  1686. ),
  1687. context,
  1688. )
  1689. else:
  1690. err_code = codes.TYPEDDICT_UNKNOWN_KEY if setitem else codes.TYPEDDICT_ITEM
  1691. self.fail(
  1692. f'TypedDict {format_type(typ, self.options)} has no key "{item_name}"',
  1693. context,
  1694. code=err_code,
  1695. )
  1696. matches = best_matches(item_name, typ.items.keys(), n=3)
  1697. if matches:
  1698. self.note(
  1699. "Did you mean {}?".format(pretty_seq(matches, "or")), context, code=err_code
  1700. )
  1701. def typeddict_context_ambiguous(self, types: list[TypedDictType], context: Context) -> None:
  1702. formatted_types = ", ".join(list(format_type_distinctly(*types, options=self.options)))
  1703. self.fail(
  1704. f"Type of TypedDict is ambiguous, none of ({formatted_types}) matches cleanly", context
  1705. )
  1706. def typeddict_key_cannot_be_deleted(
  1707. self, typ: TypedDictType, item_name: str, context: Context
  1708. ) -> None:
  1709. if typ.is_anonymous():
  1710. self.fail(f'TypedDict key "{item_name}" cannot be deleted', context)
  1711. else:
  1712. self.fail(
  1713. f'Key "{item_name}" of TypedDict {format_type(typ, self.options)} cannot be deleted',
  1714. context,
  1715. )
  1716. def typeddict_setdefault_arguments_inconsistent(
  1717. self, default: Type, expected: Type, context: Context
  1718. ) -> None:
  1719. msg = 'Argument 2 to "setdefault" of "TypedDict" has incompatible type {}; expected {}'
  1720. self.fail(
  1721. msg.format(format_type(default, self.options), format_type(expected, self.options)),
  1722. context,
  1723. code=codes.TYPEDDICT_ITEM,
  1724. )
  1725. def type_arguments_not_allowed(self, context: Context) -> None:
  1726. self.fail("Parameterized generics cannot be used with class or instance checks", context)
  1727. def disallowed_any_type(self, typ: Type, context: Context) -> None:
  1728. typ = get_proper_type(typ)
  1729. if isinstance(typ, AnyType):
  1730. message = 'Expression has type "Any"'
  1731. else:
  1732. message = f'Expression type contains "Any" (has type {format_type(typ, self.options)})'
  1733. self.fail(message, context)
  1734. def incorrectly_returning_any(self, typ: Type, context: Context) -> None:
  1735. message = (
  1736. f"Returning Any from function declared to return {format_type(typ, self.options)}"
  1737. )
  1738. self.fail(message, context, code=codes.NO_ANY_RETURN)
  1739. def incorrect__exit__return(self, context: Context) -> None:
  1740. self.fail(
  1741. '"bool" is invalid as return type for "__exit__" that always returns False',
  1742. context,
  1743. code=codes.EXIT_RETURN,
  1744. )
  1745. self.note(
  1746. 'Use "typing_extensions.Literal[False]" as the return type or change it to "None"',
  1747. context,
  1748. code=codes.EXIT_RETURN,
  1749. )
  1750. self.note(
  1751. 'If return type of "__exit__" implies that it may return True, '
  1752. "the context manager may swallow exceptions",
  1753. context,
  1754. code=codes.EXIT_RETURN,
  1755. )
  1756. def untyped_decorated_function(self, typ: Type, context: Context) -> None:
  1757. typ = get_proper_type(typ)
  1758. if isinstance(typ, AnyType):
  1759. self.fail("Function is untyped after decorator transformation", context)
  1760. else:
  1761. self.fail(
  1762. f'Type of decorated function contains type "Any" ({format_type(typ, self.options)})',
  1763. context,
  1764. )
  1765. def typed_function_untyped_decorator(self, func_name: str, context: Context) -> None:
  1766. self.fail(f'Untyped decorator makes function "{func_name}" untyped', context)
  1767. def bad_proto_variance(
  1768. self, actual: int, tvar_name: str, expected: int, context: Context
  1769. ) -> None:
  1770. msg = capitalize(
  1771. '{} type variable "{}" used in protocol where'
  1772. " {} one is expected".format(
  1773. variance_string(actual), tvar_name, variance_string(expected)
  1774. )
  1775. )
  1776. self.fail(msg, context)
  1777. def concrete_only_assign(self, typ: Type, context: Context) -> None:
  1778. self.fail(
  1779. f"Can only assign concrete classes to a variable of type {format_type(typ, self.options)}",
  1780. context,
  1781. code=codes.TYPE_ABSTRACT,
  1782. )
  1783. def concrete_only_call(self, typ: Type, context: Context) -> None:
  1784. self.fail(
  1785. f"Only concrete class can be given where {format_type(typ, self.options)} is expected",
  1786. context,
  1787. code=codes.TYPE_ABSTRACT,
  1788. )
  1789. def cannot_use_function_with_type(
  1790. self, method_name: str, type_name: str, context: Context
  1791. ) -> None:
  1792. self.fail(f"Cannot use {method_name}() with {type_name} type", context)
  1793. def report_non_method_protocol(
  1794. self, tp: TypeInfo, members: list[str], context: Context
  1795. ) -> None:
  1796. self.fail(
  1797. "Only protocols that don't have non-method members can be used with issubclass()",
  1798. context,
  1799. )
  1800. if len(members) < 3:
  1801. attrs = ", ".join(members)
  1802. self.note(f'Protocol "{tp.name}" has non-method member(s): {attrs}', context)
  1803. def note_call(
  1804. self, subtype: Type, call: Type, context: Context, *, code: ErrorCode | None
  1805. ) -> None:
  1806. self.note(
  1807. '"{}.__call__" has type {}'.format(
  1808. format_type_bare(subtype, self.options),
  1809. format_type(call, self.options, verbosity=1),
  1810. ),
  1811. context,
  1812. code=code,
  1813. )
  1814. def unreachable_statement(self, context: Context) -> None:
  1815. self.fail("Statement is unreachable", context, code=codes.UNREACHABLE)
  1816. def redundant_left_operand(self, op_name: str, context: Context) -> None:
  1817. """Indicates that the left operand of a boolean expression is redundant:
  1818. it does not change the truth value of the entire condition as a whole.
  1819. 'op_name' should either be the string "and" or the string "or".
  1820. """
  1821. self.redundant_expr(f'Left operand of "{op_name}"', op_name == "and", context)
  1822. def unreachable_right_operand(self, op_name: str, context: Context) -> None:
  1823. """Indicates that the right operand of a boolean expression is redundant:
  1824. it does not change the truth value of the entire condition as a whole.
  1825. 'op_name' should either be the string "and" or the string "or".
  1826. """
  1827. self.fail(
  1828. f'Right operand of "{op_name}" is never evaluated', context, code=codes.UNREACHABLE
  1829. )
  1830. def redundant_condition_in_comprehension(self, truthiness: bool, context: Context) -> None:
  1831. self.redundant_expr("If condition in comprehension", truthiness, context)
  1832. def redundant_condition_in_if(self, truthiness: bool, context: Context) -> None:
  1833. self.redundant_expr("If condition", truthiness, context)
  1834. def redundant_expr(self, description: str, truthiness: bool, context: Context) -> None:
  1835. self.fail(
  1836. f"{description} is always {str(truthiness).lower()}",
  1837. context,
  1838. code=codes.REDUNDANT_EXPR,
  1839. )
  1840. def impossible_intersection(
  1841. self, formatted_base_class_list: str, reason: str, context: Context
  1842. ) -> None:
  1843. template = "Subclass of {} cannot exist: would have {}"
  1844. self.fail(
  1845. template.format(formatted_base_class_list, reason), context, code=codes.UNREACHABLE
  1846. )
  1847. def report_protocol_problems(
  1848. self,
  1849. subtype: Instance | TupleType | TypedDictType | TypeType | CallableType,
  1850. supertype: Instance,
  1851. context: Context,
  1852. *,
  1853. code: ErrorCode | None,
  1854. ) -> None:
  1855. """Report possible protocol conflicts between 'subtype' and 'supertype'.
  1856. This includes missing members, incompatible types, and incompatible
  1857. attribute flags, such as settable vs read-only or class variable vs
  1858. instance variable.
  1859. """
  1860. OFFSET = 4 # Four spaces, so that notes will look like this:
  1861. # note: 'Cls' is missing following 'Proto' members:
  1862. # note: method, attr
  1863. MAX_ITEMS = 2 # Maximum number of conflicts, missing members, and overloads shown
  1864. # List of special situations where we don't want to report additional problems
  1865. exclusions: dict[type, list[str]] = {
  1866. TypedDictType: ["typing.Mapping"],
  1867. TupleType: ["typing.Iterable", "typing.Sequence"],
  1868. }
  1869. if supertype.type.fullname in exclusions.get(type(subtype), []):
  1870. return
  1871. if any(isinstance(tp, UninhabitedType) for tp in get_proper_types(supertype.args)):
  1872. # We don't want to add notes for failed inference (e.g. Iterable[<nothing>]).
  1873. # This will be only confusing a user even more.
  1874. return
  1875. class_obj = False
  1876. is_module = False
  1877. skip = []
  1878. if isinstance(subtype, TupleType):
  1879. if not isinstance(subtype.partial_fallback, Instance):
  1880. return
  1881. subtype = subtype.partial_fallback
  1882. elif isinstance(subtype, TypedDictType):
  1883. if not isinstance(subtype.fallback, Instance):
  1884. return
  1885. subtype = subtype.fallback
  1886. elif isinstance(subtype, TypeType):
  1887. if not isinstance(subtype.item, Instance):
  1888. return
  1889. class_obj = True
  1890. subtype = subtype.item
  1891. elif isinstance(subtype, CallableType):
  1892. if subtype.is_type_obj():
  1893. ret_type = get_proper_type(subtype.ret_type)
  1894. if isinstance(ret_type, TupleType):
  1895. ret_type = ret_type.partial_fallback
  1896. if not isinstance(ret_type, Instance):
  1897. return
  1898. class_obj = True
  1899. subtype = ret_type
  1900. else:
  1901. subtype = subtype.fallback
  1902. skip = ["__call__"]
  1903. if subtype.extra_attrs and subtype.extra_attrs.mod_name:
  1904. is_module = True
  1905. # Report missing members
  1906. missing = get_missing_protocol_members(subtype, supertype, skip=skip)
  1907. if (
  1908. missing
  1909. and (len(missing) < len(supertype.type.protocol_members) or missing == ["__call__"])
  1910. and len(missing) <= MAX_ITEMS
  1911. ):
  1912. if missing == ["__call__"] and class_obj:
  1913. self.note(
  1914. '"{}" has constructor incompatible with "__call__" of "{}"'.format(
  1915. subtype.type.name, supertype.type.name
  1916. ),
  1917. context,
  1918. code=code,
  1919. )
  1920. else:
  1921. self.note(
  1922. '"{}" is missing following "{}" protocol member{}:'.format(
  1923. subtype.type.name, supertype.type.name, plural_s(missing)
  1924. ),
  1925. context,
  1926. code=code,
  1927. )
  1928. self.note(", ".join(missing), context, offset=OFFSET, code=code)
  1929. elif len(missing) > MAX_ITEMS or len(missing) == len(supertype.type.protocol_members):
  1930. # This is an obviously wrong type: too many missing members
  1931. return
  1932. # Report member type conflicts
  1933. conflict_types = get_conflict_protocol_types(subtype, supertype, class_obj=class_obj)
  1934. if conflict_types and (
  1935. not is_subtype(subtype, erase_type(supertype))
  1936. or not subtype.type.defn.type_vars
  1937. or not supertype.type.defn.type_vars
  1938. ):
  1939. type_name = format_type(subtype, self.options, module_names=True)
  1940. self.note(f"Following member(s) of {type_name} have conflicts:", context, code=code)
  1941. for name, got, exp in conflict_types[:MAX_ITEMS]:
  1942. exp = get_proper_type(exp)
  1943. got = get_proper_type(got)
  1944. if not isinstance(exp, (CallableType, Overloaded)) or not isinstance(
  1945. got, (CallableType, Overloaded)
  1946. ):
  1947. self.note(
  1948. "{}: expected {}, got {}".format(
  1949. name, *format_type_distinctly(exp, got, options=self.options)
  1950. ),
  1951. context,
  1952. offset=OFFSET,
  1953. code=code,
  1954. )
  1955. else:
  1956. self.note("Expected:", context, offset=OFFSET, code=code)
  1957. if isinstance(exp, CallableType):
  1958. self.note(
  1959. pretty_callable(exp, self.options, skip_self=class_obj or is_module),
  1960. context,
  1961. offset=2 * OFFSET,
  1962. code=code,
  1963. )
  1964. else:
  1965. assert isinstance(exp, Overloaded)
  1966. self.pretty_overload(
  1967. exp, context, 2 * OFFSET, code=code, skip_self=class_obj or is_module
  1968. )
  1969. self.note("Got:", context, offset=OFFSET, code=code)
  1970. if isinstance(got, CallableType):
  1971. self.note(
  1972. pretty_callable(got, self.options, skip_self=class_obj or is_module),
  1973. context,
  1974. offset=2 * OFFSET,
  1975. code=code,
  1976. )
  1977. else:
  1978. assert isinstance(got, Overloaded)
  1979. self.pretty_overload(
  1980. got, context, 2 * OFFSET, code=code, skip_self=class_obj or is_module
  1981. )
  1982. self.print_more(conflict_types, context, OFFSET, MAX_ITEMS, code=code)
  1983. # Report flag conflicts (i.e. settable vs read-only etc.)
  1984. conflict_flags = get_bad_protocol_flags(subtype, supertype, class_obj=class_obj)
  1985. for name, subflags, superflags in conflict_flags[:MAX_ITEMS]:
  1986. if not class_obj and IS_CLASSVAR in subflags and IS_CLASSVAR not in superflags:
  1987. self.note(
  1988. "Protocol member {}.{} expected instance variable,"
  1989. " got class variable".format(supertype.type.name, name),
  1990. context,
  1991. code=code,
  1992. )
  1993. if not class_obj and IS_CLASSVAR in superflags and IS_CLASSVAR not in subflags:
  1994. self.note(
  1995. "Protocol member {}.{} expected class variable,"
  1996. " got instance variable".format(supertype.type.name, name),
  1997. context,
  1998. code=code,
  1999. )
  2000. if IS_SETTABLE in superflags and IS_SETTABLE not in subflags:
  2001. self.note(
  2002. "Protocol member {}.{} expected settable variable,"
  2003. " got read-only attribute".format(supertype.type.name, name),
  2004. context,
  2005. code=code,
  2006. )
  2007. if IS_CLASS_OR_STATIC in superflags and IS_CLASS_OR_STATIC not in subflags:
  2008. self.note(
  2009. "Protocol member {}.{} expected class or static method".format(
  2010. supertype.type.name, name
  2011. ),
  2012. context,
  2013. code=code,
  2014. )
  2015. if (
  2016. class_obj
  2017. and IS_VAR in superflags
  2018. and (IS_VAR in subflags and IS_CLASSVAR not in subflags)
  2019. ):
  2020. self.note(
  2021. "Only class variables allowed for class object access on protocols,"
  2022. ' {} is an instance variable of "{}"'.format(name, subtype.type.name),
  2023. context,
  2024. code=code,
  2025. )
  2026. if class_obj and IS_CLASSVAR in superflags:
  2027. self.note(
  2028. "ClassVar protocol member {}.{} can never be matched by a class object".format(
  2029. supertype.type.name, name
  2030. ),
  2031. context,
  2032. code=code,
  2033. )
  2034. self.print_more(conflict_flags, context, OFFSET, MAX_ITEMS, code=code)
  2035. def pretty_overload(
  2036. self,
  2037. tp: Overloaded,
  2038. context: Context,
  2039. offset: int,
  2040. *,
  2041. add_class_or_static_decorator: bool = False,
  2042. allow_dups: bool = False,
  2043. code: ErrorCode | None = None,
  2044. skip_self: bool = False,
  2045. ) -> None:
  2046. for item in tp.items:
  2047. self.note("@overload", context, offset=offset, allow_dups=allow_dups, code=code)
  2048. if add_class_or_static_decorator:
  2049. decorator = pretty_class_or_static_decorator(item)
  2050. if decorator is not None:
  2051. self.note(decorator, context, offset=offset, allow_dups=allow_dups, code=code)
  2052. self.note(
  2053. pretty_callable(item, self.options, skip_self=skip_self),
  2054. context,
  2055. offset=offset,
  2056. allow_dups=allow_dups,
  2057. code=code,
  2058. )
  2059. def print_more(
  2060. self,
  2061. conflicts: Sequence[Any],
  2062. context: Context,
  2063. offset: int,
  2064. max_items: int,
  2065. *,
  2066. code: ErrorCode | None = None,
  2067. ) -> None:
  2068. if len(conflicts) > max_items:
  2069. self.note(
  2070. f"<{len(conflicts) - max_items} more conflict(s) not shown>",
  2071. context,
  2072. offset=offset,
  2073. code=code,
  2074. )
  2075. def try_report_long_tuple_assignment_error(
  2076. self,
  2077. subtype: ProperType,
  2078. supertype: ProperType,
  2079. context: Context,
  2080. msg: message_registry.ErrorMessage,
  2081. subtype_label: str | None = None,
  2082. supertype_label: str | None = None,
  2083. ) -> bool:
  2084. """Try to generate meaningful error message for very long tuple assignment
  2085. Returns a bool: True when generating long tuple assignment error,
  2086. False when no such error reported
  2087. """
  2088. if isinstance(subtype, TupleType):
  2089. if (
  2090. len(subtype.items) > 10
  2091. and isinstance(supertype, Instance)
  2092. and supertype.type.fullname == "builtins.tuple"
  2093. ):
  2094. lhs_type = supertype.args[0]
  2095. lhs_types = [lhs_type] * len(subtype.items)
  2096. self.generate_incompatible_tuple_error(lhs_types, subtype.items, context, msg)
  2097. return True
  2098. elif isinstance(supertype, TupleType) and (
  2099. len(subtype.items) > 10 or len(supertype.items) > 10
  2100. ):
  2101. if len(subtype.items) != len(supertype.items):
  2102. if supertype_label is not None and subtype_label is not None:
  2103. msg = msg.with_additional_msg(
  2104. " ({} {}, {} {})".format(
  2105. subtype_label,
  2106. self.format_long_tuple_type(subtype),
  2107. supertype_label,
  2108. self.format_long_tuple_type(supertype),
  2109. )
  2110. )
  2111. self.fail(msg.value, context, code=msg.code)
  2112. return True
  2113. self.generate_incompatible_tuple_error(
  2114. supertype.items, subtype.items, context, msg
  2115. )
  2116. return True
  2117. return False
  2118. def format_long_tuple_type(self, typ: TupleType) -> str:
  2119. """Format very long tuple type using an ellipsis notation"""
  2120. item_cnt = len(typ.items)
  2121. if item_cnt > 10:
  2122. return "{}[{}, {}, ... <{} more items>]".format(
  2123. "tuple" if self.options.use_lowercase_names() else "Tuple",
  2124. format_type_bare(typ.items[0], self.options),
  2125. format_type_bare(typ.items[1], self.options),
  2126. str(item_cnt - 2),
  2127. )
  2128. else:
  2129. return format_type_bare(typ, self.options)
  2130. def generate_incompatible_tuple_error(
  2131. self,
  2132. lhs_types: list[Type],
  2133. rhs_types: list[Type],
  2134. context: Context,
  2135. msg: message_registry.ErrorMessage,
  2136. ) -> None:
  2137. """Generate error message for individual incompatible tuple pairs"""
  2138. error_cnt = 0
  2139. notes: list[str] = []
  2140. for i, (lhs_t, rhs_t) in enumerate(zip(lhs_types, rhs_types)):
  2141. if not is_subtype(lhs_t, rhs_t):
  2142. if error_cnt < 3:
  2143. notes.append(
  2144. "Expression tuple item {} has type {}; {} expected; ".format(
  2145. str(i),
  2146. format_type(rhs_t, self.options),
  2147. format_type(lhs_t, self.options),
  2148. )
  2149. )
  2150. error_cnt += 1
  2151. info = f" ({str(error_cnt)} tuple items are incompatible"
  2152. if error_cnt - 3 > 0:
  2153. info += f"; {str(error_cnt - 3)} items are omitted)"
  2154. else:
  2155. info += ")"
  2156. msg = msg.with_additional_msg(info)
  2157. self.fail(msg.value, context, code=msg.code)
  2158. for note in notes:
  2159. self.note(note, context, code=msg.code)
  2160. def add_fixture_note(self, fullname: str, ctx: Context) -> None:
  2161. self.note(f'Maybe your test fixture does not define "{fullname}"?', ctx)
  2162. if fullname in SUGGESTED_TEST_FIXTURES:
  2163. self.note(
  2164. "Consider adding [builtins fixtures/{}] to your test description".format(
  2165. SUGGESTED_TEST_FIXTURES[fullname]
  2166. ),
  2167. ctx,
  2168. )
  2169. def annotation_in_unchecked_function(self, context: Context) -> None:
  2170. self.note(
  2171. "By default the bodies of untyped functions are not checked,"
  2172. " consider using --check-untyped-defs",
  2173. context,
  2174. code=codes.ANNOTATION_UNCHECKED,
  2175. )
  2176. def quote_type_string(type_string: str) -> str:
  2177. """Quotes a type representation for use in messages."""
  2178. no_quote_regex = r"^<(tuple|union): \d+ items>$"
  2179. if (
  2180. type_string in ["Module", "overloaded function", "<nothing>", "<deleted>"]
  2181. or type_string.startswith("Module ")
  2182. or re.match(no_quote_regex, type_string) is not None
  2183. or type_string.endswith("?")
  2184. ):
  2185. # Messages are easier to read if these aren't quoted. We use a
  2186. # regex to match strings with variable contents.
  2187. return type_string
  2188. return f'"{type_string}"'
  2189. def format_callable_args(
  2190. arg_types: list[Type],
  2191. arg_kinds: list[ArgKind],
  2192. arg_names: list[str | None],
  2193. format: Callable[[Type], str],
  2194. verbosity: int,
  2195. ) -> str:
  2196. """Format a bunch of Callable arguments into a string"""
  2197. arg_strings = []
  2198. for arg_name, arg_type, arg_kind in zip(arg_names, arg_types, arg_kinds):
  2199. if arg_kind == ARG_POS and arg_name is None or verbosity == 0 and arg_kind.is_positional():
  2200. arg_strings.append(format(arg_type))
  2201. else:
  2202. constructor = ARG_CONSTRUCTOR_NAMES[arg_kind]
  2203. if arg_kind.is_star() or arg_name is None:
  2204. arg_strings.append(f"{constructor}({format(arg_type)})")
  2205. else:
  2206. arg_strings.append(f"{constructor}({format(arg_type)}, {repr(arg_name)})")
  2207. return ", ".join(arg_strings)
  2208. def format_type_inner(
  2209. typ: Type,
  2210. verbosity: int,
  2211. options: Options,
  2212. fullnames: set[str] | None,
  2213. module_names: bool = False,
  2214. ) -> str:
  2215. """
  2216. Convert a type to a relatively short string suitable for error messages.
  2217. Args:
  2218. verbosity: a coarse grained control on the verbosity of the type
  2219. fullnames: a set of names that should be printed in full
  2220. """
  2221. def format(typ: Type) -> str:
  2222. return format_type_inner(typ, verbosity, options, fullnames)
  2223. def format_list(types: Sequence[Type]) -> str:
  2224. return ", ".join(format(typ) for typ in types)
  2225. def format_union(types: Sequence[Type]) -> str:
  2226. formatted = [format(typ) for typ in types if format(typ) != "None"]
  2227. if any(format(typ) == "None" for typ in types):
  2228. formatted.append("None")
  2229. return " | ".join(formatted)
  2230. def format_literal_value(typ: LiteralType) -> str:
  2231. if typ.is_enum_literal():
  2232. underlying_type = format(typ.fallback)
  2233. return f"{underlying_type}.{typ.value}"
  2234. else:
  2235. return typ.value_repr()
  2236. if isinstance(typ, TypeAliasType) and typ.is_recursive:
  2237. # TODO: find balance here, str(typ) doesn't support custom verbosity, and may be
  2238. # too verbose for user messages, OTOH it nicely shows structure of recursive types.
  2239. if verbosity < 2:
  2240. type_str = typ.alias.name if typ.alias else "<alias (unfixed)>"
  2241. if typ.args:
  2242. type_str += f"[{format_list(typ.args)}]"
  2243. return type_str
  2244. return str(typ)
  2245. # TODO: always mention type alias names in errors.
  2246. typ = get_proper_type(typ)
  2247. if isinstance(typ, Instance):
  2248. itype = typ
  2249. # Get the short name of the type.
  2250. if itype.type.fullname == "types.ModuleType":
  2251. # Make some common error messages simpler and tidier.
  2252. base_str = "Module"
  2253. if itype.extra_attrs and itype.extra_attrs.mod_name and module_names:
  2254. return f'{base_str} "{itype.extra_attrs.mod_name}"'
  2255. return base_str
  2256. if itype.type.fullname == "typing._SpecialForm":
  2257. # This is not a real type but used for some typing-related constructs.
  2258. return "<typing special form>"
  2259. if itype.type.fullname in reverse_builtin_aliases and not options.use_lowercase_names():
  2260. alias = reverse_builtin_aliases[itype.type.fullname]
  2261. base_str = alias.split(".")[-1]
  2262. elif verbosity >= 2 or (fullnames and itype.type.fullname in fullnames):
  2263. base_str = itype.type.fullname
  2264. else:
  2265. base_str = itype.type.name
  2266. if not itype.args:
  2267. # No type arguments, just return the type name
  2268. return base_str
  2269. elif itype.type.fullname == "builtins.tuple":
  2270. item_type_str = format(itype.args[0])
  2271. return f"{'tuple' if options.use_lowercase_names() else 'Tuple'}[{item_type_str}, ...]"
  2272. else:
  2273. # There are type arguments. Convert the arguments to strings.
  2274. return f"{base_str}[{format_list(itype.args)}]"
  2275. elif isinstance(typ, UnpackType):
  2276. return f"Unpack[{format(typ.type)}]"
  2277. elif isinstance(typ, TypeVarType):
  2278. # This is similar to non-generic instance types.
  2279. return typ.name
  2280. elif isinstance(typ, TypeVarTupleType):
  2281. # This is similar to non-generic instance types.
  2282. return typ.name
  2283. elif isinstance(typ, ParamSpecType):
  2284. # Concatenate[..., P]
  2285. if typ.prefix.arg_types:
  2286. args = format_callable_args(
  2287. typ.prefix.arg_types, typ.prefix.arg_kinds, typ.prefix.arg_names, format, verbosity
  2288. )
  2289. return f"[{args}, **{typ.name_with_suffix()}]"
  2290. else:
  2291. return typ.name_with_suffix()
  2292. elif isinstance(typ, TupleType):
  2293. # Prefer the name of the fallback class (if not tuple), as it's more informative.
  2294. if typ.partial_fallback.type.fullname != "builtins.tuple":
  2295. return format(typ.partial_fallback)
  2296. if options.use_lowercase_names():
  2297. s = f"tuple[{format_list(typ.items)}]"
  2298. else:
  2299. s = f"Tuple[{format_list(typ.items)}]"
  2300. return s
  2301. elif isinstance(typ, TypedDictType):
  2302. # If the TypedDictType is named, return the name
  2303. if not typ.is_anonymous():
  2304. return format(typ.fallback)
  2305. items = []
  2306. for item_name, item_type in typ.items.items():
  2307. modifier = "" if item_name in typ.required_keys else "?"
  2308. items.append(f"{item_name!r}{modifier}: {format(item_type)}")
  2309. s = f"TypedDict({{{', '.join(items)}}})"
  2310. return s
  2311. elif isinstance(typ, LiteralType):
  2312. return f"Literal[{format_literal_value(typ)}]"
  2313. elif isinstance(typ, UnionType):
  2314. literal_items, union_items = separate_union_literals(typ)
  2315. # Coalesce multiple Literal[] members. This also changes output order.
  2316. # If there's just one Literal item, retain the original ordering.
  2317. if len(literal_items) > 1:
  2318. literal_str = "Literal[{}]".format(
  2319. ", ".join(format_literal_value(t) for t in literal_items)
  2320. )
  2321. if len(union_items) == 1 and isinstance(get_proper_type(union_items[0]), NoneType):
  2322. return (
  2323. f"{literal_str} | None"
  2324. if options.use_or_syntax()
  2325. else f"Optional[{literal_str}]"
  2326. )
  2327. elif union_items:
  2328. return (
  2329. f"{literal_str} | {format_union(union_items)}"
  2330. if options.use_or_syntax()
  2331. else f"Union[{format_list(union_items)}, {literal_str}]"
  2332. )
  2333. else:
  2334. return literal_str
  2335. else:
  2336. # Only print Union as Optional if the Optional wouldn't have to contain another Union
  2337. print_as_optional = (
  2338. len(typ.items) - sum(isinstance(get_proper_type(t), NoneType) for t in typ.items)
  2339. == 1
  2340. )
  2341. if print_as_optional:
  2342. rest = [t for t in typ.items if not isinstance(get_proper_type(t), NoneType)]
  2343. return (
  2344. f"{format(rest[0])} | None"
  2345. if options.use_or_syntax()
  2346. else f"Optional[{format(rest[0])}]"
  2347. )
  2348. else:
  2349. s = (
  2350. format_union(typ.items)
  2351. if options.use_or_syntax()
  2352. else f"Union[{format_list(typ.items)}]"
  2353. )
  2354. return s
  2355. elif isinstance(typ, NoneType):
  2356. return "None"
  2357. elif isinstance(typ, AnyType):
  2358. return "Any"
  2359. elif isinstance(typ, DeletedType):
  2360. return "<deleted>"
  2361. elif isinstance(typ, UninhabitedType):
  2362. if typ.is_noreturn:
  2363. return "NoReturn"
  2364. else:
  2365. return "<nothing>"
  2366. elif isinstance(typ, TypeType):
  2367. type_name = "type" if options.use_lowercase_names() else "Type"
  2368. return f"{type_name}[{format(typ.item)}]"
  2369. elif isinstance(typ, FunctionLike):
  2370. func = typ
  2371. if func.is_type_obj():
  2372. # The type of a type object type can be derived from the
  2373. # return type (this always works).
  2374. return format(TypeType.make_normalized(erase_type(func.items[0].ret_type)))
  2375. elif isinstance(func, CallableType):
  2376. if func.type_guard is not None:
  2377. return_type = f"TypeGuard[{format(func.type_guard)}]"
  2378. else:
  2379. return_type = format(func.ret_type)
  2380. if func.is_ellipsis_args:
  2381. return f"Callable[..., {return_type}]"
  2382. param_spec = func.param_spec()
  2383. if param_spec is not None:
  2384. return f"Callable[{format(param_spec)}, {return_type}]"
  2385. args = format_callable_args(
  2386. func.arg_types, func.arg_kinds, func.arg_names, format, verbosity
  2387. )
  2388. return f"Callable[[{args}], {return_type}]"
  2389. else:
  2390. # Use a simple representation for function types; proper
  2391. # function types may result in long and difficult-to-read
  2392. # error messages.
  2393. return "overloaded function"
  2394. elif isinstance(typ, UnboundType):
  2395. return typ.accept(TypeStrVisitor(options=options))
  2396. elif isinstance(typ, Parameters):
  2397. args = format_callable_args(typ.arg_types, typ.arg_kinds, typ.arg_names, format, verbosity)
  2398. return f"[{args}]"
  2399. elif typ is None:
  2400. raise RuntimeError("Type is None")
  2401. else:
  2402. # Default case; we simply have to return something meaningful here.
  2403. return "object"
  2404. def collect_all_instances(t: Type) -> list[Instance]:
  2405. """Return all instances that `t` contains (including `t`).
  2406. This is similar to collect_all_inner_types from typeanal but only
  2407. returns instances and will recurse into fallbacks.
  2408. """
  2409. visitor = CollectAllInstancesQuery()
  2410. t.accept(visitor)
  2411. return visitor.instances
  2412. class CollectAllInstancesQuery(TypeTraverserVisitor):
  2413. def __init__(self) -> None:
  2414. self.instances: list[Instance] = []
  2415. def visit_instance(self, t: Instance) -> None:
  2416. self.instances.append(t)
  2417. super().visit_instance(t)
  2418. def visit_type_alias_type(self, t: TypeAliasType) -> None:
  2419. if t.alias and not t.is_recursive:
  2420. t.alias.target.accept(self)
  2421. super().visit_type_alias_type(t)
  2422. def find_type_overlaps(*types: Type) -> set[str]:
  2423. """Return a set of fullnames that share a short name and appear in either type.
  2424. This is used to ensure that distinct types with the same short name are printed
  2425. with their fullname.
  2426. """
  2427. d: dict[str, set[str]] = {}
  2428. for type in types:
  2429. for inst in collect_all_instances(type):
  2430. d.setdefault(inst.type.name, set()).add(inst.type.fullname)
  2431. for shortname in d.keys():
  2432. if f"typing.{shortname}" in TYPES_FOR_UNIMPORTED_HINTS:
  2433. d[shortname].add(f"typing.{shortname}")
  2434. overlaps: set[str] = set()
  2435. for fullnames in d.values():
  2436. if len(fullnames) > 1:
  2437. overlaps.update(fullnames)
  2438. return overlaps
  2439. def format_type(
  2440. typ: Type, options: Options, verbosity: int = 0, module_names: bool = False
  2441. ) -> str:
  2442. """
  2443. Convert a type to a relatively short string suitable for error messages.
  2444. `verbosity` is a coarse grained control on the verbosity of the type
  2445. This function returns a string appropriate for unmodified use in error
  2446. messages; this means that it will be quoted in most cases. If
  2447. modification of the formatted string is required, callers should use
  2448. format_type_bare.
  2449. """
  2450. return quote_type_string(format_type_bare(typ, options, verbosity, module_names))
  2451. def format_type_bare(
  2452. typ: Type, options: Options, verbosity: int = 0, module_names: bool = False
  2453. ) -> str:
  2454. """
  2455. Convert a type to a relatively short string suitable for error messages.
  2456. `verbosity` is a coarse grained control on the verbosity of the type
  2457. `fullnames` specifies a set of names that should be printed in full
  2458. This function will return an unquoted string. If a caller doesn't need to
  2459. perform post-processing on the string output, format_type should be used
  2460. instead. (The caller may want to use quote_type_string after
  2461. processing has happened, to maintain consistent quoting in messages.)
  2462. """
  2463. return format_type_inner(typ, verbosity, options, find_type_overlaps(typ), module_names)
  2464. def format_type_distinctly(*types: Type, options: Options, bare: bool = False) -> tuple[str, ...]:
  2465. """Jointly format types to distinct strings.
  2466. Increase the verbosity of the type strings until they become distinct
  2467. while also requiring that distinct types with the same short name are
  2468. formatted distinctly.
  2469. By default, the returned strings are created using format_type() and will be
  2470. quoted accordingly. If ``bare`` is True, the returned strings will not
  2471. be quoted; callers who need to do post-processing of the strings before
  2472. quoting them (such as prepending * or **) should use this.
  2473. """
  2474. overlapping = find_type_overlaps(*types)
  2475. for verbosity in range(2):
  2476. strs = [
  2477. format_type_inner(type, verbosity=verbosity, options=options, fullnames=overlapping)
  2478. for type in types
  2479. ]
  2480. if len(set(strs)) == len(strs):
  2481. break
  2482. if bare:
  2483. return tuple(strs)
  2484. else:
  2485. return tuple(quote_type_string(s) for s in strs)
  2486. def pretty_class_or_static_decorator(tp: CallableType) -> str | None:
  2487. """Return @classmethod or @staticmethod, if any, for the given callable type."""
  2488. if tp.definition is not None and isinstance(tp.definition, SYMBOL_FUNCBASE_TYPES):
  2489. if tp.definition.is_class:
  2490. return "@classmethod"
  2491. if tp.definition.is_static:
  2492. return "@staticmethod"
  2493. return None
  2494. def pretty_callable(tp: CallableType, options: Options, skip_self: bool = False) -> str:
  2495. """Return a nice easily-readable representation of a callable type.
  2496. For example:
  2497. def [T <: int] f(self, x: int, y: T) -> None
  2498. If skip_self is True, print an actual callable type, as it would appear
  2499. when bound on an instance/class, rather than how it would appear in the
  2500. defining statement.
  2501. """
  2502. s = ""
  2503. asterisk = False
  2504. slash = False
  2505. for i in range(len(tp.arg_types)):
  2506. if s:
  2507. s += ", "
  2508. if tp.arg_kinds[i].is_named() and not asterisk:
  2509. s += "*, "
  2510. asterisk = True
  2511. if tp.arg_kinds[i] == ARG_STAR:
  2512. s += "*"
  2513. asterisk = True
  2514. if tp.arg_kinds[i] == ARG_STAR2:
  2515. s += "**"
  2516. name = tp.arg_names[i]
  2517. if name:
  2518. s += name + ": "
  2519. type_str = format_type_bare(tp.arg_types[i], options)
  2520. if tp.arg_kinds[i] == ARG_STAR2 and tp.unpack_kwargs:
  2521. type_str = f"Unpack[{type_str}]"
  2522. s += type_str
  2523. if tp.arg_kinds[i].is_optional():
  2524. s += " = ..."
  2525. if (
  2526. not slash
  2527. and tp.arg_kinds[i].is_positional()
  2528. and name is None
  2529. and (
  2530. i == len(tp.arg_types) - 1
  2531. or (tp.arg_names[i + 1] is not None or not tp.arg_kinds[i + 1].is_positional())
  2532. )
  2533. ):
  2534. s += ", /"
  2535. slash = True
  2536. # If we got a "special arg" (i.e: self, cls, etc...), prepend it to the arg list
  2537. if isinstance(tp.definition, FuncDef) and hasattr(tp.definition, "arguments"):
  2538. definition_arg_names = [arg.variable.name for arg in tp.definition.arguments]
  2539. if (
  2540. len(definition_arg_names) > len(tp.arg_names)
  2541. and definition_arg_names[0]
  2542. and not skip_self
  2543. ):
  2544. if s:
  2545. s = ", " + s
  2546. s = definition_arg_names[0] + s
  2547. s = f"{tp.definition.name}({s})"
  2548. elif tp.name:
  2549. first_arg = tp.def_extras.get("first_arg")
  2550. if first_arg:
  2551. if s:
  2552. s = ", " + s
  2553. s = first_arg + s
  2554. s = f"{tp.name.split()[0]}({s})" # skip "of Class" part
  2555. else:
  2556. s = f"({s})"
  2557. s += " -> "
  2558. if tp.type_guard is not None:
  2559. s += f"TypeGuard[{format_type_bare(tp.type_guard, options)}]"
  2560. else:
  2561. s += format_type_bare(tp.ret_type, options)
  2562. if tp.variables:
  2563. tvars = []
  2564. for tvar in tp.variables:
  2565. if isinstance(tvar, TypeVarType):
  2566. upper_bound = get_proper_type(tvar.upper_bound)
  2567. if (
  2568. isinstance(upper_bound, Instance)
  2569. and upper_bound.type.fullname != "builtins.object"
  2570. ):
  2571. tvars.append(f"{tvar.name} <: {format_type_bare(upper_bound, options)}")
  2572. elif tvar.values:
  2573. tvars.append(
  2574. "{} in ({})".format(
  2575. tvar.name,
  2576. ", ".join([format_type_bare(tp, options) for tp in tvar.values]),
  2577. )
  2578. )
  2579. else:
  2580. tvars.append(tvar.name)
  2581. else:
  2582. # For other TypeVarLikeTypes, just use the repr
  2583. tvars.append(repr(tvar))
  2584. s = f"[{', '.join(tvars)}] {s}"
  2585. return f"def {s}"
  2586. def variance_string(variance: int) -> str:
  2587. if variance == COVARIANT:
  2588. return "covariant"
  2589. elif variance == CONTRAVARIANT:
  2590. return "contravariant"
  2591. else:
  2592. return "invariant"
  2593. def get_missing_protocol_members(left: Instance, right: Instance, skip: list[str]) -> list[str]:
  2594. """Find all protocol members of 'right' that are not implemented
  2595. (i.e. completely missing) in 'left'.
  2596. """
  2597. assert right.type.is_protocol
  2598. missing: list[str] = []
  2599. for member in right.type.protocol_members:
  2600. if member in skip:
  2601. continue
  2602. if not find_member(member, left, left):
  2603. missing.append(member)
  2604. return missing
  2605. def get_conflict_protocol_types(
  2606. left: Instance, right: Instance, class_obj: bool = False
  2607. ) -> list[tuple[str, Type, Type]]:
  2608. """Find members that are defined in 'left' but have incompatible types.
  2609. Return them as a list of ('member', 'got', 'expected').
  2610. """
  2611. assert right.type.is_protocol
  2612. conflicts: list[tuple[str, Type, Type]] = []
  2613. for member in right.type.protocol_members:
  2614. if member in ("__init__", "__new__"):
  2615. continue
  2616. supertype = find_member(member, right, left)
  2617. assert supertype is not None
  2618. subtype = mypy.typeops.get_protocol_member(left, member, class_obj)
  2619. if not subtype:
  2620. continue
  2621. is_compat = is_subtype(subtype, supertype, ignore_pos_arg_names=True)
  2622. if IS_SETTABLE in get_member_flags(member, right):
  2623. is_compat = is_compat and is_subtype(supertype, subtype)
  2624. if not is_compat:
  2625. conflicts.append((member, subtype, supertype))
  2626. return conflicts
  2627. def get_bad_protocol_flags(
  2628. left: Instance, right: Instance, class_obj: bool = False
  2629. ) -> list[tuple[str, set[int], set[int]]]:
  2630. """Return all incompatible attribute flags for members that are present in both
  2631. 'left' and 'right'.
  2632. """
  2633. assert right.type.is_protocol
  2634. all_flags: list[tuple[str, set[int], set[int]]] = []
  2635. for member in right.type.protocol_members:
  2636. if find_member(member, left, left):
  2637. item = (member, get_member_flags(member, left), get_member_flags(member, right))
  2638. all_flags.append(item)
  2639. bad_flags = []
  2640. for name, subflags, superflags in all_flags:
  2641. if (
  2642. IS_CLASSVAR in subflags
  2643. and IS_CLASSVAR not in superflags
  2644. and IS_SETTABLE in superflags
  2645. or IS_CLASSVAR in superflags
  2646. and IS_CLASSVAR not in subflags
  2647. or IS_SETTABLE in superflags
  2648. and IS_SETTABLE not in subflags
  2649. or IS_CLASS_OR_STATIC in superflags
  2650. and IS_CLASS_OR_STATIC not in subflags
  2651. or class_obj
  2652. and IS_VAR in superflags
  2653. and IS_CLASSVAR not in subflags
  2654. or class_obj
  2655. and IS_CLASSVAR in superflags
  2656. ):
  2657. bad_flags.append((name, subflags, superflags))
  2658. return bad_flags
  2659. def capitalize(s: str) -> str:
  2660. """Capitalize the first character of a string."""
  2661. if s == "":
  2662. return ""
  2663. else:
  2664. return s[0].upper() + s[1:]
  2665. def extract_type(name: str) -> str:
  2666. """If the argument is the name of a method (of form C.m), return
  2667. the type portion in quotes (e.g. "y"). Otherwise, return the string
  2668. unmodified.
  2669. """
  2670. name = re.sub('^"[a-zA-Z0-9_]+" of ', "", name)
  2671. return name
  2672. def strip_quotes(s: str) -> str:
  2673. """Strip a double quote at the beginning and end of the string, if any."""
  2674. s = re.sub('^"', "", s)
  2675. s = re.sub('"$', "", s)
  2676. return s
  2677. def format_string_list(lst: list[str]) -> str:
  2678. assert lst
  2679. if len(lst) == 1:
  2680. return lst[0]
  2681. elif len(lst) <= 5:
  2682. return f"{', '.join(lst[:-1])} and {lst[-1]}"
  2683. else:
  2684. return "%s, ... and %s (%i methods suppressed)" % (
  2685. ", ".join(lst[:2]),
  2686. lst[-1],
  2687. len(lst) - 3,
  2688. )
  2689. def format_item_name_list(s: Iterable[str]) -> str:
  2690. lst = list(s)
  2691. if len(lst) <= 5:
  2692. return "(" + ", ".join([f'"{name}"' for name in lst]) + ")"
  2693. else:
  2694. return "(" + ", ".join([f'"{name}"' for name in lst[:5]]) + ", ...)"
  2695. def callable_name(type: FunctionLike) -> str | None:
  2696. name = type.get_name()
  2697. if name is not None and name[0] != "<":
  2698. return f'"{name}"'.replace(" of ", '" of "')
  2699. return name
  2700. def for_function(callee: CallableType) -> str:
  2701. name = callable_name(callee)
  2702. if name is not None:
  2703. return f" for {name}"
  2704. return ""
  2705. def wrong_type_arg_count(n: int, act: str, name: str) -> str:
  2706. s = f"{n} type arguments"
  2707. if n == 0:
  2708. s = "no type arguments"
  2709. elif n == 1:
  2710. s = "1 type argument"
  2711. if act == "0":
  2712. act = "none"
  2713. return f'"{name}" expects {s}, but {act} given'
  2714. def find_defining_module(modules: dict[str, MypyFile], typ: CallableType) -> MypyFile | None:
  2715. if not typ.definition:
  2716. return None
  2717. fullname = typ.definition.fullname
  2718. if "." in fullname:
  2719. for i in range(fullname.count(".")):
  2720. module_name = fullname.rsplit(".", i + 1)[0]
  2721. try:
  2722. return modules[module_name]
  2723. except KeyError:
  2724. pass
  2725. assert False, "Couldn't determine module from CallableType"
  2726. return None
  2727. # For hard-coding suggested missing member alternatives.
  2728. COMMON_MISTAKES: Final[dict[str, Sequence[str]]] = {"add": ("append", "extend")}
  2729. def _real_quick_ratio(a: str, b: str) -> float:
  2730. # this is an upper bound on difflib.SequenceMatcher.ratio
  2731. # similar to difflib.SequenceMatcher.real_quick_ratio, but faster since we don't instantiate
  2732. al = len(a)
  2733. bl = len(b)
  2734. return 2.0 * min(al, bl) / (al + bl)
  2735. def best_matches(current: str, options: Collection[str], n: int) -> list[str]:
  2736. if not current:
  2737. return []
  2738. # narrow down options cheaply
  2739. options = [o for o in options if _real_quick_ratio(current, o) > 0.75]
  2740. if len(options) >= 50:
  2741. options = [o for o in options if abs(len(o) - len(current)) <= 1]
  2742. ratios = {option: difflib.SequenceMatcher(a=current, b=option).ratio() for option in options}
  2743. options = [option for option, ratio in ratios.items() if ratio > 0.75]
  2744. return sorted(options, key=lambda v: (-ratios[v], v))[:n]
  2745. def pretty_seq(args: Sequence[str], conjunction: str) -> str:
  2746. quoted = ['"' + a + '"' for a in args]
  2747. if len(quoted) == 1:
  2748. return quoted[0]
  2749. if len(quoted) == 2:
  2750. return f"{quoted[0]} {conjunction} {quoted[1]}"
  2751. last_sep = ", " + conjunction + " "
  2752. return ", ".join(quoted[:-1]) + last_sep + quoted[-1]
  2753. def append_invariance_notes(
  2754. notes: list[str], arg_type: Instance, expected_type: Instance
  2755. ) -> list[str]:
  2756. """Explain that the type is invariant and give notes for how to solve the issue."""
  2757. invariant_type = ""
  2758. covariant_suggestion = ""
  2759. if (
  2760. arg_type.type.fullname == "builtins.list"
  2761. and expected_type.type.fullname == "builtins.list"
  2762. and is_subtype(arg_type.args[0], expected_type.args[0])
  2763. ):
  2764. invariant_type = "List"
  2765. covariant_suggestion = 'Consider using "Sequence" instead, which is covariant'
  2766. elif (
  2767. arg_type.type.fullname == "builtins.dict"
  2768. and expected_type.type.fullname == "builtins.dict"
  2769. and is_same_type(arg_type.args[0], expected_type.args[0])
  2770. and is_subtype(arg_type.args[1], expected_type.args[1])
  2771. ):
  2772. invariant_type = "Dict"
  2773. covariant_suggestion = (
  2774. 'Consider using "Mapping" instead, ' "which is covariant in the value type"
  2775. )
  2776. if invariant_type and covariant_suggestion:
  2777. notes.append(
  2778. f'"{invariant_type}" is invariant -- see '
  2779. + "https://mypy.readthedocs.io/en/stable/common_issues.html#variance"
  2780. )
  2781. notes.append(covariant_suggestion)
  2782. return notes
  2783. def append_numbers_notes(
  2784. notes: list[str], arg_type: Instance, expected_type: Instance
  2785. ) -> list[str]:
  2786. """Explain if an unsupported type from "numbers" is used in a subtype check."""
  2787. if expected_type.type.fullname in UNSUPPORTED_NUMBERS_TYPES:
  2788. notes.append('Types from "numbers" aren\'t supported for static type checking')
  2789. notes.append("See https://peps.python.org/pep-0484/#the-numeric-tower")
  2790. notes.append("Consider using a protocol instead, such as typing.SupportsFloat")
  2791. return notes
  2792. def make_inferred_type_note(
  2793. context: Context, subtype: Type, supertype: Type, supertype_str: str
  2794. ) -> str:
  2795. """Explain that the user may have forgotten to type a variable.
  2796. The user does not expect an error if the inferred container type is the same as the return
  2797. type of a function and the argument type(s) are a subtype of the argument type(s) of the
  2798. return type. This note suggests that they add a type annotation with the return type instead
  2799. of relying on the inferred type.
  2800. """
  2801. subtype = get_proper_type(subtype)
  2802. supertype = get_proper_type(supertype)
  2803. if (
  2804. isinstance(subtype, Instance)
  2805. and isinstance(supertype, Instance)
  2806. and subtype.type.fullname == supertype.type.fullname
  2807. and subtype.args
  2808. and supertype.args
  2809. and isinstance(context, ReturnStmt)
  2810. and isinstance(context.expr, NameExpr)
  2811. and isinstance(context.expr.node, Var)
  2812. and context.expr.node.is_inferred
  2813. ):
  2814. for subtype_arg, supertype_arg in zip(subtype.args, supertype.args):
  2815. if not is_subtype(subtype_arg, supertype_arg):
  2816. return ""
  2817. var_name = context.expr.name
  2818. return 'Perhaps you need a type annotation for "{}"? Suggestion: {}'.format(
  2819. var_name, supertype_str
  2820. )
  2821. return ""
  2822. def format_key_list(keys: list[str], *, short: bool = False) -> str:
  2823. formatted_keys = [f'"{key}"' for key in keys]
  2824. td = "" if short else "TypedDict "
  2825. if len(keys) == 0:
  2826. return f"no {td}keys"
  2827. elif len(keys) == 1:
  2828. return f"{td}key {formatted_keys[0]}"
  2829. else:
  2830. return f"{td}keys ({', '.join(formatted_keys)})"