diff --git a/pyproject.toml b/pyproject.toml index b1aacfe..65c53ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "literal-dict" -version = "1.0.1.1" +version = "1.1.0" description = "Use JavaScript-like object definition syntax in Python" authors = [{ name = "Muspi Merol", email = "me@muspimerol.site" }] dependencies = [] diff --git a/src/literal_dict.py b/src/literal_dict.py index 359abba..de38d77 100644 --- a/src/literal_dict.py +++ b/src/literal_dict.py @@ -8,6 +8,7 @@ class DictBuilder(Generic[D]): def __init__(self, constructor: Callable[[dict], D] = dict): self.constructor = constructor + assert callable(self.constructor), "Constructor must be callable" def __getitem__(self, args: Union[slice, T, Sequence[Union[slice, T]]]) -> D: if not isinstance(args, tuple): @@ -26,18 +27,21 @@ def __getitem__(self, args: Union[slice, T, Sequence[Union[slice, T]]]) -> D: obj[arg.start] = arg.stop else: for name, var in caller_frame.f_locals.items(): - if var is arg: + if var is arg and name not in obj: obj[name] = arg break else: for name, var in caller_frame.f_globals.items(): - if var is arg: + if var is arg and name not in obj: obj[name] = arg break else: for name, var in caller_frame.f_builtins.items(): - if var is arg: + if var is arg and name not in obj: obj[name] = arg break return self.constructor(obj) if self.constructor is not dict else obj # type: ignore + + def __repr__(self): + return f"{self.__class__.__qualname__}({self.constructor.__qualname__})" diff --git a/test.py b/test.py index 5a7ff61..88df951 100644 --- a/test.py +++ b/test.py @@ -43,3 +43,9 @@ def f(): assert d[print] == {"print": print} f() + + +def test_same_value_different_name(): + d = DictBuilder() + a = b = 1 + assert d[a, b] == {"a": 1, "b": 1}