Skip to content

Commit

Permalink
Make Drop[] of 0th not error, DRY "normal" error messages, and go o…
Browse files Browse the repository at this point in the history
…ver `Drop` doc (#1225)

* Make `Drop[]` of  0th not error 
* DRY "normal" error messages
* Go over `Drop` doc
* Move some doctests to pytests when they belong there
* Move some pytests to less generic and more the more specific module
that the test tests

Fixes #1223
  • Loading branch information
rocky authored Dec 14, 2024
1 parent 0af3ccc commit 2ae6b7d
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 121 deletions.
4 changes: 0 additions & 4 deletions mathics/builtin/assignments/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,6 @@ class UpSet(InfixOperator, _SetOperator):
>> UpValues[b]
= {HoldPattern[a[b]] :> 3}
>> a ^= 3
: Nonatomic expression expected.
= 3
You can use 'UpSet' to specify special values like format values.
However, these values will not be saved in 'UpValues':
>> Format[r] ^= "custom";
Expand Down
7 changes: 5 additions & 2 deletions mathics/builtin/atomic/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mathics_scanner.tokeniser import is_symbol_name

from mathics.core.assignment import get_symbol_values
from mathics.core.atoms import String
from mathics.core.atoms import Integer1, String
from mathics.core.attributes import (
A_HOLD_ALL,
A_HOLD_FIRST,
Expand Down Expand Up @@ -39,6 +39,7 @@
)
from mathics.core.systemsymbols import (
SymbolAttributes,
SymbolContext,
SymbolDefinition,
SymbolFormat,
SymbolGrid,
Expand Down Expand Up @@ -122,7 +123,9 @@ def eval(self, symbol, evaluation):

name = symbol.get_name()
if not name:
evaluation.message("Context", "normal")
evaluation.message(
"Context", "normal", Integer1, Expression(SymbolContext, symbol)
)
return
assert "`" in name
context = name[: name.rindex("`") + 1]
Expand Down
8 changes: 5 additions & 3 deletions mathics/builtin/exp_structure/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Structural Expression Functions
"""

from mathics.core.atoms import Integer
from mathics.core.atoms import Integer, Integer1
from mathics.core.builtin import Builtin, InfixOperator, Predefined
from mathics.core.exceptions import InvalidLevelspecError
from mathics.core.expression import Evaluation, Expression
from mathics.core.list import ListExpression
from mathics.core.rules import BasePattern
from mathics.core.symbols import Atom, SymbolFalse, SymbolTrue
from mathics.core.systemsymbols import SymbolMap
from mathics.core.systemsymbols import SymbolMap, SymbolSortBy
from mathics.eval.parts import python_levelspec, walk_levels


Expand Down Expand Up @@ -265,7 +265,9 @@ def eval(self, li, f, evaluation: Evaluation):
"SortBy[li_, f_]"

if isinstance(li, Atom):
evaluation.message("Sort", "normal")
evaluation.message(
"Sort", "normal", Integer1, Expression(SymbolSortBy, li, f)
)
return
elif li.get_head_name() != "System`List":
expr = Expression(SymbolSortBy, li, f)
Expand Down
13 changes: 7 additions & 6 deletions mathics/builtin/intfns/combinatorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from itertools import combinations

from mathics.core.atoms import Integer
from mathics.core.atoms import Integer, Integer1
from mathics.core.attributes import (
A_LISTABLE,
A_N_HOLD_FIRST,
Expand Down Expand Up @@ -536,7 +536,6 @@ class Subsets(Builtin):

messages = {
"nninfseq": "Position 2 of `1` must be All, Infinity, a non-negative integer, or a List whose first element (required) is a non-negative integer, second element (optional) is a non-negative integer or Infinity, and third element (optional) is a nonzero integer.",
"normal": "Nonatomic expression expected at position 1 in `1`.",
}

rules = {
Expand All @@ -549,7 +548,9 @@ def eval_list(self, list, evaluation):
"Subsets[list_]"

if isinstance(list, Atom):
evaluation.message("Subsets", "normal", Expression(SymbolSubsets, list))
evaluation.message(
"Subsets", "normal", Integer1, Expression(SymbolSubsets, list)
)
else:
return self.eval_list_n(list, Integer(len(list.elements)), evaluation)

Expand All @@ -558,7 +559,7 @@ def eval_list_n(self, list, n, evaluation):

expr = Expression(SymbolSubsets, list, n)
if isinstance(list, Atom):
evaluation.message("Subsets", "normal", expr)
evaluation.message("Subsets", "normal", Integer1, expr)
return
else:
head_t = list.head
Expand All @@ -584,7 +585,7 @@ def eval_list_pattern(self, list, n, evaluation):
expr = Expression(SymbolSubsets, list, n)

if isinstance(list, Atom):
evaluation.message("Subsets", "normal", expr)
evaluation.message("Subsets", "normal", Integer1, expr)
return
else:
head_t = list.head
Expand Down Expand Up @@ -663,7 +664,7 @@ def eval_atom_pattern(self, list, n, spec, evaluation):
"Subsets[list_?AtomQ, Pattern[n,_List|All|DirectedInfinity[1]], spec_]"

evaluation.message(
"Subsets", "normal", Expression(SymbolSubsets, list, n, spec)
"Subsets", "normal", Integer1, Expression(SymbolSubsets, list, n, spec)
)


Expand Down
3 changes: 0 additions & 3 deletions mathics/builtin/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@ class Infix(Builtin):
= a + b - c
"""

messages = {
"normal": "Nonatomic expression expected at position `1`",
}
summary_text = "infix form"


Expand Down
12 changes: 7 additions & 5 deletions mathics/builtin/list/constructing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Optional, Tuple

from mathics.builtin.box.layout import RowBox
from mathics.core.atoms import Integer, is_integer_rational_or_real
from mathics.core.atoms import Integer, Integer1, is_integer_rational_or_real
from mathics.core.attributes import A_HOLD_FIRST, A_LISTABLE, A_LOCKED, A_PROTECTED
from mathics.core.builtin import BasePattern, Builtin, IterationFunction
from mathics.core.convert.expression import to_expression
Expand All @@ -23,7 +23,7 @@
from mathics.core.expression import Expression, structure
from mathics.core.list import ListExpression
from mathics.core.symbols import Atom, Symbol
from mathics.core.systemsymbols import SymbolNormal
from mathics.core.systemsymbols import SymbolNormal, SymbolTuples
from mathics.eval.lists import get_tuples, list_boxes


Expand Down Expand Up @@ -607,7 +607,7 @@ def eval_n(self, expr, n: Integer, evaluation: Evaluation):
"Tuples[expr_, n_Integer]"

if isinstance(expr, Atom):
evaluation.message("Tuples", "normal")
evaluation.message("Tuples", "normal", Integer1, Expression(expr, n))
return
py_n = n.value
if py_n is None or py_n < 0:
Expand All @@ -633,10 +633,12 @@ def eval_lists(self, exprs, evaluation: Evaluation):

exprs = exprs.get_sequence()
items = []
for expr in exprs:
for i, expr in enumerate(exprs):
evaluation.check_stopped()
if isinstance(expr, Atom):
evaluation.message("Tuples", "normal")
evaluation.message(
"Tuples", "normal", Integer(i + 1), Expression(SymbolTuples)
)
return
items.append(expr.elements)

Expand Down
Loading

0 comments on commit 2ae6b7d

Please sign in to comment.