From cea722d81fa7e0bb6b57395e7748055123136e6a Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 30 Jan 2023 02:16:36 -0500 Subject: [PATCH] Fix Ruff-reported errors manually --- mesa/__init__.py | 3 ++- mesa/main.py | 2 +- mesa/space.py | 6 +++--- mesa/visualization/UserParam.py | 11 +++++------ mesa/visualization/modules/BarChartVisualization.py | 10 +++++----- pyproject.toml | 2 ++ setup.py | 12 ++++++------ tests/test_examples.py | 4 ++-- tests/test_main.py | 2 +- tests/test_space.py | 4 ++-- tests/test_time.py | 8 ++++---- 11 files changed, 33 insertions(+), 31 deletions(-) diff --git a/mesa/__init__.py b/mesa/__init__.py index aea6e4710fd..25054b50ff1 100644 --- a/mesa/__init__.py +++ b/mesa/__init__.py @@ -26,4 +26,5 @@ __title__ = "mesa" __version__ = "1.1.1" __license__ = "Apache 2.0" -__copyright__ = f"Copyright {datetime.date.today().year} Project Mesa Team" +_this_year = datetime.datetime.now(tz=datetime.timezone.utc).date().year +__copyright__ = f"Copyright {_this_year} Project Mesa Team" diff --git a/mesa/main.py b/mesa/main.py index b59b449b46f..9d800cbb5b3 100644 --- a/mesa/main.py +++ b/mesa/main.py @@ -30,7 +30,7 @@ def runserver(project): with open("run.py") as f: code = compile(f.read(), "run.py", "exec") - exec(code, {}, {}) + exec(code, {}, {}) # noqa: S102 @click.command() diff --git a/mesa/space.py b/mesa/space.py index 2ad406242dc..4b03b1e0033 100644 --- a/mesa/space.py +++ b/mesa/space.py @@ -1019,15 +1019,15 @@ def out_of_bounds(self, pos: FloatCoordinate) -> bool: class NetworkGrid: """Network Grid where each node contains zero or more agents.""" - def __init__(self, G: Any) -> None: + def __init__(self, g: Any) -> None: """Create a new network. Args: G: a NetworkX graph instance. """ - self.G = G + self.G = g for node_id in self.G.nodes: - G.nodes[node_id]["agent"] = self.default_val() + g.nodes[node_id]["agent"] = self.default_val() @staticmethod def default_val() -> list: diff --git a/mesa/visualization/UserParam.py b/mesa/visualization/UserParam.py index 2fe3d80840c..7fd2103b993 100644 --- a/mesa/visualization/UserParam.py +++ b/mesa/visualization/UserParam.py @@ -121,12 +121,11 @@ def value(self, value): self._value = self.min_value elif self._value > self.max_value: self._value = self.max_value - elif self.param_type == self.CHOICE: - if self._value not in self.choices: - print( - "Selected choice value not in available choices, selected first choice from 'choices' list" - ) - self._value = self.choices[0] + elif (self.param_type == self.CHOICE) and self._value not in self.choices: + print( + "Selected choice value not in available choices, selected first choice from 'choices' list" + ) + self._value = self.choices[0] @property def json(self): diff --git a/mesa/visualization/modules/BarChartVisualization.py b/mesa/visualization/modules/BarChartVisualization.py index bec182bdd66..20fd5e7039c 100644 --- a/mesa/visualization/modules/BarChartVisualization.py +++ b/mesa/visualization/modules/BarChartVisualization.py @@ -75,20 +75,20 @@ def render(self, model): if self.scope == "agent": df = data_collector.get_agent_vars_dataframe().astype("float") latest_step = df.index.levels[0][-1] - labelStrings = [f["Label"] for f in self.fields] - dict = df.loc[latest_step].T.loc[labelStrings].to_dict() + label_strings = [f["Label"] for f in self.fields] + dict = df.loc[latest_step].T.loc[label_strings].to_dict() current_values = list(dict.values()) elif self.scope == "model": - outDict = {} + out_dict = {} for s in self.fields: name = s["Label"] try: val = data_collector.model_vars[name][-1] except (IndexError, KeyError): val = 0 - outDict[name] = val - current_values.append(outDict) + out_dict[name] = val + current_values.append(out_dict) else: raise ValueError("scope must be 'agent' or 'model'") return current_values diff --git a/pyproject.toml b/pyproject.toml index 483423524b1..6583f2196cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,8 @@ extend-ignore = [ "S101", # Use of `assert` detected "B017", # `assertRaises(Exception)` should be considered evil TODO "PGH004", # Use specific rule codes when using `noqa` TODO + "B905", # `zip()` without an explicit `strict=` parameter + "N802", # Function name should be lowercase ] extend-exclude = ["docs", "build"] # Hardcode to Python 3.8. diff --git a/setup.py b/setup.py index 6e2292dd824..e829de3389b 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ os.makedirs(external_dir_single, exist_ok=True) -def ensure_JS_dep(dirname, url): +def ensure_js_dep(dirname, url): dst_path = os.path.join(external_dir, dirname) if os.path.isdir(dst_path): # Do nothing if already downloaded @@ -50,7 +50,7 @@ def ensure_JS_dep(dirname, url): print("Done") -def ensure_JS_dep_single(url, out_name=None): +def ensure_js_dep_single(url, out_name=None): # Used for downloading e.g. D3.js single file if out_name is None: out_name = url.split("/")[-1] @@ -67,14 +67,14 @@ def ensure_JS_dep_single(url, out_name=None): # Ensure Bootstrap bootstrap_version = "5.1.3" -ensure_JS_dep( +ensure_js_dep( f"bootstrap-{bootstrap_version}-dist", f"https://github.com/twbs/bootstrap/releases/download/v{bootstrap_version}/bootstrap-{bootstrap_version}-dist.zip", ) # Ensure Bootstrap Slider bootstrap_slider_version = "11.0.2" -ensure_JS_dep( +ensure_js_dep( f"bootstrap-slider-{bootstrap_slider_version}", f"https://github.com/seiyria/bootstrap-slider/archive/refs/tags/v{bootstrap_slider_version}.zip", ) @@ -82,14 +82,14 @@ def ensure_JS_dep_single(url, out_name=None): # Important: when updating the D3 version, make sure to update the constant # D3_JS_FILE in mesa/visualization/ModularVisualization.py. d3_version = "7.4.3" -ensure_JS_dep_single( +ensure_js_dep_single( f"https://cdnjs.cloudflare.com/ajax/libs/d3/{d3_version}/d3.min.js", out_name=f"d3-{d3_version}.min.js", ) # Important: Make sure to update CHART_JS_FILE in # mesa/visualization/ModularVisualization.py. chartjs_version = "3.6.1" -ensure_JS_dep_single( +ensure_js_dep_single( f"https://cdn.jsdelivr.net/npm/chart.js@{chartjs_version}/dist/chart.min.js", out_name=f"chart-{chartjs_version}.min.js", ) diff --git a/tests/test_examples.py b/tests/test_examples.py index bb2c31c30f1..1c149da4b75 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -62,7 +62,7 @@ def test_examples(self): f"{example.replace('-', '_')}.server" ) server.server.render_model() - Model = getattr(mod, classcase(example)) - model = Model() + model_class = getattr(mod, classcase(example)) + model = model_class() for _ in range(10): model.step() diff --git a/tests/test_main.py b/tests/test_main.py index 730a2189619..9f7f9834626 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -24,7 +24,7 @@ def tearDown(self): "Skipping test_run, because examples folder was moved. More discussion needed." ) def test_run(self): - with patch("mesa.visualization.ModularServer") as ModularServer: + with patch("mesa.visualization.ModularServer") as ModularServer: # noqa: N806 example_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), "../examples/wolf_sheep") ) diff --git a/tests/test_space.py b/tests/test_space.py index 765c3662a4d..a7aba2529f2 100644 --- a/tests/test_space.py +++ b/tests/test_space.py @@ -337,7 +337,7 @@ def setUp(self): """ Create a test network grid and populate with Mock Agents. """ - G = nx.cycle_graph(TestSingleNetworkGrid.GRAPH_SIZE) + G = nx.cycle_graph(TestSingleNetworkGrid.GRAPH_SIZE) # noqa: N806 self.space = NetworkGrid(G) self.agents = [] for i, pos in enumerate(TEST_AGENTS_NETWORK_SINGLE): @@ -408,7 +408,7 @@ def setUp(self): """ Create a test network grid and populate with Mock Agents. """ - G = nx.complete_graph(TestMultipleNetworkGrid.GRAPH_SIZE) + G = nx.complete_graph(TestMultipleNetworkGrid.GRAPH_SIZE) # noqa: N806 self.space = NetworkGrid(G) self.agents = [] for i, pos in enumerate(TEST_AGENTS_NETWORK_MULTIPLE): diff --git a/tests/test_time.py b/tests/test_time.py index 903d3f2d207..7bf8b816b8a 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -188,7 +188,7 @@ def test_random_activation_step_steps_each_agent(self): model.step() agent_steps = [i.steps for i in model.schedule.agents] # one step for each of 2 agents - assert all(map(lambda x: x == 1, agent_steps)) + assert all(x == 1 for x in agent_steps) def test_intrastep_remove(self): """ @@ -214,8 +214,8 @@ def test_simultaneous_activation_step_steps_and_advances_each_agent(self): # one step for each of 2 agents agent_steps = [i.steps for i in model.schedule.agents] agent_advances = [i.advances for i in model.schedule.agents] - assert all(map(lambda x: x == 1, agent_steps)) - assert all(map(lambda x: x == 1, agent_advances)) + assert all(x == 1 for x in agent_steps) + assert all(x == 1 for x in agent_advances) class TestRandomActivationByType(TestCase): @@ -254,7 +254,7 @@ def test_random_activation_step_steps_each_agent(self): model.step() agent_steps = [i.steps for i in model.schedule.agents] # one step for each of 2 agents - assert all(map(lambda x: x == 1, agent_steps)) + assert all(x == 1 for x in agent_steps) def test_add_non_unique_ids(self): """