From 14cac7591d1715bdb8bb5c562af53a022215547c Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sat, 14 Oct 2023 12:48:18 +0900 Subject: [PATCH] Fix typing and add it to CI (#55) * Fix typing and add it to CI --- .github/workflows/lint.yml | 10 +++++---- nsiqcppstyle.py | 5 ----- nsiqcppstyle_types.py | 6 +++-- pyproject.toml | 22 +++++++++---------- ..._bufferoverflow_risky_function_for_unix.py | 4 ++-- ...do_not_use_same_filename_more_than_once.py | 4 ++-- ..._function_comment_on_function_in_header.py | 4 ++-- 7 files changed, 26 insertions(+), 29 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index af4965d..3fd00a4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,7 +2,7 @@ name: Lint CI on: pull_request: - branches: [ "master" ] + branches: ["master"] jobs: run-lints: @@ -16,11 +16,13 @@ jobs: - name: Set up pip cache uses: actions/cache@v3.3.2 with: - path: ~/.cache/pip + path: | + ~/.cache/pip + ${{ github.workspace }}/.mypy_cache key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }} restore-keys: ${{ runner.os }}-pip- - name: Install Hatch run: pipx install hatch - - name: Check style - run: hatch run lint:style + - name: Check style and typing + run: hatch run lint:all diff --git a/nsiqcppstyle.py b/nsiqcppstyle.py index bbc6964..ee01a69 100644 --- a/nsiqcppstyle.py +++ b/nsiqcppstyle.py @@ -29,11 +29,6 @@ # from nsiqcppstyle_util import * -try: - import hashlib # @UnusedImport -except ImportError: - import md5 # @UnusedImport - if __name__ == "__main__": sys.path.append(GetRuntimePath()) diff --git a/nsiqcppstyle_types.py b/nsiqcppstyle_types.py index be86d60..a1fbcc7 100644 --- a/nsiqcppstyle_types.py +++ b/nsiqcppstyle_types.py @@ -1,7 +1,7 @@ # Copyright (c) 2022 All rights reserved. # SPDX-License-Identifier: GPL-2.0-only -from typing import NewType, Union +from typing import NewType, TypeVar import nsiqcppstyle_checker import nsiqcppstyle_lexer @@ -32,7 +32,9 @@ LineText = NewType("LineText", str) # The target directory currently being analyzed -TargetDirectory = NewType("TargetDirectory", Union[str, bytes]) +TargetDirectoryStr = NewType("TargetDirectoryStr", str) +TargetDirectoryBytes = NewType("TargetDirectoryBytes", bytes) +TargetDirectory = TypeVar("TargetDirectory", TargetDirectoryStr, TargetDirectoryBytes) # The token currently being processed Token = NewType("Token", nsiqcppstyle_lexer.LexToken) diff --git a/pyproject.toml b/pyproject.toml index ea21f54..d9b3ddb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,16 +22,11 @@ classifiers = [ "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", ] -dependencies = [ -] +dependencies = [] [project.optional-dependencies] -dev = [ - "pip_search" -] -test = [ - "pytest == 7.4.*", -] +dev = ["pip_search"] +test = ["pytest == 7.4.*"] [project.urls] Documentation = "https://github.com/kunaltyagi/nsiqcppstyle/blob/master/README.md" @@ -79,8 +74,8 @@ python = "3.8" typing = "mypy --install-types --non-interactive {args:.}" # style = ["ruff {args:.}", "black --check --diff {args:.}"] style = ["black --check --diff {args:.}"] -# fmt = ["black {args:.}", "ruff --fix {args:.}", "style"] -fmt = ["black {args:.}"] +fmt = ["black {args:.}", "ruff --fix {args:.}", "style"] +# fmt = ["black {args:.}"] all = ["style", "typing"] [tool.hatch.build.targets.sdist] @@ -95,7 +90,7 @@ exclude = ["/updateagent"] # mypy-args = ["--no-warn-unused-ignores"] [tool.black] -target-version = ["py310"] +target-version = ["py38"] line-length = 120 skip-string-normalization = true extend-exclude = """ @@ -103,7 +98,7 @@ temp/ """ [tool.ruff] -target-version = "py310" +target-version = "py38" line-length = 120 select = [ "A", @@ -215,6 +210,9 @@ warn_unreachable = true [[tool.mypy.overrides]] module = "nsiqcppstyle.*" ignore_missing_imports = true +[[tool.mypy.overrides]] +module = "updateagent.*" +ignore_errors = true [tool.bandit] exclude_dirs = ["tests"] diff --git a/rules/RULE_10_1_A_do_not_use_bufferoverflow_risky_function_for_unix.py b/rules/RULE_10_1_A_do_not_use_bufferoverflow_risky_function_for_unix.py index e12aebb..752ea8a 100644 --- a/rules/RULE_10_1_A_do_not_use_bufferoverflow_risky_function_for_unix.py +++ b/rules/RULE_10_1_A_do_not_use_bufferoverflow_risky_function_for_unix.py @@ -178,7 +178,7 @@ def test8(self): ) self.ExpectError(__name__) - def test8(self): + def test9(self): self.Analyze( "thisfile.c", """ @@ -190,7 +190,7 @@ def test8(self): ) self.ExpectError(__name__) - def test9(self): + def test10(self): # known issue. Not a problem self.Analyze( "thisfile.c", diff --git a/rules/RULE_3_2_B_do_not_use_same_filename_more_than_once.py b/rules/RULE_3_2_B_do_not_use_same_filename_more_than_once.py index f4bb456..f1bcdc7 100644 --- a/rules/RULE_3_2_B_do_not_use_same_filename_more_than_once.py +++ b/rules/RULE_3_2_B_do_not_use_same_filename_more_than_once.py @@ -18,13 +18,13 @@ testdir/stadfx.* testdir1/stdafx.* """ -import string +from typing import Dict, List from nsiqcppstyle_reporter import * # @UnusedWildImport from nsiqcppstyle_rulemanager import * # @UnusedWildImport from nsiqunittest.nsiqcppstyle_unittestbase import * -filenameMap = {} +filenameMap: Dict[str, List[str]] = {} def RunRule(lexer, filename, dirname): diff --git a/rules/RULE_5_3_A_provide_doxygen_function_comment_on_function_in_header.py b/rules/RULE_5_3_A_provide_doxygen_function_comment_on_function_in_header.py index a810e65..59dd7d1 100644 --- a/rules/RULE_5_3_A_provide_doxygen_function_comment_on_function_in_header.py +++ b/rules/RULE_5_3_A_provide_doxygen_function_comment_on_function_in_header.py @@ -270,7 +270,7 @@ class J { ) self.ExpectSuccess(__name__) - def test14(self): + def test16(self): self.Analyze( "thisfile.h", """ @@ -282,7 +282,7 @@ class J { ) self.ExpectError(__name__) - def test15(self): + def test17(self): self.Analyze( "thisfile.h", """