ML ibUddlmZddlmZddlmZddlmZddlm Z m Z m Z ddl m ZddlmZddlmZmZmZmZmZdd lmZmZmZmZmZmZmZmZdd l m!Z!dd l"m#Z#m$Z$dd l%m&Z&dd l'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5ddl6m7Z7e eeefZ8de9d<Gdde Z:GddZ;eeeGddZ?ddZ@ddZAy)) annotations) defaultdict)Iterator)contextmanager) NamedTupleOptionalUnion) TypeAlias)!remove_instance_last_known_values)Keyextract_var_from_literal_hashliteral literal_hashsubkeys) LITERAL_NO Expression IndexExpr MemberExprNameExprRefExprTypeInfoVar)Options) is_same_type is_subtype)make_simplified_union)AnyTypeInstanceNoneType PartialType ProperType TupleTypeType TypeOfAnyTypeType TypeVarType UnionType UnpackTypefind_unpack_in_listget_proper_type)fill_typevars_with_any _TypeAliasBindableExpressionc"eZdZUded<ded<y) CurrentTyper#typeboolfrom_assignmentN)__name__ __module__ __qualname____annotations__Q/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypy/binder.pyr/r/-s Jr8r/c"eZdZdZdddZddZy)FrameaA Frame represents a specific point in the execution of a program. It carries information about the current types of expressions at that point, arising either from assignments to those expressions or the result of isinstance checks and other type narrowing operations. It also records whether it is possible to reach that point at all. We add a new frame wherenever there is a new scope or control flow branching. This information is not copied into a new Frame when it is pushed onto the stack, so a given Frame only has information about types that were assigned in that frame. Expressions are stored in dicts using 'literal hashes' as keys (type "Key"). These are hashable values derived from expression AST nodes (only those that can be narrowed). literal_hash(expr) is used to calculate the hashes. Note that this isn't directly related to literal types -- the concept predates literal types. cJ||_i|_d|_||_d|_y)NF)idtypes unreachableconditional_framesuppress_unreachable_warnings)selfr=r@s r9__init__zFrame.__init__Is(-/  !2-2*r8c pd|jd|jd|jd|jd S)NzFrame(z, ))r=r>r?r@rBs r9__repr__zFrame.__repr__Ps:y4::,b1A1A0B"TE[E[D\\]^^r8NF)r=intr@r1returnNone)rJstr)r3r4r5__doc__rCrGr7r8r9r;r;2s,3_r8r;cFeZdZUdZdZded<d$dZd%dZd&d'dZd(d)d Z d*d+d Z d*d,d Z e d-d Z d dd.dZd/dZd/dZd0dZd1dZd1dZd2dZd3dZd4dZd5dZed6dZd7dZd8dZd9dZd/dZd/dZedd d ddd! d:d"Zed;d#Z y)<ConditionalTypeBinderaKeep track of conditional types of variables. NB: Variables are tracked by literal hashes of expressions, so it is possible to confuse the binder when there is aliasing. Example: class A: a: int | str x = A() lst = [x] reveal_type(x.a) # int | str x.a = 1 reveal_type(x.a) # int reveal_type(lst[0].a) # int | str lst[0].a = 'a' reveal_type(x.a) # int reveal_type(lst[0].a) # str NzAssigns | Nonetype_assignmentscd|_t|jg|_g|_i|_i|_d|_t|_ g|_ g|_ |j|_ d|_y)NFr)next_idr;_get_idframesoptions_on_return declarations dependencieslast_pop_changedset try_frames break_framescontinue_framesallow_redefinition_newbind_allversion)rBoptionss r9rCzConditionalTypeBinder.__init__os{ T\\^,- 575724!&%(E')*,  66  r8cD|xjdz c_|jSNrR)rSrFs r9rTzConditionalTypeBinder._get_ids  ||r8c||}n3|jj|tj|t |D]}|j ||yN)rX setdefaultrZaddr_add_dependencies)rBkeyvalueelts r9rhz'ConditionalTypeBinder._add_dependenciessR =E    ( (ce 4 8 8 ?3< /C  " "3 . /r8Fct|j|}|jj||jjg|S)z!Push a new frame into the binder.)r;rTrUappendrV)rBr@fs r9 push_framez ConditionalTypeBinder.push_frames@ $,,."3 4 1 %%b)r8cz|xjdz c_t|||j|j|<yrc)r`r/rUr>)rBrir0r2indexs r9_putzConditionalTypeBinder._puts0  (3D/(J E  %r8c|dkr|t|jz }t|ddD];}||j|jvs|j|j|cSy)Nr)lenrUranger>)rBrirqis r9_getzConditionalTypeBinder._getsh 19 S% %Eub"% 1Adkk!n***{{1~++C00 1r8c`t|tttfxrt |t kDS)zWill `.put()` on this expression be successful? This is inlined in `.put()` because the logic is rather hot and must be kept in sync. ) isinstancerrrrr)clsexprs r9can_put_directlyz&ConditionalTypeBinder.can_put_directlys'$J ABawt}WaGaar8Tr2ct|tttfsyt |syt |}|Jd||j vr)t||j |<|j||j|||y)zDirectly set the narrowed type of expression (if it supports it). This is used for isinstance() etc. Assignments should go through assign_type(). Nz/Internal error: binder tried to put non-literal) rzrrrrrrWget_declarationrhrr)rBr|typr2ris r9putzConditionalTypeBinder.puts} $J AB t} 4 Q QQ d'' '%4T%:D  c "  " "3 ' #sO,r8cV|xjdz c_d|jd_y)NrRTrt)r`rUr?rFs r9r?z!ConditionalTypeBinder.unreachables   &* B#r8c,d|jd_y)NTrt)rUrArFs r9rAz3ConditionalTypeBinder.suppress_unreachable_warningss8< B5r8cjt|}|Jd|j|}|y|jS)Nz/Internal error: binder tried to get non-literal)rrxr0)rBr|rifounds r9getzConditionalTypeBinder.gets;4 Q QQ # =zzr8c:td|jDS)Nc34K|]}|jywrer?.0rns r9 z7ConditionalTypeBinder.is_unreachable..s6Q1==6anyrUrFs r9is_unreachablez$ConditionalTypeBinder.is_unreachables6$++666r8c:td|jDS)Nc34K|]}|jywre)rArs r9rzJConditionalTypeBinder.is_unreachable_warning_suppressed..sHq122HrrrFs r9!is_unreachable_warning_suppressedz7ConditionalTypeBinder.is_unreachable_warning_suppressedsHDKKHHHr8cNt|}|Jd|j|y)z0Remove all references to a Node from the binder.Nz0Internal error: binder tried cleanse non-literal)r _cleanse_key)rBr|ris r9cleansezConditionalTypeBinder.cleanses*4 R RR #r8c^|jD]}||jvs|j|= y)z/Remove all references to a key from the binder.N)rUr>)rBriframes r9rz"ConditionalTypeBinder._cleanse_keys-[[ %Eekk!KK$ %r8c td|D}|s|Dcgc]}|jr|}}d}|Dcgc]}|jD]}|}}}t|dkDrt t |}|D]?}|j |}|Dcgc]}|jj|| }}|j xs t|du} | rtd|Dro|D cgc]} | |  }} |rtd|Dr|d} | J| jt|jj|} t| tr4tfd|ddDstt j"| ng} |D]!}|J| j%|j#t| dk(r| dnt'| t| t(rt+| s| tt,r t/| r{tt0rktt,r[t-j2j4Dcgc]}tt|tr| c}}||j|k(r||t7|jr*|j9|d d }B| |j:d _|Scc}wcc}}wcc}wcc} wcc}w) zUpdate the frame to reflect that each key will be updated as in one of the frames. Return whether any item changes. If a key is declared as AnyType, only update it if all the options are the same. c36K|]}|j ywrerrs r9rz.s>! ->sFrRNc3$K|]}|du ywrer7rxs r9rz. s$I1Q$Y$Isc3BK|]}|duxr |j ywrer~rs r9rz.s*%<= 7a&7&7"77%src3VK|] }|duxrt|j"ywre)rr0)rtr0s r9rz.$s-EFATM@l4&@@s&)) source_anyTr~rt)allr?r>rulistrZrxrr_r rr0r*rWrzrr$from_another_anyrmrr&rr'collapse_variadic_unionr! make_unionitemsrrrrU)rBrU all_reachablernchangedrikeys current_valueresulting_values old_semanticsr current_typedeclaration_typepossible_typesr simplifiedr0s @r9update_from_optionsz)ConditionalTypeBinder.update_from_optionss>v>> !'=Aq}}a=F=#7qww7777 t9q=D ?DM C IIcNMIOPA C ?P P !% -[1Ns1SW[1[M$I8H$I!I+;Maq}M M%AQ%"+A.L+ ++$$D.t/@/@/D/DS/IJ *G4JZ[\[]J^#9#=#=JZ!#)2A=(="))!&&12~&!+*!,D0@D!"2K@.J 0"$ 26t<%&tZ8&tY7&/%9%9(, b1*_UVEWY`:aQb& &):):3)??#-D$L}?Q?Q,R #tT :[M ^+1j B#m>7 Q Npcs-KKK#K-K$5K$ K) +K) c |dkDr|j| |jj}|jj}|r|j d|jd|j ||_|S)zcPop a frame and return it. See frame_context() for documentation of fall_through. rrt) allow_jumprUpoprVinsertrrY)rBcan_skip fall_throughresultras r9 pop_framezConditionalTypeBinder.pop_frameVso !  OO\M *"((,,.  NN1dkk"o . $ 8 8 A r8c#Kd}|j |j}tt|_|j||_yw)aPush a new map to collect assigned types in multiassign from union. If this map is not None, actual binding is deferred until all items in the union are processed (a union of collected items is later bound manually by the caller). N)rPrr)rBold_assignmentss r9accumulate_type_assignmentsz1ConditionalTypeBinder.accumulate_type_assignmentsisE  ,"33O +D 1### /sAAct|}|j!|j|j||fyt|tt t fsyt|sy|j||yt||syt|}t|}t|tr"t|trDt|jtr*|jjr|j!||nVt|t"rlt%d|j&DrP|j&Dcgc]}tt|t(r|n|!}}|j!|t#|nt|t"r/t%d|j&Dr|j!||n|j!||nt|trft|trCt|jtr)|jjr|j!||n%|j!||n|j!|||j*D]}|j-|ycc}w)aNarrow type of expression through an assignment. Do nothing if the expression doesn't support narrowing. When not narrowing though an assignment (isinstance() etc.), use put() directly. This omits some special-casing logic for assignments. Nc3NK|]}tt|tywre)rzr*rritems r9rz4ConditionalTypeBinder.assign_type..s!;@D ?40(;;#%c3NK|]}tt|tywre)rzr*rrs r9rz4ConditionalTypeBinder.assign_type..s!;?C ?40':;r)r rPrmrzrrrrinvalidate_dependenciesrr*rrnoder is_inferredrr'rrrr[r) rBr|r0 declared_type p_declaredp_typer new_itemsrws r9 assign_typez!ConditionalTypeBinder.assign_typexs 16  ,  ! !$ ' . .m/D E $J AB t}  $$T*   $ . $]3  & fg &$(Z 3-GDIILaLat$J 2s;HRHXHX;8!+ 0 0't'updater?rm)rBrqrrns r9rz ConditionalTypeBinder.allow_jumps 19 S//0 0Edlln%UQY[) )A KK  qww '}}$(! ) u%,,U3r8c`|j|jd|jyNrt)rr\r?rFs r9 handle_breakz"ConditionalTypeBinder.handle_breaks% ))"-. r8c`|j|jd|jyr)rr]r?rFs r9handle_continuez%ConditionalTypeBinder.handle_continues% ,,R01 r8rRr)r break_framecontinue_framer@ try_framec#Kt|jdkDsJ|r1|jjt|j|z |r1|jjt|j|z |r1|j j t|jdz |j|}|r|jd||j|||r|jj|r|jj|r2|j jt|jdz yyw)a4Return a context manager that pushes/pops frames on enter/exit. If can_skip is True, control flow is allowed to bypass the newly-created frame. If fall_through > 0, then it will allow control flow that falls off the end of the frame to escape to its ancestor `fall_through` levels higher. Otherwise control flow ends at the end of the frame. If break_frame > 0, then 'break' statements within this frame will jump out to the frame break_frame levels higher than the frame created by this call to frame_context. Similarly for continue_frame and 'continue' statements. If try_frame is true, then execution is allowed to jump at any point within the newly created frame (or its descendants) to its parent (i.e., to the frame that was on top before this call to frame_context). After the context manager exits, self.last_pop_changed indicates whether any types changed in the newly-topmost frame as a result of popping this frame. rRrtN) rurUr\rmr]r[rgrorrrremove)rBrrrrr@r new_frames r9 frame_contextz#ConditionalTypeBinder.frame_contextsF4;;!###     $ $S%5 %C D   ' 'DKK(8>(I J  OO  DKK 01 4 5OO$56  OOB  x.     ! ! #   $ $ &  OO " "3t{{#3a#7 8 sEEc#Kt|jdk(sJ|j|jddt|jdk(sJyw)zqA variant of frame_context for use at the top level of a namespace (module, function, or class). rRTrN)rurUrorrFs r9top_frame_contextz'ConditionalTypeBinder.top_frame_context"sQ 4;;1$$$oo tQ4;;1$$$sAA)rarrJrK)rJrIre)rir rjz Key | NonerJrKrH)r@r1rJr;)rt) rir r0r#r2r1rqrIrJrK)rir rqrIrJzCurrentType | None)r|rrJr1)r|rrr#r2r1rJrK)rJrK)r|rrJ Type | None)rJr1)r|rrJrK)rir rJrK)rUz list[Frame]rJr1)rr1rrIrJr;)rJzIterator[Assigns])r|rr0r#rrrJrK)r|r-rJrK)rqrIrJrK)rr1rrIrrIrrIr@r1rr1rJIterator[Frame])rJr)!r3r4r5rMrPr6rCrTrhrorrrx classmethodr}rr?rArrrrrrrrrrrrrrrrr7r8r9rOrOWs5*(,n+)V/KbbKO- +=7 I % _B& 0 0M^ # 4 "'7979 79  79  79 7979 7979r%%r8rOc,t|trt|jtr2|jj}tt |t s|Syt|jtrtt|jSy)zGet the declared or inferred type of a RefExpr expression. Return None if there is no type or the expression is not a RefExpr. This can return None if the type hasn't been inferred yet. N) rzrrrr0r*r rr%r+)r|r0s r9rr-sj $ dii %99>>Dod3[A   8 ,2499=> > r8cg}g}|jD]@}t|}t|tr|j |0|j |Bt |dkr|St |d}|d}|d}t|j}||S|j|}t|tsJt|j} t| ts|S| jjdk(sJ|j|dzd} t |jt | kr|S| r|jt |  d| k7r|S| r|jdt |  } n |j} | jd} t|ddD]%\} }|j| | g| dzzz| zk7s#|cS|j| | gt |jdz zz|gz| zk7r|St |jdk(r| j}nt| |gz| z|j}t!j"|g|zS) aSimplify a union involving variadic tuple if possible. This will collapse a type like e.g. tuple[X, Z] | tuple[X, Y, Z] | tuple[X, Y, Y, *tuple[Y, ...], Z] back to tuple[X, *tuple[Y, ...], Z] which is equivalent, but much simpler form of the same type. rRc,t|jSre)rur)rs r9z)collapse_variadic_union..QsCLr8)rirrtNzbuiltins.tuple)fallback)rr*rzr"rmrusortedr)r(r0rfullnameargs enumerate copy_modifiedpartial_fallbackr'r)r tuple_items other_itemsrp_tfirstlast unpack_indexunpackunpackedsuffixprefixargrwitrs r9rr=sFKK YY"a  c9 %   s #   q ! "  ;1 *@AK NE r?D&tzz2L ZZ %F fj )) )v{{+H h ) == ! !%5 55 5 ZZ q(* +F 5;;#f+%  %++s6{ln-7  ^F |, -- C;q,-2 88vQ/&8 8J  zzVses399~'9::fXENN  5;;1#113 v069DDYDYZ    { : ;;r8N)r|r-rJr)rr'rJr#)B __future__r collectionsrcollections.abcr contextlibrtypingrrr typing_extensionsr r,mypy.erasetyper mypy.literalsr r rrr mypy.nodesrrrrrrrr mypy.optionsr mypy.subtypesrr mypy.typeopsr mypy.typesrrrr r!r"r#r$r%r&r'r(r)r* mypy.typevarsr+r-r6r/r;rtupleAssignsrOrrr7r8r9r s"#$%..5<\\   !2. 1!&y*h'F!GJG* __D j$uT8D>-A'B"CC DS%S%l  9