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 collections.ControlledDict handling of Iterators via update method, fix Python version in test workflow and recover Windows in CI #732

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ jobs:
fail-fast: false
max-parallel: 20
matrix:
os: [ubuntu-latest, macos-14] #, windows-latest]
python-version: ["3.10", "3.12"]
os: [ubuntu-latest, macos-14, windows-latest]
python-version: ["3.10", "3.11", "3.12", "3.13"]

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

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ maintainers = [
]
description = "Monty is the missing complement to Python."
readme = "README.md"
requires-python = ">=3.10"
requires-python = ">=3.10,<3.14"
classifiers = [
"Programming Language :: Python :: 3",
"Development Status :: 4 - Beta",
Expand Down
6 changes: 4 additions & 2 deletions src/monty/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def __setitem__(self, key, value) -> None:

def update(self, *args, **kwargs) -> None:
"""Forbid adding or updating keys based on _allow_add and _allow_update."""
for key in dict(*args, **kwargs):

updates = dict(*args, **kwargs)
for key in updates:
if key not in self.data and not self._allow_add:
raise TypeError(
f"Cannot add new key {key!r} using update, because add is disabled."
Expand All @@ -99,7 +101,7 @@ def update(self, *args, **kwargs) -> None:
f"Cannot update key {key!r} using update, because update is disabled."
)

super().update(*args, **kwargs)
super().update(updates)

def setdefault(self, key, default=None) -> Any:
"""Forbid adding or updating keys based on _allow_add and _allow_update.
Expand Down
2 changes: 1 addition & 1 deletion src/monty/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def zopen(
# TODO: remove default value of `mode` to force user to give one after deadline
if mode is None:
warnings.warn(
"We strongly discourage using a default `mode`, it would be"
"We strongly discourage using a default `mode`, it would be "
f"set to `r` now but would not be allowed after {_deadline}",
FutureWarning,
stacklevel=2,
Expand Down
15 changes: 14 additions & 1 deletion tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def test_update_allowed(self):
dct.update({"a": 3})
assert dct["a"] == 3

# Test Iterator handling
dct.update(zip(["c", "d"], [11, 12]))
assert dct["c"] == 11

dct.setdefault("a", 4) # existing key
assert dct["a"] == 3

Expand Down Expand Up @@ -122,6 +126,11 @@ def test_frozen_like(self):
assert not dct._allow_add
assert not dct._allow_update

def test_iterator_handling(self):
"""Make sure iterators are handling correctly."""
c_dict = ControlledDict(zip(["c", "d"], [11, 12]))
assert c_dict["c"] == 11


def test_frozendict():
dct = frozendict({"hello": "world"})
Expand Down Expand Up @@ -157,7 +166,11 @@ def test_namespace_dict():
dct["hello"] = "world"
assert dct["key"] == "val"

# Test update (not allowed)
# Test use `update` to add new values
dct.update({"new_key": "new_value"})
assert dct["new_key"] == "new_value"

# Test add (not allowed)
with pytest.raises(TypeError, match="update is disabled"):
dct["key"] = "val"

Expand Down
1 change: 1 addition & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def test_lzw_files(self):

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