Skip to content

Commit

Permalink
Merge pull request #244 from spacether/feat_adds_covariance
Browse files Browse the repository at this point in the history
Adds covariant true to key type
  • Loading branch information
corenting authored Jul 20, 2023
2 parents e2d2aaa + 4141169 commit 33beaef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions immutabledict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__version__ = "2.2.5"

_K = TypeVar("_K")
_V = TypeVar("_V")
_V = TypeVar("_V", covariant=True)


class immutabledict(Mapping[_K, _V]):
Expand Down Expand Up @@ -35,8 +35,8 @@ def __getitem__(self, key: _K) -> _V:
def __contains__(self, key: object) -> bool:
return key in self._dict

def copy(self, **add_or_replace: _V) -> immutabledict[_K, _V]:
return self.__class__(self, **add_or_replace)
def copy(self) -> immutabledict[_K, _V]:
return self.__class__(self)

def __iter__(self) -> Iterator[_K]:
return iter(self._dict)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_immutabledict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@


class TestImmutableDict:
def test_covariance(self):
assert immutabledict.__parameters__[0].__covariant__ is False
assert immutabledict.__parameters__[1].__covariant__ is True

def test_cannot_assign_value(self):
with pytest.raises(AttributeError):
immutabledict().setitem("key", "value")
Expand Down Expand Up @@ -41,6 +45,7 @@ def test_copy(self):
original = immutabledict({"a": "value"})
copy = original.copy()
assert original == copy
assert id(original) != id(copy)

def test_iter(self):
immutable_dict = immutabledict({"a": "value", "b": "other_value"})
Expand Down

0 comments on commit 33beaef

Please sign in to comment.