From f7f6873e2627c6c018958a10844cf0b446512a6a Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 7 Jun 2024 22:56:39 -0500 Subject: [PATCH] fix: unbound local var (#127) * fix: resolve unbound local variable Closes #118 * chore: clean up issues identified by linter * revert rename * tidy up workflows * bump pytest --- .github/workflows/build_tests.yml | 4 ++- .github/workflows/install_tests.yml | 9 +++--- .github/workflows/unit_tests.yml | 47 +++++++++++++++-------------- ovos_config/__main__.py | 9 +++--- ovos_config/models.py | 14 +++++---- ovos_config/utils.py | 5 ++- requirements/tests.txt | 2 +- 7 files changed, 48 insertions(+), 42 deletions(-) diff --git a/.github/workflows/build_tests.yml b/.github/workflows/build_tests.yml index f5a9411..3a71fbb 100644 --- a/.github/workflows/build_tests.yml +++ b/.github/workflows/build_tests.yml @@ -13,4 +13,6 @@ jobs: uses: neongeckocom/.github/.github/workflows/python_build_tests.yml@master with: test_pipaudit: true - pipaudit_ignored: "GHSA-r9hx-vwmv-q579 PYSEC-2022-43012 GHSA-j8r2-6x86-q33q" \ No newline at end of file + # PYSEC-2023-228 is a pip vulnerability that only exists in the pipeline + # GHSA-9wx4-h78v-vm56 is problematic but it's an issue upstream + pipaudit_ignored: "GHSA-r9hx-vwmv-q579 PYSEC-2022-43012 GHSA-j8r2-6x86-q33q PYSEC-2023-228 GHSA-9wx4-h78v-vm56" diff --git a/.github/workflows/install_tests.yml b/.github/workflows/install_tests.yml index 4aaabea..1df4dad 100644 --- a/.github/workflows/install_tests.yml +++ b/.github/workflows/install_tests.yml @@ -11,14 +11,15 @@ jobs: strategy: max-parallel: 2 matrix: - python-version: [ 3.7, 3.8, 3.9, "3.10" ] + python-version: [3.8, 3.9, "3.10", "3.11"] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Setup Python - uses: actions/setup-python@v1 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} + cache: "pip" - name: Install Build Tools run: | python -m pip install build wheel @@ -31,4 +32,4 @@ jobs: python setup.py bdist_wheel - name: Install package run: | - pip install .[all] \ No newline at end of file + pip install .[all] diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index eba3247..6d1c3b1 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -4,29 +4,29 @@ on: branches: - dev paths-ignore: - - 'ovos_config/version.py' - - 'examples/**' - - '.github/**' - - '.gitignore' - - 'LICENSE' - - 'CHANGELOG.md' - - 'MANIFEST.in' - - 'readme.md' - - 'scripts/**' + - "ovos_config/version.py" + - "examples/**" + - ".github/**" + - ".gitignore" + - "LICENSE" + - "CHANGELOG.md" + - "MANIFEST.in" + - "readme.md" + - "scripts/**" push: branches: - master paths-ignore: - - 'ovos_config/version.py' - - 'requirements/**' - - 'examples/**' - - '.github/**' - - '.gitignore' - - 'LICENSE' - - 'CHANGELOG.md' - - 'MANIFEST.in' - - 'readme.md' - - 'scripts/**' + - "ovos_config/version.py" + - "requirements/**" + - "examples/**" + - ".github/**" + - ".gitignore" + - "LICENSE" + - "CHANGELOG.md" + - "MANIFEST.in" + - "readme.md" + - "scripts/**" workflow_dispatch: jobs: @@ -34,14 +34,15 @@ jobs: strategy: max-parallel: 2 matrix: - python-version: [ 3.7, 3.8, 3.9] + python-version: [3.8, 3.9, "3.10", "3.11"] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} + cache: "pip" - name: Install System Dependencies run: | sudo apt-get update @@ -62,4 +63,4 @@ jobs: - name: Upload coverage env: CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}} - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v4 diff --git a/ovos_config/__main__.py b/ovos_config/__main__.py index 19f2b3b..b08c508 100644 --- a/ovos_config/__main__.py +++ b/ovos_config/__main__.py @@ -160,7 +160,7 @@ def show(user, system, remote, section, list_sections): # based on chosen configuration if name != "Joined": _sections = [k for k, v in config.items() if isinstance(v, dict)] - if len([k for k, v in config.items() if not isinstance(v, dict)]): + if [k for k, v in config.items() if not isinstance(v, dict)]: _sections.append("base") else: _sections = SECTIONS @@ -275,7 +275,7 @@ def set(key, value): console.print("User exit", style="red") exit() elif not paths: - console.print(f"[red]Error:[/red] No key that fits the query") + console.print("[red]Error:[/red] No key that fits the query") exit() else: choice = 0 @@ -292,6 +292,8 @@ def set(key, value): f"(type: [red]{selected_type}[/red]) ")) value = value.replace('"','').replace("'","").replace("`","") + local_conf = CONFIGS[2][1] + _value = None # type checking/casting try: if isinstance(selected_value, str): @@ -319,8 +321,7 @@ def set(key, value): except (TypeError, ValueError): console.print(f"[red]Error:[/red] The value passed can't be cast into {selected_type}") exit() - - local_conf = CONFIGS[2][1] + pathSet(local_conf, selected_path, _value) local_conf.store() diff --git a/ovos_config/models.py b/ovos_config/models.py index 10a0206..b4a9c22 100644 --- a/ovos_config/models.py +++ b/ovos_config/models.py @@ -96,13 +96,13 @@ def load_local(self, path=None): """ path = path or self.path if not path: - LOG.error(f"in memory configuration, nothing to load") + LOG.error("in memory configuration, nothing to load") return if exists(path) and isfile(path): with self.__lock: try: if self._get_file_format(path) == "yaml": - with open(path) as f: + with open(path, encoding="utf-8") as f: config = yaml.safe_load(f) else: config = load_commented_json(path) @@ -120,7 +120,9 @@ def load_local(self, path=None): LOG.debug(f"Configuration '{path}' not defined, skipping") def reload(self): - if isfile(self.path) and self._last_loaded == getmtime(self.path): + if isfile(self.path) \ + and self._last_loaded \ + and self._last_loaded == getmtime(self.path): LOG.debug(f"{self.path} not changed since last load " f"(changed {time() - self._last_loaded} seconds ago)") return @@ -129,15 +131,15 @@ def reload(self): def store(self, path=None): path = path or self.path if not path: - LOG.error(f"in memory configuration, no save location") + LOG.error("in memory configuration, no save location") return with self.__lock: if self._get_file_format(path) == "yaml": - with open(path, 'w+') as f: + with open(path, 'w+', encoding="utf-8") as f: yaml.dump(dict(self), f, allow_unicode=True, default_flow_style=False, sort_keys=False) else: - with open(path, 'w+') as f: + with open(path, 'w+', encoding="utf-8") as f: json.dump(self, f, indent=2) def merge(self, conf): diff --git a/ovos_config/utils.py b/ovos_config/utils.py index 1b7c6b9..a264392 100644 --- a/ovos_config/utils.py +++ b/ovos_config/utils.py @@ -65,8 +65,8 @@ def init_module_config(module_name: str, module_override: str, # Check for and update submodules if module_name == "__main__": - raise ValueError(f"Configuring `__main__` has unintended consequences" - f"and is not supported here") + raise ValueError("Configuring `__main__` has unintended consequences" + "and is not supported here") if module_name in ovos_conf['submodule_mappings']: LOG.debug(f"{module_name} already configured, skipping configuration") else: @@ -109,4 +109,3 @@ def init_module_config(module_name: str, module_override: str, importlib.reload(ovos_config.models) importlib.reload(ovos_config.config) importlib.reload(ovos_config) - diff --git a/requirements/tests.txt b/requirements/tests.txt index a6b8459..74a681e 100644 --- a/requirements/tests.txt +++ b/requirements/tests.txt @@ -1,6 +1,6 @@ coveralls==1.8.2 flake8==3.7.9 -pytest==5.2.4 +pytest==8.2.2 pytest-cov==2.8.1 cov-core==1.15.0 sphinx==2.2.1