diff --git a/.vscode/settings.json b/.vscode/settings.json index 25860b5e51f60ecfef6527c1530ab3ce02c49de2..6fc7c93a68570454a16958a7525df3cf055b428b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,20 +4,23 @@ "editor.formatOnSave": true, "[python]": { "editor.codeActionsOnSave": { - "source.fixAll": "explicit" + "source.fixAll": "explicit", + "source.organizeImports": "explicit" }, "editor.defaultFormatter": "charliermarsh.ruff" }, - // "notebook.codeActionsOnSave": { - // "source.fixAll": "explicit" - // }, - "notebook.defaultFormatter": "charliermarsh.ruff", - // "ruff.lint.args": ["--config=pyproject.toml"], - "ruff.fixAll": true, + "notebook.codeActionsOnSave": { + "notebook.source.fixAll": "explicit", + "notebook.source.organizeImports": "explicit" + }, + "notebook.formatOnSave.enabled": true, + "notebook.lineNumbers": "on", "[cpp]": { "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd" }, - "notebook.lineNumbers": "on", + "[cmake]": { + "editor.defaultFormatter": "josetr.cmake-language-support-vscode" + }, "clangd.arguments": [ "--log=info", "--pretty", @@ -35,11 +38,16 @@ "clangd.path": "clangd-17", "jupyter.debugJustMyCode": false, "debugpy.debugJustMyCode": false, - "pylint.args": ["--prefer-stubs=true"], - // "pylint.args": [ - // "--extension-pkg-allow-list=simulation._csrc._hello", - // "--prefer-stubs=true" - // ], + "ruff.fixAll": true, + "python.defaultInterpreterPath": ".venv/bin/python", "python.testing.pytestEnabled": true, - "python.testing.unittestEnabled": false + "python.testing.unittestEnabled": false, + "python.analysis.autoFormatStrings": true, + "python.analysis.autoImportCompletions": true, + "python.analysis.completeFunctionParens": true, + "python.analysis.inlayHints.functionReturnTypes": true, + "python.analysis.inlayHints.pytestParameters": true, + "python.analysis.inlayHints.variableTypes": true, + "python.analysis.typeCheckingMode": "strict", + "python.analysis.extraPaths": ["${workspaceFolder}/python"] } diff --git a/README.md b/README.md index ebeb45bf39eaa0e1ff0582f7560a3260d69d4db2..638045f10ad4c140149840ae8f5396b6a58db8f0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # scikit-build-cpp-python - - ## Why and What use scikit-build-core to setup a python-cmake project that can be installed with `pip install -e .` @@ -10,15 +8,20 @@ use scikit-build-core to setup a python-cmake project that can be installed with - package name has nothing to do with the import path. pacakge named my_package is installed with `pip install .`, uninstalled with `pip uninstall my_package`, but can be used as `import kk.ss as ks`. - installation of cpp files: + ```cmake install(TARGET mylib LIBRARY DESTINATION .) ``` + and set + ``` [tool.scikit-build] wheel.install-dir = "kk/ss" ``` + - installation of python files: + ``` [tool.scikit-build.wheel.packages] "mm/tt" = "python/myproj/simulation" @@ -27,10 +30,8 @@ wheel.install-dir = "kk/ss" wheel.exclude = ["CMakeLists.txt","*.cc","*.h"] ``` - ## special variable - - `${SKBUILD_PLATLIB_DIR}`: The original platlib directory. Anything here goes directly to site-packages when a wheel is installed. - `${SKBUILD_DATA_DIR}`: The data directory. Anything here goes to the root of @@ -47,13 +48,19 @@ wheel.exclude = ["CMakeLists.txt","*.cc","*.h"] wheel. ## style and lint check + ### notebook output stripping + - use pre-commit + 1. install + ```bash pip install pre-commit nbstripout ``` + 2. set + ```yaml repos: - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/kynan/nbstripout @@ -62,22 +69,30 @@ repos: - id: nbstripout files: \.ipynb$ ``` + 3. install + ```bash pre-commit install ``` + 4. run and verify + ```bash pre-commit run --all-files ``` + or + ```bash nox -s pre_commit ``` ### c++ formating + - pre-commit + vscode extensions - install extensions + ``` Name: clangd Id: llvm-vs-code-extensions.vscode-clangd @@ -86,19 +101,23 @@ Version: 0.1.30 Publisher: LLVM VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd ``` + and make sure this is in `settings.json`: + ```json "[cpp]": { "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd" } ``` + - install pre-commit hooks -Name: Black Formatter -Id: ms-python.black-formatter -Description: Formatting support for Python files using the Black formatter. -Version: 2024.4.0 -Publisher: Microsoft -VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter + Name: Black Formatter + Id: ms-python.black-formatter + Description: Formatting support for Python files using the Black formatter. + Version: 2024.4.0 + Publisher: Microsoft + VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter + ```yaml - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/pre-commit/mirrors-clang-format rev: v19.1.3 @@ -108,47 +127,52 @@ VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-pyth ``` ### python formatting + - pre-commit + vscode extensions - install extensions + ``` -Name: Black Formatter -Id: ms-python.black-formatter -Description: Formatting support for Python files using the Black formatter. -Version: 2024.4.0 -Publisher: Microsoft -VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter +Name: Ruff +Id: charliermarsh.ruff +Description: A Visual Studio Code extension with support for the Ruff linter. +Version: 2024.56.0 +Publisher: Astral Software +VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff ``` + and update `settings.json` + ```json "[python]": { "editor.codeActionsOnSave": { - "source.organizeImports": "always" + "source.fixAll": "explicit", + "source.organizeImports": "explicit" }, - "editor.defaultFormatter": "ms-python.black-formatter" + "editor.defaultFormatter": "charliermarsh.ruff" }, - "isort.args": ["--profile", "black"], + "notebook.codeActionsOnSave": { + "notebook.source.fixAll": "explicit", + "notebook.source.organizeImports": "explicit" + }, + "notebook.formatOnSave.enabled": true, ``` + - install pre-commit hooks -``` - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.10.0 # Use the latest stable version - hooks: - - id: black-jupyter - - repo: https://github.com/pycqa/isort - rev: 5.13.2 - hooks: - - id: isort - name: isort (python)repos: - - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/psf/black-pre-commit-mirror - rev: 24.10.0 # Use the latest stable version +``` + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.7.4 # Use the latest version hooks: - - id: black + - id: ruff + args: ["--fix", "--exit-non-zero-on-fix", "--show-fixes"] + - id: ruff-formatp ``` ### c++ lint + - nox + vscode extensions - setup nox + ```python @nox.session(venv_backend="none") def dev(session: nox.Session) -> None: @@ -156,6 +180,8 @@ def dev(session: nox.Session) -> None: Prepare a .venv folder. """ + library_output_dir = DIR / "python" / "myproj" / "simulation" / "_csrc" + session.run(sys.executable, "-m", "venv", ".venv") session.run( ".venv/bin/pip", @@ -163,17 +189,19 @@ def dev(session: nox.Session) -> None: "scikit-build-core[pyproject]", "pybind11", "pip>=23", + "nox", + "pytest", ) session.run( ".venv/bin/pip", "install", "--no-build-isolation", "--check-build-dependencies", - "-ve.", - "-C", - "cmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1", - "-C", - "build-dir=build", + "-ve", + ".", + "-Ccmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1", + f"-Ccmake.define.CMAKE_LIBRARY_OUTPUT_DIRECTORY={library_output_dir}", + "-Cbuild-dir=build", ) @nox.session(reuse_venv=True) @@ -182,7 +210,9 @@ def cpp_lint(session: nox.Session) -> None: session.install("clangd-tidy") session.run("clangd-tidy", "-p", "build", *cpp_files) ``` + - vscode extensions. use `clangd` + ``` Name: clangd Id: llvm-vs-code-extensions.vscode-clangd @@ -191,7 +221,9 @@ Version: 0.1.30 Publisher: LLVM VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd ``` + and set + ```json "clangd.arguments": [ "--log=info", @@ -211,16 +243,21 @@ and set ``` ### python lint + #### ruff: pre-commit + vscode extensions + - install pre-commit + ```yaml - - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: v0.7.3 # Use the latest version - hooks: - - id: ruff - args: ["--fix", "--show-fixes"] +- repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.7.3 # Use the latest version + hooks: + - id: ruff + args: ["--fix", "--show-fixes"] ``` + - install extensions + ``` Name: Ruff Id: charliermarsh.ruff @@ -233,7 +270,9 @@ VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=charlie #### pylint: nox + vscode extensions ## vscode extensions to install + - "isort", which sort python import + ``` Name: isort Id: ms-python.isort @@ -242,7 +281,9 @@ Version: 2023.10.1 Publisher: Microsoft VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.isort ``` + - "pylint", for complrehensive code analysis + ``` Name: Pylint Id: ms-python.pylint diff --git a/notebooks/example.ipynb b/notebooks/example.ipynb index d9eedf5e2c34beeac71a43647f5a0ef33c83f78b..f47fc7e9cbf9685eda7389c76d4cac9c45121b98 100644 --- a/notebooks/example.ipynb +++ b/notebooks/example.ipynb @@ -74,6 +74,22 @@ "results = json.loads(output)\n", "print(json.dumps(results, indent=2))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from collections.abc import Iterable\n", + "\n", + "\n", + "def sum_even_numbers(numbers: Iterable[int]) -> int:\n", + " \"\"\"\n", + " Given an iterable of integers, return the sum of all even numbers in the iterable.\\\n", + " \"\"\"\n", + " return sum(num for num in numbers if num % 2 == 0)" + ] } ], "metadata": { diff --git a/noxfile.py b/noxfile.py index f877b03da3b25ed314fb749eb04d1d78f79629a7..45cc1c41f34d78148289816a25aedad0e4b3baa6 100644 --- a/noxfile.py +++ b/noxfile.py @@ -26,6 +26,8 @@ def dev(session: nox.Session) -> None: Prepare a .venv folder. """ + library_output_dir = DIR / "python" / "myproj" / "simulation" / "_csrc" + session.run(sys.executable, "-m", "venv", ".venv") session.run( ".venv/bin/pip", @@ -33,17 +35,20 @@ def dev(session: nox.Session) -> None: "scikit-build-core[pyproject]", "pybind11", "pip>=23", + "nox", + "pytest", + "pre-commit", ) session.run( ".venv/bin/pip", "install", "--no-build-isolation", "--check-build-dependencies", - "-ve.", - "-C", - "cmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1", - "-C", - "build-dir=build", + "-ve", + ".", + "-Ccmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1", + f"-Ccmake.define.CMAKE_LIBRARY_OUTPUT_DIRECTORY={library_output_dir}", + "-Cbuild-dir=build", ) diff --git a/pyproject.toml b/pyproject.toml index 7c1f46e1f60190489ebaeda45c99b3123aad6513..30d55b3ecf0603bfb8ed6a6ac573d232ca15b2ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,10 @@ Changelog = "https://package.readthedocs.io/en/latest/changelog.html" install-dir = "myproj/simulation" exclude = ["CMakeLists.txt","*.cc","*.h"] +# [tool.scikit-build.package-data] +# myproj = ["py.typed"] +# myproj.simulation._csrc = ["*.pyi"] + [tool.mypy] strict = true ignore_missing_imports = true @@ -51,13 +55,6 @@ warn_unreachable = true disable_error_code = [] mypy_path = ["python"] -[tool.isort] -multi_line_output = 3 -include_trailing_comma = true -force_grid_wrap = 0 -line_length = 88 -profile = "black" - [tool.pylint."MESSAGES CONTROL"] disable = [ "C0114", # Missing module docstring diff --git a/python/myproj/simulation/_csrc/_hello.cpython-310-x86_64-linux-gnu.so b/python/myproj/simulation/_csrc/_hello.cpython-310-x86_64-linux-gnu.so new file mode 100755 index 0000000000000000000000000000000000000000..01bcd3dfac649dbc6500a2a5c2a163a864d21ba9 Binary files /dev/null and b/python/myproj/simulation/_csrc/_hello.cpython-310-x86_64-linux-gnu.so differ diff --git a/python/myproj/simulation/_csrc/libcomplexf.so b/python/myproj/simulation/_csrc/libcomplexf.so new file mode 100755 index 0000000000000000000000000000000000000000..f12fc94e0546749c8bd06eb95d8a270e902bc11b Binary files /dev/null and b/python/myproj/simulation/_csrc/libcomplexf.so differ diff --git a/python/myproj/simulation/_csrc/libhello.so b/python/myproj/simulation/_csrc/libhello.so new file mode 100755 index 0000000000000000000000000000000000000000..914f56c088e597c1a743da262b051b2aa0718cac Binary files /dev/null and b/python/myproj/simulation/_csrc/libhello.so differ diff --git a/python/myproj/simulation/py.typed b/python/myproj/simulation/py.typed new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391