@@ -6050,26 +6050,7 @@ def accept(
60506050 typ = self .visit_conditional_expr (node , allow_none_return = True )
60516051 elif allow_none_return and isinstance (node , AwaitExpr ):
60526052 typ = self .visit_await_expr (node , allow_none_return = True )
6053- # Deeply nested generic calls can deteriorate performance dramatically.
6054- # Although in most cases caching makes little difference, in worst case
6055- # it avoids exponential complexity.
6056- # We cannot use cache inside lambdas, because they skip immediate type
6057- # context, and use enclosing one, see infer_lambda_type_using_context().
6058- # TODO: consider using cache for more expression kinds.
6059- elif (
6060- isinstance (node , (CallExpr , ListExpr , TupleExpr , DictExpr , OpExpr ))
6061- and not (self .in_lambda_expr or self .chk .current_node_deferred )
6062- and not self .chk .options .disable_expression_cache
6063- ):
6064- if (node , type_context ) in self .expr_cache :
6065- binder_version , typ , messages , type_map = self .expr_cache [(node , type_context )]
6066- if binder_version == self .chk .binder .version :
6067- self .chk .store_types (type_map )
6068- self .msg .add_errors (messages )
6069- else :
6070- typ = self .accept_maybe_cache (node , type_context = type_context )
6071- else :
6072- typ = self .accept_maybe_cache (node , type_context = type_context )
6053+
60736054 elif (
60746055 isinstance (p_type_context , TypeType )
60756056 and p_type_context .is_type_form
@@ -6102,6 +6083,26 @@ def accept(
61026083 else :
61036084 typ2 = node .accept (self )
61046085 typ = typ2 # r-value type, when interpreted as a value expression
6086+ # Deeply nested generic calls can deteriorate performance dramatically.
6087+ # Although in most cases caching makes little difference, in worst case
6088+ # it avoids exponential complexity.
6089+ # We cannot use cache inside lambdas, because they skip immediate type
6090+ # context, and use enclosing one, see infer_lambda_type_using_context().
6091+ # TODO: consider using cache for more expression kinds.
6092+ elif (
6093+ isinstance (node , (CallExpr , ListExpr , TupleExpr , DictExpr , OpExpr ))
6094+ and not (self .in_lambda_expr or self .chk .current_node_deferred )
6095+ and not self .chk .options .disable_expression_cache
6096+ ):
6097+ if (node , type_context ) in self .expr_cache :
6098+ binder_version , typ , messages , type_map = self .expr_cache [(node , type_context )]
6099+ if binder_version == self .chk .binder .version :
6100+ self .chk .store_types (type_map )
6101+ self .msg .add_errors (messages )
6102+ else :
6103+ typ = self .accept_maybe_cache (node , type_context = type_context )
6104+ else :
6105+ typ = self .accept_maybe_cache (node , type_context = type_context )
61056106 else :
61066107 typ = node .accept (self ) # r-value type, when interpreted as a value expression
61076108 except Exception as err :
0 commit comments