From 6bc5e41a304785fdc80c33ef419e4cbd348c7544 Mon Sep 17 00:00:00 2001 From: "Xuefeng Ding (IHEP)" <dingxf@ihep.ac.cn> Date: Fri, 29 Nov 2024 01:36:50 +0800 Subject: [PATCH] fix mypy --- .gitignore | 1 + .pre-commit-config.yaml | 13 +++++-- .vscode/settings.json | 3 ++ noxfile.py | 66 +------------------------------- pyproject.toml | 54 ++++++++++++++------------ python/myproj/simulation/core.py | 2 +- 6 files changed, 45 insertions(+), 94 deletions(-) diff --git a/.gitignore b/.gitignore index f066e9c..ac2521c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ build/ .cache/ .venv/ +.coverage diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 601377a..233f5b2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -37,14 +37,19 @@ repos: - id: nbstripout types_or: [jupyter] - - repo: https://github.com/charliermarsh/ruff-pre-commit + - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.8.0 # Use the latest version hooks: - id: ruff args: ["--fix", "--show-fixes"] - id: ruff-format - - repo: https://github.com/RobertCraigie/pyright-python - rev: v1.1.389 + - repo: https://github.com/pre-commit/mirrors-mypy + rev: "v1.13.0" hooks: - - id: pyright + - id: mypy + + # - repo: https://github.com/PyCQA/pylint + # rev: "v3.3.1" + # hooks: + # - id: pylint diff --git a/.vscode/settings.json b/.vscode/settings.json index 6fc7c93..86d7a47 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,6 +21,9 @@ "[cmake]": { "editor.defaultFormatter": "josetr.cmake-language-support-vscode" }, + "[toml]": { + "editor.defaultFormatter": "tamasfe.even-better-toml" + }, "clangd.arguments": [ "--log=info", "--pretty", diff --git a/noxfile.py b/noxfile.py index 45cc1c4..b2ced96 100644 --- a/noxfile.py +++ b/noxfile.py @@ -37,6 +37,7 @@ def dev(session: nox.Session) -> None: "pip>=23", "nox", "pytest", + "pytest-cov", "pre-commit", ) session.run( @@ -59,13 +60,6 @@ def cpp_lint_slow(session: nox.Session) -> None: session.run("clang-tidy", "-p", "build", *cpp_files) -@nox.session(reuse_venv=True) -def cpp_lint(session: nox.Session) -> None: - cpp_files = glob.glob("**/*.cc", recursive=True) - session.install("clangd-tidy") - session.run("clangd-tidy", "-p", "build", *cpp_files) - - @nox.session(reuse_venv=True) def rr_run(session: nox.Session) -> None: """ @@ -76,15 +70,6 @@ def rr_run(session: nox.Session) -> None: session.run("repo-review", ".") -# @nox.session -# def rr_lint(session: nox.Session) -> None: -# """ -# Run the linter. -# """ -# session.install("pre-commit") -# session.run("pre-commit", "run", "--all-files", *session.posargs) - - @nox.session def rr_pylint(session: nox.Session) -> None: """ @@ -95,52 +80,3 @@ def rr_pylint(session: nox.Session) -> None: session.install("-e.") session.install("pylint>=3.2") session.run("pylint", "myproj.simulation", *session.posargs) - - -# @nox.session -# def rr_tests(session: nox.Session) -> None: -# """ -# Run the unit and regular tests for sp-repo-review. -# """ -# session.install("-e.[test,cli]") -# session.run("pytest", *session.posargs, env={"PYTHONWARNDEFAULTENCODING": "1"}) - - -# @nox.session(reuse_venv=True) -# def rr_build(session: nox.Session) -> None: -# """ -# Build an SDist and wheel for sp-repo-review. -# """ - -# build_p = DIR.joinpath("build") -# if build_p.exists(): -# shutil.rmtree(build_p) - -# session.install("build") -# session.run("python", "-m", "build") - - -@nox.session -def build_extension(session: nox.Session) -> None: - # Install build dependencies - session.install( - "pybind11", "scikit-build", "setuptools", "wheel", "pybind11-stubgen" - ) - - # Build the extension module using scikit-build - session.run("python", "-m", "pip", "install", ".", "--no-build-isolation") - - # Generate the .pyi file using pybind11-stubgen - # Replace 'your_package._hello' with the actual module name - session.run("pybind11-stubgen", "your_package._hello") - - # Move the generated .pyi file to the source directory - # By default, pybind11-stubgen outputs to 'stubs/your_package/_hello/__init__.pyi' - stub_src = ( - Path("stubs") / "myproj" / "simulation" / "_csrc" / "_hello" / "__init__.pyi" - ) - stub_dest = Path("myproj") / "simulation" / "_csrc" / "_hello.pyi" - session.run("mv", stub_src, stub_dest, external=True) - - # Clean up the stubs directory - session.run("rm", "-r", "stubs", external=True) diff --git a/pyproject.toml b/pyproject.toml index 30d55b3..1745f2a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,12 +7,8 @@ name = "myproj_simulation" version = "0.2.1" description = "A great package." readme = "README.md" -authors = [ - { name = "Xuefeng Ding", email = "dingxf@ihep.ac.cn" }, -] -maintainers = [ - { name = "Xuefeng Ding (IHEP)", email = "dingxf@ihep.ac.cn" }, -] +authors = [{ name = "Xuefeng Ding", email = "dingxf@ihep.ac.cn" }] +maintainers = [{ name = "Xuefeng Ding (IHEP)", email = "dingxf@ihep.ac.cn" }] requires-python = ">=3.9" dependencies = [] @@ -41,7 +37,7 @@ Changelog = "https://package.readthedocs.io/en/latest/changelog.html" [tool.scikit-build.wheel] install-dir = "myproj/simulation" -exclude = ["CMakeLists.txt","*.cc","*.h"] +exclude = ["CMakeLists.txt", "*.cc", "*.h"] # [tool.scikit-build.package-data] # myproj = ["py.typed"] @@ -52,25 +48,35 @@ strict = true ignore_missing_imports = true warn_unused_configs = true warn_unreachable = true +enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] disable_error_code = [] -mypy_path = ["python"] - -[tool.pylint."MESSAGES CONTROL"] -disable = [ - "C0114", # Missing module docstring - "C0115", # Missing class docstring - "C0116", # Missing function or method docstring -] - -[tool.pylint.MASTER] -extension-pkg-allow-list = "myproj.simulation" +disallow_untyped_decorators = false +mypy_path = ["python/myproj/simulation"] [tool.ruff.lint] select = [ - "E", # pycodestyle - "F", # Pyflakes - "UP", # pyupgrade - "B", # flake8-bugbear - "SIM", # flake8-simplify - "I", # isort + "E", # pycodestyle + "F", # Pyflakes + "UP", # pyupgrade + "B", # flake8-bugbear + "SIM", # flake8-simplify + "I", # isort +] + +[tool.pytest.ini_options] +minversion = "7" +testpaths = ["python/tests"] +python_files = "test_*.py" +python_classes = "Test*" +python_functions = "test_*" +addopts = [ + "--cov=myproj.simulation", + "--cov-report=term-missing", + "-ra", + "--showlocals", + "--strict-markers", + "--strict-config", ] +xfail_strict = true +filterwarnings = ["error"] +log_cli_level = "info" diff --git a/python/myproj/simulation/core.py b/python/myproj/simulation/core.py index c7f2525..5f49694 100644 --- a/python/myproj/simulation/core.py +++ b/python/myproj/simulation/core.py @@ -5,5 +5,5 @@ def div(a: float, b: float) -> float: return a / b -def complex2(a, b: float) -> float: +def complex2(a: float, b: float) -> float: return complexf(a, b) + add(a, b) -- GitLab