7474 ASSIGN_TYPES = (ast .Assign , ast .AnnAssign )
7575 AssignType = Union [ASSIGN_TYPES ] # type: ignore
7676
77+ PY311 = sys .version_info >= (3 , 11 )
7778MODULE_IGNORE_ID_NAME = "__varname_ignore_id__"
7879
7980
@@ -281,6 +282,7 @@ def bytecode_nameof(code: CodeType, offset: int) -> str:
281282 "CALL_FUNCTION" ,
282283 "CALL_METHOD" ,
283284 "CALL" ,
285+ "CALL_KW" ,
284286 ):
285287 raise VarnameRetrievingError ("Did you call 'nameof' in a weird way?" )
286288
@@ -290,7 +292,7 @@ def bytecode_nameof(code: CodeType, offset: int) -> str:
290292 current_instruction_index -= 1
291293 name_instruction = instructions [current_instruction_index ]
292294
293- if name_instruction .opname == "KW_NAMES" : # pragma: no cover
295+ if name_instruction .opname in ( "KW_NAMES" , "LOAD_CONST" ) : # LOAD_CONST python 3.13
294296 raise pos_only_error
295297
296298 if not name_instruction .opname .startswith ("LOAD_" ):
@@ -533,22 +535,38 @@ def _(node: Union[ast.Attribute, ast.Subscript]) -> ast.Call:
533535
534536 # x[1], x.a
535537 if isinstance (node .ctx , ast .Load ):
536- return ast .Call (
537- func = ast .Attribute (
538- value = node .value ,
539- attr = (
540- "__getitem__"
541- if isinstance (node , ast .Subscript )
542- else "__getattr__"
538+ if PY311 :
539+ return ast .Call (
540+ func = ast .Attribute (
541+ value = node .value ,
542+ attr = (
543+ "__getitem__"
544+ if isinstance (node , ast .Subscript )
545+ else "__getattr__"
546+ ),
547+ ctx = ast .Load (),
548+ ** nodemeta ,
543549 ),
544- ctx = ast .Load (),
545- ** nodemeta ,
546- ),
547- args = [keynode ],
548- keywords = [],
549- starargs = None ,
550- kwargs = None ,
551- )
550+ args = [keynode ],
551+ keywords = [],
552+ )
553+ else :
554+ return ast .Call (
555+ func = ast .Attribute (
556+ value = node .value ,
557+ attr = (
558+ "__getitem__"
559+ if isinstance (node , ast .Subscript )
560+ else "__getattr__"
561+ ),
562+ ctx = ast .Load (),
563+ ** nodemeta ,
564+ ),
565+ args = [keynode ],
566+ keywords = [],
567+ starargs = None ,
568+ kwargs = None ,
569+ )
552570
553571 # x[a] = b, x.a = b
554572 if (
@@ -564,22 +582,38 @@ def _(node: Union[ast.Attribute, ast.Subscript]) -> ast.Call:
564582 )
565583 )
566584
567- return ast .Call (
568- func = ast .Attribute (
569- value = node .value ,
570- attr = (
571- "__setitem__"
572- if isinstance (node , ast .Subscript )
573- else "__setattr__"
585+ if PY311 :
586+ return ast .Call (
587+ func = ast .Attribute (
588+ value = node .value ,
589+ attr = (
590+ "__setitem__"
591+ if isinstance (node , ast .Subscript )
592+ else "__setattr__"
593+ ),
594+ ctx = ast .Load (),
595+ ** nodemeta ,
574596 ),
575- ctx = ast .Load (),
576- ** nodemeta ,
577- ),
578- args = [keynode , node .parent .value ], # type: ignore
579- keywords = [],
580- starargs = None ,
581- kwargs = None ,
582- )
597+ args = [keynode , node .parent .value ], # type: ignore
598+ keywords = [],
599+ )
600+ else :
601+ return ast .Call (
602+ func = ast .Attribute (
603+ value = node .value ,
604+ attr = (
605+ "__setitem__"
606+ if isinstance (node , ast .Subscript )
607+ else "__setattr__"
608+ ),
609+ ctx = ast .Load (),
610+ ** nodemeta ,
611+ ),
612+ args = [keynode , node .parent .value ], # type: ignore
613+ keywords = [],
614+ starargs = None ,
615+ kwargs = None ,
616+ )
583617
584618
585619@reconstruct_func_node .register (ast .Compare )
@@ -593,18 +627,30 @@ def _(node: ast.Compare) -> ast.Call:
593627 "lineno" : node .lineno ,
594628 "col_offset" : node .col_offset ,
595629 }
596- return ast .Call (
597- func = ast .Attribute (
598- value = node .left ,
599- attr = CMP2MAGIC [type (node .ops [0 ])],
600- ctx = ast .Load (),
601- ** nodemeta ,
602- ),
603- args = [node .comparators [0 ]],
604- keywords = [],
605- starargs = None ,
606- kwargs = None ,
607- )
630+ if PY311 :
631+ return ast .Call (
632+ func = ast .Attribute (
633+ value = node .left ,
634+ attr = CMP2MAGIC [type (node .ops [0 ])],
635+ ctx = ast .Load (),
636+ ** nodemeta ,
637+ ),
638+ args = [node .comparators [0 ]],
639+ keywords = [],
640+ )
641+ else :
642+ return ast .Call (
643+ func = ast .Attribute (
644+ value = node .left ,
645+ attr = CMP2MAGIC [type (node .ops [0 ])],
646+ ctx = ast .Load (),
647+ ** nodemeta ,
648+ ),
649+ args = [node .comparators [0 ]],
650+ keywords = [],
651+ starargs = None ,
652+ kwargs = None ,
653+ )
608654
609655
610656@reconstruct_func_node .register (ast .BinOp )
@@ -614,18 +660,31 @@ def _(node: ast.BinOp) -> ast.Call:
614660 "lineno" : node .lineno ,
615661 "col_offset" : node .col_offset ,
616662 }
617- return ast .Call (
618- func = ast .Attribute (
619- value = node .left ,
620- attr = OP2MAGIC [type (node .op )],
621- ctx = ast .Load (),
622- ** nodemeta ,
623- ),
624- args = [node .right ],
625- keywords = [],
626- starargs = None ,
627- kwargs = None ,
628- )
663+
664+ if PY311 :
665+ return ast .Call (
666+ func = ast .Attribute (
667+ value = node .left ,
668+ attr = OP2MAGIC [type (node .op )],
669+ ctx = ast .Load (),
670+ ** nodemeta ,
671+ ),
672+ args = [node .right ],
673+ keywords = [],
674+ )
675+ else :
676+ return ast .Call (
677+ func = ast .Attribute (
678+ value = node .left ,
679+ attr = OP2MAGIC [type (node .op )],
680+ ctx = ast .Load (),
681+ ** nodemeta ,
682+ ),
683+ args = [node .right ],
684+ keywords = [],
685+ starargs = None ,
686+ kwargs = None ,
687+ )
629688
630689
631690def rich_exc_message (msg : str , node : ast .AST , context_lines : int = 4 ) -> str :
0 commit comments