f _ c@sNdZddlmZddlmZmZmZmZmZm Z dZ dZ Gdddej Z dS) aAdjust some old Python 2 idioms to their modern counterparts. * Change some type comparisons to isinstance() calls: type(x) == T -> isinstance(x, T) type(x) is T -> isinstance(x, T) type(x) != T -> not isinstance(x, T) type(x) is not T -> not isinstance(x, T) * Change "while 1:" into "while True:". * Change both v = list(EXPR) v.sort() foo(v) and the more general v = EXPR v.sort() foo(v) into v = sorted(EXPR) foo(v) i) fixer_base)CallCommaNameNode BlankLinesymsz0(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)z(power< 'type' trailer< '(' x=any ')' > >csPeZdZdZdeeeefZfddZddZddZ d d Z d d Z Z S) FixIdiomsTa isinstance=comparison< %s %s T=any > | isinstance=comparison< T=any %s %s > | while_stmt< 'while' while='1' ':' any+ > | sorted=any< any* simple_stmt< expr_stmt< id1=any '=' power< list='list' trailer< '(' (not arglist) any ')' > > > '\n' > sort= simple_stmt< power< id2=any trailer< '.' 'sort' > trailer< '(' ')' > > '\n' > next=any* > | sorted=any< any* simple_stmt< expr_stmt< id1=any '=' expr=any > '\n' > sort= simple_stmt< power< id2=any trailer< '.' 'sort' > trailer< '(' ')' > > '\n' > next=any* > cs8tt||}|r4d|vr4|d|dkr0|SdS|S)NsortedZid1Zid2)Zsuperrmatch)selfnodeZrZ __class__1/usr/lib64/python3.10/lib2to3/fixes/fix_idioms.pyr Os  zFixIdioms.matchcCsHd|vr|||Sd|vr(|||Sd|vr<|||StddS)N isinstancewhilerz Invalid match)transform_isinstancetransform_whiletransform_sort RuntimeError)r r resultsr r r transformZs   zFixIdioms.transformcCsh|d}|d}d|_d|_ttd|t|g}d|vr\d|_ttjtd|g}|j|_|S)NxTz rZnZnot)cloneprefixrrrrrZnot_test)r r rrrZtestr r rrds  zFixIdioms.transform_isinstancecCs |d}|td|jddS)NrZTruer)replacerr)r r rZoner r rrpszFixIdioms.transform_whilec Cs|d}|d}|d}|d}|r>|td|jdn8|rn|}d|_|ttd|g|jdntd||j}d |vr|r|d d |d jf} d | |d _nH|j sJ|j dusJt } |j | |j | usJ|d d | _dS) NZsortZnextZlistZexprrrrzshould not have reached hereZ i)ZgetrrrrrrZremoveZ rpartitionZjoinZparentZ next_siblingrZ append_child) r r rZ sort_stmtZ next_stmtZ list_callZ simple_exprZnewZbtwnZ prefix_linesZend_liner r rrts2      zFixIdioms.transform_sort) Z__name__Z __module__Z __qualname__ZexplicitTYPECMPZPATTERNr rrrrZ __classcell__r r r rr%s% '   rN)Z__doc__rrZ fixer_utilrrrrrrrrZBaseFixrr r r rZs