Skip to content

Commit

Permalink
refactor(common): remove ibis.collections.DotDict
Browse files Browse the repository at this point in the history
The hybrid getitem/getattr access to the validation context is not
necessary since the move from the previous rule-based validation
to the current pattern matching system.
  • Loading branch information
kszucs authored and cpcloud committed Aug 7, 2023
1 parent fe9a289 commit fedd4b1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 66 deletions.
7 changes: 3 additions & 4 deletions ibis/common/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import inspect
from typing import Any as AnyType

from ibis.common.collections import DotDict
from ibis.common.patterns import (
Any,
FrozenDictOf,
Expand Down Expand Up @@ -326,7 +325,7 @@ def unbind(self, this: AnyType):
# does the reverse of bind, but doesn't apply defaults
args, kwargs = [], {}
for name, param in self.parameters.items():
value = getattr(this, name)
value = this[name]
if param.kind is POSITIONAL_OR_KEYWORD:
args.append(value)
elif param.kind is VAR_POSITIONAL:
Expand Down Expand Up @@ -362,7 +361,7 @@ def validate(self, *args, **kwargs):
bound = self.bind(*args, **kwargs)
bound.apply_defaults()

this = DotDict()
this = {}
for name, value in bound.arguments.items():
param = self.parameters[name]
# TODO(kszucs): provide more error context on failure
Expand All @@ -372,7 +371,7 @@ def validate(self, *args, **kwargs):

def validate_nobind(self, **kwargs):
"""Validate the arguments against the signature without binding."""
this = DotDict()
this = {}
for name, param in self.parameters.items():
value = kwargs.get(name, param.default)
if value is EMPTY:
Expand Down
41 changes: 1 addition & 40 deletions ibis/common/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,45 +173,6 @@ def __hash__(self):
return self.__precomputed_hash__


@public
class DotDict(dict):
"""Dictionary that allows access to keys as attributes using the dot notation.
Note, that this is not recursive, so nested access is not supported.
Examples
--------
>>> d = DotDict({'a': 1, 'b': 2})
>>> d.a
1
>>> d.b
2
>>> d['a']
1
>>> d['b']
2
>>> d.c = 3
>>> d['c']
3
>>> d.c
3
"""

__slots__ = ()

__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__

def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError(key)

def __repr__(self):
return f"{self.__class__.__name__}({super().__repr__()})"


class RewindableIterator(Iterator):
"""Iterator that can be rewound to a checkpoint.
Expand Down Expand Up @@ -254,4 +215,4 @@ def checkpoint(self):
self._iterator, self._checkpoint = tee(self._iterator)


public(frozendict=FrozenDict, dotdict=DotDict)
public(frozendict=FrozenDict)
23 changes: 1 addition & 22 deletions ibis/common/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from ibis.common.collections import DotDict, FrozenDict, MapSet, RewindableIterator
from ibis.common.collections import FrozenDict, MapSet, RewindableIterator
from ibis.tests.util import assert_pickle_roundtrip


Expand Down Expand Up @@ -179,27 +179,6 @@ def test_mapset_set_api():
assert (f ^ a).identical(MySchema(d=4, e=5, a=1, b=2))


def test_dotdict():
d = DotDict({"a": 1, "b": 2, "c": 3})
assert d["a"] == d.a == 1
assert d["b"] == d.b == 2

d.b = 3
assert d.b == 3
assert d["b"] == 3

del d.c
assert not hasattr(d, "c")
assert "c" not in d

assert repr(d) == "DotDict({'a': 1, 'b': 3})"

with pytest.raises(KeyError):
assert d["x"]
with pytest.raises(AttributeError):
assert d.x


def test_frozendict():
d = FrozenDict({"a": 1, "b": 2, "c": 3})
e = FrozenDict(a=1, b=2, c=3)
Expand Down

0 comments on commit fedd4b1

Please sign in to comment.