Skip to content

Commit

Permalink
add typing stubs (#3)
Browse files Browse the repository at this point in the history
* remove pybind11

* update

* fix

* fix

* fix

* fix

* move code

* fix

* update

* fix

* fix

* fix

* fix

---------

Co-authored-by: tang zhixiong <[email protected]>
  • Loading branch information
district10 and zhixiong-tang authored Oct 2, 2024
1 parent 55c5c8b commit 2050715
Show file tree
Hide file tree
Showing 21 changed files with 789 additions and 333 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.x"
python-version: "3.9"
- uses: pre-commit/[email protected]
4 changes: 2 additions & 2 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
strategy:
fail-fast: false
matrix:
platform: [ubuntu-20.04, windows-2019, macos-11]
python-version: ["3.8", "3.9", "3.10"]
platform: [ubuntu-20.04, windows-2019, macos-13]
python-version: ["3.9"]

runs-on: ${{ matrix.platform }}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
with:
submodules: true

- uses: pypa/cibuildwheel@v2.12.0
- uses: pypa/cibuildwheel@v2.21.1
env:
# CIBW_ARCHS: auto64
CIBW_ARCHS_LINUX: x86_64
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
steps:
- uses: actions/setup-python@v4
with:
python-version: "3.x"
python-version: "3.9"

- uses: actions/download-artifact@v3
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ _generate/
*env*
wheelhouse
site
stubs
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[submodule "pybind11"]
path = pybind11
url = https://github.com/pybind/pybind11.git
branch = master
[submodule "headers"]
path = headers
url = https://github.com/cubao/headers.git
14 changes: 5 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,14 @@ repos:
- id: requirements-txt-fixer
- id: trailing-whitespace

# Black, the code formatter, natively supports pre-commit
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
exclude: ^(docs)

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.254"
# Check linting and style issues
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.6.5"
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format
exclude: ^(docs)

# Checking static types
- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
14 changes: 14 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"configurations": [
{
"name": "Linux",
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}
47 changes: 37 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
cmake_minimum_required(VERSION 3.4...3.18)
project(naive_svg)
cmake_minimum_required(VERSION 3.15...3.26)
if(NOT DEFINED SKBUILD_PROJECT_NAME)
set(SKBUILD_PROJECT_NAME "naive_svg")
endif()
if(NOT DEFINED PROJECT_VERSION)
set(PROJECT_VERSION "dev")
endif()
# https://scikit-build-core.readthedocs.io/en/latest/cmakelists.html#accessing-information
project(
${SKBUILD_PROJECT_NAME}
VERSION ${SKBUILD_PROJECT_VERSION}
LANGUAGES CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)

# set(CMAKE_BUILD_TYPE "Debug")
if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE
"Release"
CACHE STRING "" FORCE)
message(STATUS "Set build type to default: ${CMAKE_BUILD_TYPE}")
else()
message(STATUS "Your build type: ${CMAKE_BUILD_TYPE}")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -ggdb")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
endif()

# https://scikit-build-core.readthedocs.io/en/latest/getting_started.html
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)
include_directories(headers/include)

