1313 List ,
1414 Optional ,
1515 Sequence ,
16+ Set ,
1617 Tuple ,
17- Type ,
1818 Union ,
1919)
2020
@@ -273,7 +273,7 @@ class Expression(BaseElement, NumericOperators, EvalMixin):
273273 _sequences : Any
274274 _cache : Optional [ExpressionCache ]
275275 elements_properties : Optional [ElementsProperties ]
276- options : Optional [tuple ]
276+ options : Optional [Dict [ str , Any ] ]
277277 pattern_sequence : bool
278278
279279 def __init__ (
@@ -535,11 +535,11 @@ def evaluate(
535535 if evaluation .timeout :
536536 return None
537537
538- expr = self
538+ expr : Optional [ BaseElement ] = self
539539 reevaluate = True
540540 limit = None
541541 iteration = 1
542- names = set ()
542+ names : Set [ str ] = set ()
543543 definitions = evaluation .definitions
544544
545545 old_options = evaluation .options
@@ -553,6 +553,8 @@ def evaluate(
553553 try :
554554 # Evaluation loop:
555555 while reevaluate :
556+ assert isinstance (expr , EvalMixin )
557+
556558 # If definitions have not changed in the last evaluation,
557559 # then evaluating again will produce the same result
558560 if not expr .is_uncertain_final_definitions (definitions ):
@@ -702,16 +704,20 @@ def flatten_with_respect_to_head(
702704 sub_level = level - 1
703705 do_flatten = False
704706 for element in self ._elements :
705- if element .get_head ().sameQ (head ) and (
706- not pattern_only or element .pattern_sequence
707+ if (
708+ isinstance (element , Expression )
709+ and element .get_head ().sameQ (head )
710+ and (not pattern_only or element .pattern_sequence )
707711 ):
708712 do_flatten = True
709713 break
710714 if do_flatten :
711- new_elements = []
715+ new_elements : List [ BaseElement ] = []
712716 for element in self ._elements :
713- if element .get_head ().sameQ (head ) and (
714- not pattern_only or element .pattern_sequence
717+ if (
718+ isinstance (element , Expression )
719+ and element .get_head ().sameQ (head )
720+ and (not pattern_only or element .pattern_sequence )
715721 ):
716722 new_element = element .flatten_with_respect_to_head (
717723 head , pattern_only , callback , level = sub_level
@@ -953,14 +959,16 @@ def get_sort_key(self, pattern_sort=False) -> tuple:
953959 3: tuple: list of Elements
954960 4: 1: No clue...
955961 """
956- exps : Dict [str , float ] = {}
962+ exps : Dict [str , Union [ float , complex ] ] = {}
957963 head = self ._head
958964 if head is SymbolTimes :
959965 for element in self .elements :
960966 name = element .get_name ()
961967 if element .has_form ("Power" , 2 ):
962968 var = element .get_element (0 ).get_name ()
963- exp = element .get_element (1 ).round_to_float ()
969+ expr = element .get_element (1 )
970+ assert isinstance (expr , (Expression , NumericOperators ))
971+ exp = expr .round_to_float ()
964972 if var and exp is not None :
965973 exps [var ] = exps .get (var , 0 ) + exp
966974 elif name :
@@ -1042,6 +1050,7 @@ def is_uncertain_final_definitions(self, definitions) -> bool:
10421050
10431051 if cache .symbols is None :
10441052 cache = self ._rebuild_cache ()
1053+ assert cache is not None
10451054
10461055 return definitions .is_uncertain_final_value (time , cache .symbols )
10471056
@@ -1123,7 +1132,7 @@ def restructure(self, head, elements, evaluation, structure_cache=None, deps=Non
11231132 s = structure (head , deps , evaluation , structure_cache = structure_cache )
11241133 return s (list (elements ))
11251134
1126- def rewrite_apply_eval_step (self , evaluation ) -> Tuple ["Expression" , bool ]:
1135+ def rewrite_apply_eval_step (self , evaluation ) -> Tuple [BaseElement , bool ]:
11271136 """Perform a single rewrite/apply/eval step of the bigger
11281137 Expression.evaluate() process.
11291138
@@ -1162,6 +1171,7 @@ def rewrite_apply_eval_step(self, evaluation) -> Tuple["Expression", bool]:
11621171
11631172 if self .elements_properties is None :
11641173 self ._build_elements_properties ()
1174+ assert self .elements_properties is not None
11651175
11661176 # @timeit
11671177 def eval_elements ():
@@ -1804,14 +1814,15 @@ def thread(self, evaluation, head=None) -> Tuple[bool, "Expression"]:
18041814 if head is None :
18051815 head = SymbolList
18061816
1807- items = []
1817+ prefix : List [BaseElement ] = []
1818+ items : List [List [BaseElement ]]
18081819 dim = None
18091820 for element in self ._elements :
18101821 if element .get_head ().sameQ (head ):
18111822 if dim is None :
18121823 dim = len (element .get_elements ())
18131824 items = [
1814- (items + [innerelement ])
1825+ (prefix + [innerelement ])
18151826 for innerelement in element .get_elements ()
18161827 ]
18171828 elif len (element ._elements ) != dim :
@@ -1822,7 +1833,7 @@ def thread(self, evaluation, head=None) -> Tuple[bool, "Expression"]:
18221833 items [index ].append (element ._elements [index ])
18231834 else :
18241835 if dim is None :
1825- items .append (element )
1836+ prefix .append (element )
18261837 else :
18271838 for item in items :
18281839 item .append (element )
@@ -2040,6 +2051,7 @@ def convert_expression_elements(
20402051 elements_properties .is_flat = False
20412052 if converted_elt .elements_properties is None :
20422053 converted_elt ._build_elements_properties ()
2054+ assert converted_elt .elements_properties is not None
20432055
20442056 if elements_properties .elements_fully_evaluated :
20452057 elements_properties .elements_fully_evaluated = (
0 commit comments