Skip to content

Commit

Permalink
DictOfNamedArrays: better repr, keys (inducer#485)
Browse files Browse the repository at this point in the history
* DictOfNamedArrays: better repr, keys

* add tags to repr

* make keys() abstract

* add to doc

* fix lint

* remove invalid requirements.txt pytools version specification

---------

Co-authored-by: Andreas Klöckner <[email protected]>
  • Loading branch information
matthiasdiener and inducer authored Nov 25, 2024
1 parent bdb4aca commit 360ff9c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
23 changes: 21 additions & 2 deletions pytato/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@
import operator
import re
from abc import ABC, abstractmethod
from collections.abc import Callable, Collection, Iterable, Iterator, Mapping, Sequence
from collections.abc import (
Callable,
Collection,
Iterable,
Iterator,
KeysView,
Mapping,
Sequence,
)
from functools import cached_property, partialmethod
from sys import intern
from typing import (
Expand Down Expand Up @@ -923,6 +931,7 @@ class AbstractResultWithNamedArrays(Mapping[str, NamedArray], Taggable, ABC):
.. automethod:: __contains__
.. automethod:: __getitem__
.. automethod:: __len__
.. automethod:: keys
.. note::
Expand Down Expand Up @@ -961,6 +970,11 @@ def __eq__(self, other: Any) -> bool:
from pytato.equality import EqualityComparer
return EqualityComparer()(self, other)

@abstractmethod
def keys(self) -> KeysView[str]:
"""Return a :class:`KeysView` of the names of the named arrays."""
pass


@dataclasses.dataclass(frozen=True, eq=False, init=False)
class DictOfNamedArrays(AbstractResultWithNamedArrays):
Expand Down Expand Up @@ -1009,7 +1023,12 @@ def __iter__(self) -> Iterator[str]:
return iter(self._data)

def __repr__(self) -> str:
return "DictOfNamedArrays(" + str(self._data) + ")"
return f"DictOfNamedArrays(tags={self.tags!r}, data={self._data!r})"

# Note: items() and values() are not implemented here, they go through
# __iter__()/__getitem__() above.
def keys(self) -> KeysView[str]:
return self._data.keys()

# }}}

Expand Down
12 changes: 11 additions & 1 deletion pytato/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@
import dataclasses
import enum
import re
from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping
from collections.abc import (
Callable,
Hashable,
Iterable,
Iterator,
KeysView,
Mapping,
)
from functools import cached_property
from typing import (
Any,
Expand Down Expand Up @@ -339,6 +346,9 @@ def __len__(self) -> int:
def _with_new_tags(self: Call, tags: frozenset[Tag]) -> Call:
return dataclasses.replace(self, tags=tags)

def keys(self) -> KeysView[str]:
return self.function.returns.keys()

# }}}


Expand Down
4 changes: 4 additions & 0 deletions pytato/loopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def __len__(self) -> int:
def __iter__(self) -> Iterator[str]:
return iter(self._result_names)

# type-ignore-reason: AbstractResultWithNamedArrays returns a KeysView here
def keys(self) -> frozenset[str]: # type: ignore[override]
return self._result_names


@array_dataclass()
# https://github.com/python/mypy/issues/18115
Expand Down
2 changes: 1 addition & 1 deletion pytato/scalar_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.. class:: Expression
See :attr:`pymbolic.typing.Expression`.
See :data:`pymbolic.typing.Expression`.
"""

# FIXME: Unclear why the direct links to pymbolic don't work
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
git+https://github.com/inducer/pytools.git#egg=pytools >= 2024.1.14
git+https://github.com/inducer/pytools.git#egg=pytools
git+https://github.com/inducer/pymbolic.git#egg=pymbolic
git+https://github.com/inducer/genpy.git#egg=genpy
git+https://github.com/inducer/loopy.git#egg=loopy
Expand Down

0 comments on commit 360ff9c

Please sign in to comment.