From f31e5999faae958fb9842ac29cd374a8b9014109 Mon Sep 17 00:00:00 2001 From: Gaurav Jadhav Date: Thu, 10 Sep 2026 14:23:24 +0530 Subject: [PATCH] MAINT: Code-hygiene quick wins (#882) Address the remaining items of the July 2026 audit's hygiene list. Item 1 (common_messages.py) is already gone from main. - quantecon/__init__.py: the bare except around the numba import caught KeyboardInterrupt and SystemExit and dropped the original exception. Catch ImportError, chain it with "from e", and name pip as well as conda. - tests/test_quadsum.py: drop the nose-era __main__ block. It called three undefined names and was the source of every F821 in the package. - _dle.py, util/notebooks.py: replace "isinstance(...) == True" and "type(x) == list" / "type(x) != np.ndarray" with plain isinstance checks. - tests/test_graph_tools.py: bare except becomes except KeyError. - util/timing.py: Timer.__exit__ binds none of its three arguments, so take *exc_info instead. - game_theory/game_generators/__init__.py: import the five generators explicitly and declare __all__, which lets the file drop its flake8 noqa. The exported names are unchanged. flake8 --select=F821,E722,E712,E721 quantecon goes from 7 violations to none. No behaviour change intended. --- quantecon/__init__.py | 6 +++--- quantecon/_dle.py | 4 ++-- quantecon/game_theory/game_generators/__init__.py | 9 +++++++-- quantecon/tests/test_graph_tools.py | 2 +- quantecon/tests/test_quadsum.py | 8 -------- quantecon/util/notebooks.py | 2 +- quantecon/util/timing.py | 2 +- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/quantecon/__init__.py b/quantecon/__init__.py index b88d762fb..fabf9ab27 100644 --- a/quantecon/__init__.py +++ b/quantecon/__init__.py @@ -7,10 +7,10 @@ try: import numba -except: +except ImportError as e: raise ImportError( - "Cannot import numba from current anaconda distribution. \ - Please run `conda install numba` to install the latest version.") + "Cannot import numba, which QuantEcon.py requires. Install it with " + "`pip install numba` or `conda install numba`.") from e #-Modules-# from . import distributions diff --git a/quantecon/_dle.py b/quantecon/_dle.py index ceeaee7f1..7bafed20d 100644 --- a/quantecon/_dle.py +++ b/quantecon/_dle.py @@ -243,7 +243,7 @@ def compute_sequence(self, x0, ts_length=None, Pay=None): # === Value of asset whose payout vector is Pay*xt === # # See p.145: Equation (7.11.1) - if isinstance(Pay, np.ndarray) == True: + if isinstance(Pay, np.ndarray): self.Za = Pay.T @ self.Mc self.Q = solve_discrete_lyapunov( self.A0.T * self.beta**0.5, self.Za) @@ -277,7 +277,7 @@ def irf(self, ts_length=100, shock=None): """ - if type(shock) != np.ndarray: + if not isinstance(shock, np.ndarray): # Default is to select first element of w shock = np.vstack((np.ones((1, 1)), np.zeros((self.nw - 1, 1)))) diff --git a/quantecon/game_theory/game_generators/__init__.py b/quantecon/game_theory/game_generators/__init__.py index fa20a29ea..1b51543b5 100644 --- a/quantecon/game_theory/game_generators/__init__.py +++ b/quantecon/game_theory/game_generators/__init__.py @@ -1,6 +1,11 @@ -# flake8: noqa """ game_theory.game_generators """ -from .bimatrix_generators import * +from .bimatrix_generators import (blotto_game, ranking_game, sgc_game, + tournament_game, unit_vector_game) + +__all__ = [ + 'blotto_game', 'ranking_game', 'sgc_game', 'tournament_game', + 'unit_vector_game' +] diff --git a/quantecon/tests/test_graph_tools.py b/quantecon/tests/test_graph_tools.py index 00e20b526..222c27ef9 100644 --- a/quantecon/tests/test_graph_tools.py +++ b/quantecon/tests/test_graph_tools.py @@ -134,7 +134,7 @@ def setup_method(self): for graph_dict in self.graphs.graph_dicts: try: weighted = graph_dict['weighted'] - except: + except KeyError: weighted = False graph_dict['g'] = DiGraph(graph_dict['A'], weighted=weighted) diff --git a/quantecon/tests/test_quadsum.py b/quantecon/tests/test_quadsum.py index 60011b6c6..2843144bd 100644 --- a/quantecon/tests/test_quadsum.py +++ b/quantecon/tests/test_quadsum.py @@ -53,11 +53,3 @@ def test_m_matsum(): summedval = summedval + a**i * b * a.T**i assert_allclose(retval, summedval, atol=1e-5, rtol=0) - - - -if __name__ == '__main__': - test_simplesum() - test_identitysum() - test_m_simplesum() - test_m_identitysum diff --git a/quantecon/util/notebooks.py b/quantecon/util/notebooks.py index 37b0f1bf4..5dc60e51d 100644 --- a/quantecon/util/notebooks.py +++ b/quantecon/util/notebooks.py @@ -86,7 +86,7 @@ def fetch_nb_dependencies(files, repo=REPO, raw=RAW, branch=BRANCH, folder=FOLDE import requests #-Generate Common Data Structure-# - if type(files) == list: + if isinstance(files, list): files = {"" : files} status = [] diff --git a/quantecon/util/timing.py b/quantecon/util/timing.py index f415e23c8..83c125051 100644 --- a/quantecon/util/timing.py +++ b/quantecon/util/timing.py @@ -247,7 +247,7 @@ def __enter__(self): self._start_time = time.time() return self - def __exit__(self, exc_type, exc_val, exc_tb): + def __exit__(self, *exc_info): end_time = time.time() self.elapsed = end_time - self._start_time