Skip to content

Commit

Permalink
Version 5.2.0 (#175)
Browse files Browse the repository at this point in the history
* Adding checks for frozen boxes to `pop`, `popitem` and `clear` (thanks to Varun Madiath)
* Fixing requirements-test.txt (thanks to Fabian Affolter)
* Fixing Flake8 conflicts with black (thanks to Varun Madiath)
* Fixing coveralls update (thanks to Varun Madiath)

Co-authored-by: Varun Madiath <vamega@gmail.com>
Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch>
  • Loading branch information
3 people authored Oct 29, 2020
1 parent 4b66113 commit 4a50512
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
# stop the build if there are Python syntax errors, undefined names or print statements
flake8 box --count --select=E9,F63,F7,F82,T001,T002,T003,T004 --show-source --statistics
# exit-zero treats all errors as warnings.
flake8 . --count --exit-zero --max-complexity=20 --max-line-length=120 --statistics
flake8 . --count --exit-zero --max-complexity=20 --max-line-length=120 --statistics --extend-ignore E203
- name: Run mypy
run: mypy box
- name: Check distrubiton log description
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
pip install coveralls
- name: Test with pytest
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pytest --cov=box test/
coveralls
Expand Down
2 changes: 2 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Code contributions:
- Jonas Irgens Kylling (jkylling)
- Bruno Rocha (rochacbruno)
- Noam Graetz (NoamGraetz2)
- Fabian Affolter (fabaff)
- Varun Madiath (vamega)

Suggestions and bug reporting:

Expand Down
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

Version 5.2.0
-------------

* Adding checks for frozen boxes to `pop`, `popitem` and `clear` (thanks to Varun Madiath)
* Fixing requirements-test.txt (thanks to Fabian Affolter)
* Fixing Flake8 conflicts with black (thanks to Varun Madiath)
* Fixing coveralls update (thanks to Varun Madiath)

Version 5.1.1
-------------

Expand Down
12 changes: 11 additions & 1 deletion box/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
# -*- coding: utf-8 -*-

__author__ = "Chris Griffith"
__version__ = "5.1.1"
__version__ = "5.2.0"

from box.box import Box
from box.box_list import BoxList
from box.config_box import ConfigBox
from box.exceptions import BoxError, BoxKeyError
from box.from_file import box_from_file
from box.shorthand_box import SBox

__all__ = [
"Box",
"BoxList",
"ConfigBox",
"BoxError",
"BoxKeyError",
"box_from_file",
"SBox",
]
27 changes: 20 additions & 7 deletions box/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,41 +229,41 @@ def __init__(

def __add__(self, other: dict):
if not isinstance(other, dict):
raise BoxTypeError(f"Box can only merge two boxes or a box and a dictionary.")
raise BoxTypeError("Box can only merge two boxes or a box and a dictionary.")
new_box = self.copy()
new_box.merge_update(other)
return new_box

def __radd__(self, other: dict):
if not isinstance(other, dict):
raise BoxTypeError(f"Box can only merge two boxes or a box and a dictionary.")
raise BoxTypeError("Box can only merge two boxes or a box and a dictionary.")
new_box = self.copy()
new_box.merge_update(other)
return new_box

def __iadd__(self, other: dict):
if not isinstance(other, dict):
raise BoxTypeError(f"Box can only merge two boxes or a box and a dictionary.")
raise BoxTypeError("Box can only merge two boxes or a box and a dictionary.")
self.merge_update(other)
return self

def __or__(self, other: dict):
if not isinstance(other, dict):
raise BoxTypeError(f"Box can only merge two boxes or a box and a dictionary.")
raise BoxTypeError("Box can only merge two boxes or a box and a dictionary.")
new_box = self.copy()
new_box.update(other)
return new_box

def __ror__(self, other: dict):
if not isinstance(other, dict):
raise BoxTypeError(f"Box can only merge two boxes or a box and a dictionary.")
raise BoxTypeError("Box can only merge two boxes or a box and a dictionary.")
new_box = self.copy()
new_box.update(other)
return new_box

def __ior__(self, other: dict):
if not isinstance(other, dict):
raise BoxTypeError(f"Box can only merge two boxes or a box and a dictionary.")
raise BoxTypeError("Box can only merge two boxes or a box and a dictionary.")
self.update(other)
return self

Expand Down Expand Up @@ -561,6 +561,9 @@ def __delattr__(self, item):
raise BoxKeyError(str(err)) from None

def pop(self, key, *args):
if self._box_config["frozen_box"]:
raise BoxError("Box is frozen")

if args:
if len(args) != 1:
raise BoxError('pop() takes only one optional argument "default"')
Expand All @@ -580,6 +583,8 @@ def pop(self, key, *args):
return item

def clear(self):
if self._box_config["frozen_box"]:
raise BoxError("Box is frozen")
super().clear()
self._box_config["__safe_keys"].clear()

Expand Down Expand Up @@ -621,6 +626,9 @@ def to_dict(self) -> Dict:
return out_dict

def update(self, __m=None, **kwargs):
if self._box_config["frozen_box"]:
raise BoxError("Box is frozen")

if __m:
if hasattr(__m, "keys"):
for k in __m:
Expand Down Expand Up @@ -913,7 +921,12 @@ def to_msgpack(self, filename: Union[str, PathLike] = None, **kwargs):
return _to_msgpack(self.to_dict(), filename=filename, **kwargs)

@classmethod
def from_msgpack(cls, msgpack_bytes: bytes = None, filename: Union[str, PathLike] = None, **kwargs,) -> "Box":
def from_msgpack(
cls,
msgpack_bytes: bytes = None,
filename: Union[str, PathLike] = None,
**kwargs,
) -> "Box":
"""
Transforms msgpack bytes or file into a Box object
Expand Down
1 change: 0 additions & 1 deletion box/box_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import copy
import re
from os import PathLike
from pathlib import Path
from typing import Iterable, Type, Union

import box
Expand Down
3 changes: 2 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage>=5.0.4
msgpack>=1.0
pytest-cov>=2.8.1
pytest>=5.4.1
reusables>=0.9.5
ruamel.yaml>=0.16
wheel>=0.34.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

# Must import multiprocessing as a fix for issues with testing, experienced on win10
import multiprocessing
import multiprocessing # noqa: F401
import os
import re

Expand Down
29 changes: 23 additions & 6 deletions test/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,17 @@ def test_from_yaml(self):
assert bx.Key_2 == Box()

def test_bad_from_json(self):
with pytest.raises(BoxError) as err:
with pytest.raises(BoxError):
Box.from_json()

with pytest.raises(BoxError) as err2:
with pytest.raises(BoxError):
Box.from_json(json_string="[1]")

def test_bad_from_yaml(self):
with pytest.raises(BoxError) as err:
with pytest.raises(BoxError):
Box.from_yaml()

with pytest.raises(BoxError) as err2:
with pytest.raises(BoxError):
Box.from_yaml("lol")

def test_conversion_box(self):
Expand Down Expand Up @@ -370,6 +370,21 @@ def test_frozen(self):
with pytest.raises(TypeError):
hash(bx)

with pytest.raises(BoxError):
bx.clear()

with pytest.raises(BoxError):
bx.pop("alist")

with pytest.raises(BoxError):
bx.popitem()

with pytest.raises(BoxError):
bx.popitem()

with pytest.raises(BoxError):
bx.update({"another_list": []})

bx2 = Box(test_dict)
with pytest.raises(TypeError):
hash(bx2)
Expand Down Expand Up @@ -567,7 +582,7 @@ def test_from_multiline(self):
assert a[1].b == 3

def test_duplicate_errors(self):
with pytest.raises(BoxError) as err:
with pytest.raises(BoxError):
Box({"?a": 1, "!a": 3}, box_duplicates="error")

Box({"?a": 1, "!a": 3}, box_duplicates="ignore")
Expand Down Expand Up @@ -755,7 +770,9 @@ class ConfigBox2(ConfigBox):

def test_inheritance(self):
data = {
"users": [{"users": [{"name": "B"}]},],
"users": [
{"users": [{"name": "B"}]},
],
}

class UsersBoxList(BoxList):
Expand Down

0 comments on commit 4a50512

Please sign in to comment.