diff --git a/mathics/eval/assignments/assignment.py b/mathics/eval/assignments/assignment.py index b956fa27b8..3bf9151744 100644 --- a/mathics/eval/assignments/assignment.py +++ b/mathics/eval/assignments/assignment.py @@ -471,11 +471,15 @@ def eval_assign_format( ] lhs = lhs.elements[0] lhs_unwrapped = unwrap_expression(lhs) - lhs_unwrapped = ( - lhs_unwrapped.get_head() - if not isinstance(lhs_unwrapped, Symbol) - else lhs_unwrapped - ) + # For a named pattern or a blank, as in Format[t_h] or Format[_h], + # keep the pattern: the tag is h, which is found inside it below. + # Otherwise, the tag is the head, as in Format[h[x_]]. + if not ( + isinstance(lhs_unwrapped, Symbol) + or lhs_unwrapped.has_form(SymbolPattern, 2) + or lhs_unwrapped.get_head() in BLANK_PATTERN_HEADS + ): + lhs_unwrapped = lhs_unwrapped.get_head() target_symbol_names = collect_lhs_names_and_upset_dont_allow_custom( target_symbol_names, upset, op_name, lhs, lhs_unwrapped, evaluation ) diff --git a/test/builtin/assignments/test_assignment.py b/test/builtin/assignments/test_assignment.py index 16ea2bae4c..dc80a65f8f 100644 --- a/test/builtin/assignments/test_assignment.py +++ b/test/builtin/assignments/test_assignment.py @@ -178,6 +178,22 @@ def test_assign_list(): '"{2, a}"', "store the conditional rule for T...", ), + # Named patterns and blanks as the argument of Format + ( + 'ClearAll[A,G]; Format[A_G]:="g" /; Length[A] > 1; {G[1,2], G[1]}//ToString', + '"{g, G[1]}"', + "Format[A_G] is stored for G, not for Pattern", + ), + ( + 'ClearAll[G]; Format[_G]:="g"; G[1]//ToString', + '"g"', + "Format[_G] is stored for G", + ), + ( + 'ClearAll[t,G]; Format[t:G[__]]:="g"; G[1]//ToString', + '"g"', + "Format[t:G[__]] is stored for G", + ), # Upvalues ( "ClearAll[F,A,Y,x]; A=T; F[A[x_],Y[x_]]^:=x^2; ClearAll[A,F,Y]; F[T[2],Y[2]]",