Skip to content

Commit b5dcadf

Browse files
authored
Merge pull request #732 from DanielYang59/fix-namespace-dict-with-iterator
Fix `collections.ControlledDict` handling of Iterators via `update` method, fix Python version in test workflow and recover Windows in CI
2 parents 30d94f9 + ffd142e commit b5dcadf

File tree

6 files changed

+25
-9
lines changed

6 files changed

+25
-9
lines changed

.github/workflows/test.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ jobs:
88
fail-fast: false
99
max-parallel: 20
1010
matrix:
11-
os: [ubuntu-latest, macos-14] #, windows-latest]
12-
python-version: ["3.10", "3.12"]
11+
os: [ubuntu-latest, macos-14, windows-latest]
12+
python-version: ["3.10", "3.11", "3.12", "3.13"]
1313

1414
runs-on: ${{ matrix.os }}
1515

1616
steps:
1717
- uses: actions/checkout@v4
1818

19-
- name: Set up Python ${{ matrix.python }}
19+
- name: Set up Python ${{ matrix.python-version }}
2020
uses: actions/setup-python@v5
2121
with:
22-
python-version: ${{ matrix.python }}
22+
python-version: ${{ matrix.python-version }}
2323

2424
- name: Install dependencies
2525
run: |

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ maintainers = [
99
]
1010
description = "Monty is the missing complement to Python."
1111
readme = "README.md"
12-
requires-python = ">=3.10"
12+
requires-python = ">=3.10,<3.14"
1313
classifiers = [
1414
"Programming Language :: Python :: 3",
1515
"Development Status :: 4 - Beta",

src/monty/collections.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ def __setitem__(self, key, value) -> None:
8989

9090
def update(self, *args, **kwargs) -> None:
9191
"""Forbid adding or updating keys based on _allow_add and _allow_update."""
92-
for key in dict(*args, **kwargs):
92+
93+
updates = dict(*args, **kwargs)
94+
for key in updates:
9395
if key not in self.data and not self._allow_add:
9496
raise TypeError(
9597
f"Cannot add new key {key!r} using update, because add is disabled."
@@ -99,7 +101,7 @@ def update(self, *args, **kwargs) -> None:
99101
f"Cannot update key {key!r} using update, because update is disabled."
100102
)
101103

102-
super().update(*args, **kwargs)
104+
super().update(updates)
103105

104106
def setdefault(self, key, default=None) -> Any:
105107
"""Forbid adding or updating keys based on _allow_add and _allow_update.

src/monty/io.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def zopen(
6161
# TODO: remove default value of `mode` to force user to give one after deadline
6262
if mode is None:
6363
warnings.warn(
64-
"We strongly discourage using a default `mode`, it would be"
64+
"We strongly discourage using a default `mode`, it would be "
6565
f"set to `r` now but would not be allowed after {_deadline}",
6666
FutureWarning,
6767
stacklevel=2,

tests/test_collections.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def test_update_allowed(self):
6262
dct.update({"a": 3})
6363
assert dct["a"] == 3
6464

65+
# Test Iterator handling
66+
dct.update(zip(["c", "d"], [11, 12]))
67+
assert dct["c"] == 11
68+
6569
dct.setdefault("a", 4) # existing key
6670
assert dct["a"] == 3
6771

@@ -122,6 +126,11 @@ def test_frozen_like(self):
122126
assert not dct._allow_add
123127
assert not dct._allow_update
124128

129+
def test_iterator_handling(self):
130+
"""Make sure iterators are handling correctly."""
131+
c_dict = ControlledDict(zip(["c", "d"], [11, 12]))
132+
assert c_dict["c"] == 11
133+
125134

126135
def test_frozendict():
127136
dct = frozendict({"hello": "world"})
@@ -157,7 +166,11 @@ def test_namespace_dict():
157166
dct["hello"] = "world"
158167
assert dct["key"] == "val"
159168

160-
# Test update (not allowed)
169+
# Test use `update` to add new values
170+
dct.update({"new_key": "new_value"})
171+
assert dct["new_key"] == "new_value"
172+
173+
# Test add (not allowed)
161174
with pytest.raises(TypeError, match="update is disabled"):
162175
dct["key"] = "val"
163176

tests/test_io.py

+1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def test_lzw_files(self):
426426

427427
# Cannot decompress a real LZW file
428428
with (
429+
pytest.warns(FutureWarning, match="compress LZW-compressed files"),
429430
pytest.raises(gzip.BadGzipFile, match="Not a gzipped file"),
430431
zopen(f"{TEST_DIR}/real_lzw_file.txt.Z", "rt", encoding="utf-8") as f,
431432
):

0 commit comments

Comments
 (0)