@@ -521,40 +521,48 @@ def translate_stmt_list(
521521 return [block ]
522522
523523 stack = self .class_and_function_stack
524- if self .strip_function_bodies and len (stack ) == 1 and stack [0 ] == "F" :
524+ # Fast case for stripping function bodies
525+ if (
526+ can_strip
527+ and self .strip_function_bodies
528+ and len (stack ) == 1
529+ and stack [0 ] == "F"
530+ and not is_coroutine
531+ ):
525532 return []
526533
527534 res : list [Statement ] = []
528535 for stmt in stmts :
529536 node = self .visit (stmt )
530537 res .append (node )
531538
532- if (
533- self .strip_function_bodies
534- and can_strip
535- and stack [- 2 :] == ["C" , "F" ]
536- and not is_possible_trivial_body (res )
537- ):
538- # We only strip method bodies if they don't assign to an attribute, as
539- # this may define an attribute which has an externally visible effect.
540- visitor = FindAttributeAssign ()
541- for s in res :
542- s .accept (visitor )
543- if visitor .found :
544- break
545- else :
546- if is_coroutine :
547- # Yields inside an async function affect the return type and should not
548- # be stripped.
549- yield_visitor = FindYield ()
539+ # Slow case for stripping function bodies
540+ if can_strip and self .strip_function_bodies :
541+ if stack [- 2 :] == ["C" , "F" ]:
542+ if is_possible_trivial_body (res ):
543+ can_strip = False
544+ else :
545+ # We only strip method bodies if they don't assign to an attribute, as
546+ # this may define an attribute which has an externally visible effect.
547+ visitor = FindAttributeAssign ()
550548 for s in res :
551- s .accept (yield_visitor )
552- if yield_visitor .found :
549+ s .accept (visitor )
550+ if visitor .found :
551+ can_strip = False
553552 break
554- else :
555- return []
556- else :
557- return []
553+
554+ if can_strip and stack [- 1 ] == "F" and is_coroutine :
555+ # Yields inside an async function affect the return type and should not
556+ # be stripped.
557+ yield_visitor = FindYield ()
558+ for s in res :
559+ s .accept (yield_visitor )
560+ if yield_visitor .found :
561+ can_strip = False
562+ break
563+
564+ if can_strip :
565+ return []
558566 return res
559567
560568 def translate_type_comment (
0 commit comments