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

Stop using asdict to handle dataclasses #2561

Merged
merged 1 commit into from
Oct 28, 2021
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
16 changes: 11 additions & 5 deletions mars/services/meta/store/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
from collections import defaultdict
from dataclasses import asdict
from dataclasses import fields as dataclass_fields
from typing import Dict, List, Set

from .... import oscar as mo
Expand All @@ -23,6 +24,11 @@
from .base import AbstractMetaStore, register_meta_store


@functools.lru_cache(100)
def _get_meta_fields(meta_cls):
return [f.name for f in dataclass_fields(meta_cls)]


@register_meta_store
class DictMetaStore(AbstractMetaStore):
name = "dict"
Expand Down Expand Up @@ -63,10 +69,10 @@ def _get_meta(
if error not in ("raise", "ignore"): # pragma: no cover
raise ValueError("error must be raise or ignore")
try:
meta = asdict(self._store[object_id])
if fields:
return {k: meta[k] for k in fields}
return meta
meta = self._store[object_id]
if fields is None:
fields = _get_meta_fields(type(meta))
return {k: getattr(meta, k) for k in fields}
except KeyError:
if error == "raise":
raise
Expand Down
6 changes: 6 additions & 0 deletions mars/services/meta/store/tests/test_meta_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ async def test_mock_meta_store():
),
)

meta = await meta_store.get_meta(t.key)
assert meta["shape"] == t.shape
assert meta["order"] == t.order
assert meta["dtype"] == t.dtype

meta = await meta_store.get_meta(t.key, fields=["shape", "order"])
assert meta["shape"] == t.shape
assert meta["order"] == t.order
assert "dtype" not in meta

await meta_store.del_meta(t.key)

Expand Down