set(PYBIND11_CPP_STANDARD -std=c++14)
add_subdirectory(pybind11)
file(GLOB SRCS src/*.cpp)
pybind11_add_module(_naive_svg ${SRCS})

# EXAMPLE_VERSION_INFO is defined by setup.py and passed into the C++ code as a
# define (VERSION_INFO) here.
target_compile_definitions(_naive_svg
PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO})
python_add_library(_core MODULE ${SRCS} WITH_SOABI)
target_link_libraries(_core PRIVATE pybind11::headers)
target_include_directories(_core PRIVATE src)
target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})
install(TARGETS _core DESTINATION ${PROJECT_NAME})
43 changes: 23 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PROJECT_SOURCE_DIR ?= $(abspath ./)
PROJECT_NAME ?= $(shell basename $(PROJECT_SOURCE_DIR))
NUM_JOBS ?= 8

all:
@echo nothing special
Expand All @@ -14,11 +15,7 @@ lint:
pre-commit run -a
lint_install:
pre-commit install

build:
mkdir -p build && cd build && \
cmake .. && make
.PHONY: build
.PHONY: lint lint_install

docs_build:
python3 -m pip install -r docs/requirements.txt
Expand All @@ -28,7 +25,7 @@ docs_serve:
mkdocs serve -a 0.0.0.0:8088

DOCKER_TAG_WINDOWS ?= ghcr.io/cubao/build-env-windows-x64:v0.0.1
DOCKER_TAG_LINUX ?= ghcr.io/cubao/build-env-manylinux2014-x64:v0.0.4
DOCKER_TAG_LINUX ?= ghcr.io/cubao/build-env-manylinux2014-x64:v0.0.5
DOCKER_TAG_MACOS ?= ghcr.io/cubao/build-env-macos-arm64:v0.0.1

test_in_win:
Expand All @@ -48,27 +45,31 @@ test_in_dev_container:
-v `pwd`:`pwd` -w `pwd` -it $(DEV_CONTAINER_IMAG) bash

PYTHON ?= python3
build:
$(PYTHON) -m pip install scikit_build_core pyproject_metadata pathspec pybind11
CMAKE_BUILD_PARALLEL_LEVEL=$(NUM_JOBS) $(PYTHON) -m pip install --no-build-isolation -Ceditable.rebuild=true -Cbuild-dir=build -ve.
python_install:
$(PYTHON) setup.py install
python_build:
$(PYTHON) setup.py bdist_wheel
$(PYTHON) -m pip install . --verbose
python_wheel:
$(PYTHON) -m pip wheel . -w build --verbose
python_sdist:
$(PYTHON) setup.py sdist
python_test:
$(PYTHON) -m pip sdist . --verbose
python_test: pytest
pytest:
python3 -m pip install pytest
pytest tests
pytest tests/test_basic.py
.PHONY: build

restub:
pybind11-stubgen naive_svg._core -o stubs
cp stubs/naive_svg/_core.pyi src/naive_svg

# conda create -y -n py36 python=3.6
# conda create -y -n py37 python=3.7
# conda create -y -n py38 python=3.8
# conda create -y -n py39 python=3.9
# conda create -y -n py310 python=3.10
# conda create -y -n py311 python=3.11
# conda create -y -n py312 python=3.12
# conda env list
python_build_py36:
PYTHON=python conda run --no-capture-output -n py36 make python_build
python_build_py37:
PYTHON=python conda run --no-capture-output -n py37 make python_build
python_build_py38:
PYTHON=python conda run --no-capture-output -n py38 make python_build
python_build_py39:
Expand All @@ -77,10 +78,12 @@ python_build_py310:
PYTHON=python conda run --no-capture-output -n py310 make python_build
python_build_py311:
PYTHON=python conda run --no-capture-output -n py311 make python_build
python_build_all: python_build_py36 python_build_py37 python_build_py38 python_build_py39 python_build_py310 python_build_py311
python_build_py312:
PYTHON=python conda run --no-capture-output -n py312 make python_build
python_build_all: python_build_py38 python_build_py39 python_build_py310 python_build_py311 python_build_py312
python_build_all_in_linux:
docker run --rm -w `pwd` -v `pwd`:`pwd` -v `pwd`/build/linux:`pwd`/build -it $(DOCKER_TAG_LINUX) make python_build_all repair_wheels
python_build_all_in_macos: python_build_py38 python_build_py39 python_build_py310 python_build_py311
python_build_all_in_macos: python_build_py38 python_build_py39 python_build_py310 python_build_py311 python_build_py312
python_build_all_in_windows: python_build_all

repair_wheels:
Expand Down
2 changes: 0 additions & 2 deletions naive_svg/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion pybind11
Submodule pybind11 deleted from 8a099e
100 changes: 72 additions & 28 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,48 +1,92 @@
[build-system]
requires = [
"setuptools>=42",
"wheel",
"ninja",
"cmake>=3.12",
requires = ["scikit-build-core>=0.3.3", "pybind11"]
build-backend = "scikit_build_core.build"


[project]
name = "naive_svg"
version = "0.0.3"
url = "https://github.com/cubao/pybind11-naive-svg"
description="naive svg writer"
readme = "README.md"
authors = [
{ name = "district10", email = "[email protected]" },
]
requires-python = ">=3.7"
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
build-backend = "setuptools.build_meta"

[tool.mypy]
files = "setup.py"
python_version = "3.7"
strict = true
show_error_codes = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
warn_unreachable = true
[project.optional-dependencies]
test = ["pytest", "scipy"]


[tool.scikit-build]
wheel.expand-macos-universal-tags = true

[[tool.mypy.overrides]]
module = ["ninja"]
ignore_missing_imports = true

[tool.pytest.ini_options]
minversion = "6.0"
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
xfail_strict = true
filterwarnings = ["error"]
log_cli_level = "INFO"
filterwarnings = [
"error",
]
testpaths = ["tests"]


[tool.cibuildwheel]
test-command = "pytest {project}/tests"
test-extras = ["test"]
test-skip = ["*universal2:arm64"]
# Setuptools bug causes collision between pypy and cpython artifacts
before-build = "rm -rf {project}/build"
build-verbosity = 1


[tool.ruff]
src = ["src"]

[tool.ruff.lint]
exclude = ["*.pyi"]
extend-select = [
"B", # flake8-bugbear
"B904",
"I", # isort
"PGH", # pygrep-hooks
"RUF", # Ruff-specific
"UP", # pyupgrade
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"NPY", # NumPy specific rules
"PD", # pandas-vet
]
extend-ignore = [
"E501", # Line too long
ignore = [
"PLR09", # Too many X
"PLR2004", # Magic comparison
"PTH100",
"PTH103",
"PTH120",
]
target-version = "py37"
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["T20"]
Loading

0 comments on commit 2050715

Please sign in to comment.