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

fix sourcery's suggestions #22

Merged
merged 3 commits into from
Jun 7, 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
28 changes: 26 additions & 2 deletions tests/test_udict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@

class UDictTestCase(unittest.TestCase):
def test_init(self):
d = UDict(hello=1, hi=2, default=10)
d2 = UDict({'hello': 1, 'hi': 2})
d = UDict(hello=1, hi='world', default=10)
d2 = UDict({'hello': 1, 'hi': 'world'})
d3 = UDict()
bleudev marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(d.default, 10)
self.assertEqual(d2.default, None)
self.assertEqual(d3.default, None)

self.assertDictEqual(d.dictionary, d2.dictionary)
self.assertDictEqual(d3.dictionary, {})

self.assertEqual(d, d2)
self.assertNotEqual(d, d3)
self.assertNotEqual(d2, d3)

def test_keys_values_items(self):
d = UDict(hello=1, hi=2)
Expand Down Expand Up @@ -63,6 +71,9 @@ def test_get_item(self):

self.assertEqual(d[1], d['hello'])

# not existent key
self.assertEqual(d['hey'], None)
bleudev marked this conversation as resolved.
Show resolved Hide resolved

def test_set_item(self):
d = UDict(hello=1, hi=2)
d['hi'] = 3
Expand Down Expand Up @@ -96,6 +107,19 @@ def test_get(self):
self.assertEqual(d.get(value=3, default='missing'), 'missing')
self.assertEqual(d.get(value=3), None)

with self.assertRaises(ValueError):
bleudev marked this conversation as resolved.
Show resolved Hide resolved
d.get()
with self.assertRaises(ValueError):
d.get(index=2, value=1)
with self.assertRaises(ValueError):
d.get(index=2, key=2)
with self.assertRaises(ValueError):
d.get(value=1, key=4)
with self.assertRaises(ValueError):
d.get(value=1, key=4, index=3)
with self.assertRaises(IndexError):
d.get(index=4)

def test_len_and_iter(self):
d = UDict(hello=1, hi=2)
self.assertEqual(len(d), 2)
Expand Down
12 changes: 7 additions & 5 deletions ufpy/udict.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, *, default: CDV, **kwargs: VT): ...
def __init__(self, dictionary: AnyDict[KT, VT] = None, *, default: CDV = None, **kwargs: VT):
if isinstance(dictionary, UDict):
dictionary = dictionary.dictionary
self.__dict = dictionary if dictionary else kwargs
self.__dict = dictionary or kwargs
bleudev marked this conversation as resolved.
Show resolved Hide resolved
self.__default = default

# dictionary
Expand Down Expand Up @@ -145,7 +145,9 @@ def __getitem__(self, key: KT | int | slice) -> UDict[KT, VT, DV] | VT:

def __setitem__(self, key: KT | int | slice, value: VT | list[VT]) -> None:
keys = self.__get_keys_from_slice_or_int(key)
values = [value] if not isinstance(value, (list, tuple)) else value

# Ensure 'values' is always a list for consistent processing
values = value if isinstance(value, (list, tuple)) else [value]
bleudev marked this conversation as resolved.
Show resolved Hide resolved

if len(keys) > len(values):
values.extend([values[-1] for _ in range(len(keys) - len(values) + 1)])
Expand Down Expand Up @@ -222,16 +224,16 @@ def get(

# Len, iterator and reversed version
def __len__(self) -> int:
return len(self.items)
return len(self.__dict)

def __iter__(self) -> Iterator[tuple[KT, VT]]:
return iter(self.items)
return iter(self.__dict.items())
bleudev marked this conversation as resolved.
Show resolved Hide resolved

# Booleans
def is_empty(self) -> bool:
return len(self) == 0

def __nonzero__(self) -> bool:
def __bool__(self) -> bool:
return not self.is_empty()

def __contains__(self, item: tuple[KT, VT] | list[KT | VT] | KT) -> bool:
Expand Down
Loading