Skip to content

Commit

Permalink
Removing false flagging of assert statements from tests. (pybamm-team…
Browse files Browse the repository at this point in the history
…#4236)

* Removing flase flagging of assert statements from tests

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>

* style: pre-commit fixes

* Using ruff to check for asserts

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>

* Using TypeError instead of assert

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>

* Adding back bandit file

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>

* style: pre-commit fixes

* Adding tests

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>

---------

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 19, 2024
1 parent 27c29df commit ed412f8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bandit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# To ignore false flagging of assert statements in tests by Codacy.
skips: ['B101']
3 changes: 2 additions & 1 deletion pybamm/expression_tree/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ def name(self):

@name.setter
def name(self, value: str):
assert isinstance(value, str)
if not isinstance(value, str):
raise TypeError(f"{value} must be of type str")
self._name = value

@property
Expand Down
10 changes: 6 additions & 4 deletions pybamm/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ def debug_mode(self):
return self._debug_mode

@debug_mode.setter
def debug_mode(self, value):
assert isinstance(value, bool)
def debug_mode(self, value: bool):
if not isinstance(value, bool):
raise TypeError(f"{value} must be of type bool")
self._debug_mode = value

@property
def simplify(self):
return self._simplify

@simplify.setter
def simplify(self, value):
assert isinstance(value, bool)
def simplify(self, value: bool):
if not isinstance(value, bool):
raise TypeError(f"{value} must be of type bool")
self._simplify = value

def set_smoothing_parameters(self, k):
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ extend-select = [
"UP", # pyupgrade
"YTT", # flake8-2020
"TID252", # relative-imports
"S101", # to identify use of assert statement
]
ignore = [
"E741", # Ambiguous variable name
Expand All @@ -220,7 +221,7 @@ ignore = [
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["T20"]
"tests/*" = ["T20", "S101"]
"docs/*" = ["T20"]
"examples/*" = ["T20"]
"**.ipynb" = ["E402", "E703"]
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_expression_tree/test_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
class TestSymbol(TestCase):
def test_symbol_init(self):
sym = pybamm.Symbol("a symbol")
with self.assertRaises(TypeError):
sym.name = 1
self.assertEqual(sym.name, "a symbol")
self.assertEqual(str(sym), "a symbol")

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

class TestSettings:
def test_simplify(self):
with pytest.raises(TypeError):
pybamm.settings.simplify = "Not Bool"

assert pybamm.settings.simplify

pybamm.settings.simplify = False
assert not pybamm.settings.simplify

pybamm.settings.simplify = True

def test_debug_mode(self):
with pytest.raises(TypeError):
pybamm.settings.debug_mode = "Not bool"

def test_smoothing_parameters(self):
assert pybamm.settings.min_max_mode == "exact"
assert pybamm.settings.heaviside_smoothing == "exact"
Expand Down

0 comments on commit ed412f8

Please sign in to comment.