Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various typing/dataclasses fixes #151

Merged
merged 7 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@
"sympy": ("https://docs.sympy.org/dev/", None),
"typing_extensions":
("https://typing-extensions.readthedocs.io/en/latest/", None),
"immutabledict":
("https://immutabledict.corenting.fr/", None)
}
autodoc_type_aliases = {
"ExpressionT": "ExpressionT",
inducer marked this conversation as resolved.
Show resolved Hide resolved
"ArithmeticExpressionT": "ArithmeticExpressionT",
}
1 change: 1 addition & 0 deletions doc/primitives.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Primitives (Basic Objects)
==========================

.. automodule:: pymbolic.typing
.. automodule:: pymbolic.primitives

.. vim: sw=4
33 changes: 23 additions & 10 deletions pymbolic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@

from .polynomial import Polynomial

from .primitives import Variable as var # noqa: N813
from .primitives import variables
from .primitives import flattened_sum
from .primitives import subscript
from .primitives import flattened_product
from .primitives import quotient
from .primitives import linear_combination
from .primitives import make_common_subexpression as cse
from .primitives import make_sym_vector
from .primitives import disable_subscript_by_getitem
from .primitives import (Variable as var, # noqa: N813
Variable,
Expression,
variables,
flattened_sum,
subscript,
flattened_product,
quotient,
linear_combination,
make_common_subexpression as cse,
make_sym_vector,
disable_subscript_by_getitem,
expr_dataclass,
)
from .parser import parse
from .mapper.evaluator import evaluate
from .mapper.evaluator import evaluate_kw
Expand All @@ -60,10 +64,18 @@
from .mapper.distributor import distribute as expand
from .mapper.distributor import distribute
from .mapper.flattener import flatten
from .typing import NumberT, ScalarT, ArithmeticExpressionT, ExpressionT, BoolT


__all__ = (
"ArithmeticExpressionT",
"BoolT",
"Expression",
"ExpressionT",
"NumberT",
"Polynomial",
"ScalarT",
"Variable",
"compile",
"compiler",
"cse",
Expand All @@ -78,6 +90,7 @@
"evaluate_kw",
"evaluator",
"expand",
"expr_dataclass",
"flatten",
"flattened_product",
"flattened_sum",
Expand Down
12 changes: 6 additions & 6 deletions pymbolic/mapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
.. rubric:: Handling objects that don't declare mapper methods

In particular, this includes many non-subclasses of
:class:`pymbolic.primitives.Expression`.
:class:`pymbolic.Expression`.

.. automethod:: map_foreign

Expand Down Expand Up @@ -113,16 +113,16 @@ class UnsupportedExpressionError(ValueError):
# {{{ mapper base

class Mapper:
"""A visitor for trees of :class:`pymbolic.primitives.Expression`
"""A visitor for trees of :class:`pymbolic.Expression`
subclasses. Each expression-derived object is dispatched to the
method named by the :attr:`pymbolic.primitives.Expression.mapper_method`
method named by the :attr:`pymbolic.Expression.mapper_method`
attribute and if not found, the methods named by the class attribute
*mapper_method* in the method resolution order of the object.
"""

def handle_unsupported_expression(self, expr, *args, **kwargs):
"""Mapper method that is invoked for
:class:`pymbolic.primitives.Expression` subclasses for which a mapper
:class:`pymbolic.Expression` subclasses for which a mapper
method does not exist in this mapper.
"""

Expand Down Expand Up @@ -496,9 +496,9 @@ def map_call_with_kwargs(self, expr, *args, **kwargs):
parameters = tuple([
self.rec(child, *args, **kwargs) for child in expr.parameters
])
kw_parameters = {
kw_parameters = immutabledict({
key: self.rec(val, *args, **kwargs)
for key, val in expr.kw_parameters.items()}
for key, val in expr.kw_parameters.items()})
alexfikl marked this conversation as resolved.
Show resolved Hide resolved

if (function is expr.function
and all(child is orig_child for child, orig_child in
Expand Down
6 changes: 3 additions & 3 deletions pymbolic/mapper/stringifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
class StringifyMapper(Mapper):
"""A mapper to turn an expression tree into a string.

:class:`pymbolic.primitives.Expression.__str__` is often implemented using
:class:`pymbolic.Expression.__str__` is often implemented using
this mapper.

When it encounters an unsupported :class:`pymbolic.primitives.Expression`
subclass, it calls its :meth:`pymbolic.primitives.Expression.make_stringifier`
When it encounters an unsupported :class:`pymbolic.Expression`
subclass, it calls its :meth:`pymbolic.Expression.make_stringifier`
method to get a :class:`StringifyMapper` that potentially does.
"""

Expand Down
Loading
Loading