Skip to content

Commit 4a10f88

Browse files
add typing stubs (#20)
* fix * update * remote pybind11 * move to src * update script * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * reset --------- Co-authored-by: tang zhixiong <zhixiong.tang@momenta.ai>
1 parent fdbf813 commit 4a10f88

28 files changed

+2520
-450
lines changed

.github/workflows/pip.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ on:
1010
jobs:
1111
build:
1212
strategy:
13-
fail-fast: false
13+
fail-fast: true
1414
matrix:
15-
platform: [ubuntu-20.04, windows-2019, macos-12]
16-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
15+
platform: [ubuntu-20.04, windows-2019, macos-13]
16+
python-version: ["3.9"]
1717

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

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ _generate/
99
wheelhouse
1010
!test.py
1111
site
12+
stubs

.gitmodules

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
[submodule "pybind11"]
2-
path = pybind11
3-
url = https://github.com/pybind/pybind11.git
4-
branch = master
51
[submodule "headers"]
62
path = headers
73
url = https://github.com/cubao/headers.git

.pre-commit-config.yaml

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,14 @@ repos:
3333
- id: requirements-txt-fixer
3434
- id: trailing-whitespace
3535

36-
# Black, the code formatter, natively supports pre-commit
37-
- repo: https://github.com/psf/black
38-
rev: 22.3.0
36+
# Check linting and style issues
37+
- repo: https://github.com/astral-sh/ruff-pre-commit
38+
rev: "v0.6.5"
3939
hooks:
40-
- id: black
41-
exclude: ^(docs)
42-
43-
# Sort your imports in a standard form
44-
- repo: https://github.com/PyCQA/isort
45-
rev: 5.11.5
46-
hooks:
47-
- id: isort
40+
- id: ruff
41+
args: ["--fix", "--show-fixes"]
42+
- id: ruff-format
43+
exclude: ^(docs)
4844

4945
# Upgrade older Python syntax
5046
- repo: https://github.com/asottile/pyupgrade
@@ -60,12 +56,6 @@ repos:
6056
- id: remove-tabs
6157
exclude: ^(docs|Makefile)
6258

63-
- repo: https://github.com/PyCQA/flake8
64-
rev: 3.9.2
65-
hooks:
66-
- id: flake8
67-
additional_dependencies: [flake8-bugbear]
68-
6959
# CMake formatting
7060
- repo: https://github.com/cheshirekow/cmake-format-precommit
7161
rev: v0.6.13

CMakeLists.txt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
cmake_minimum_required(VERSION 3.4...3.18)
2-
project(fast_crossing)
1+
cmake_minimum_required(VERSION 3.15...3.26)
2+
if(NOT DEFINED SKBUILD_PROJECT_NAME)
3+
set(SKBUILD_PROJECT_NAME "fast_crossing")
4+
endif()
5+
if(NOT DEFINED PROJECT_VERSION)
6+
set(PROJECT_VERSION "dev")
7+
endif()
8+
# https://scikit-build-core.readthedocs.io/en/latest/cmakelists.html#accessing-information
9+
project(
10+
${SKBUILD_PROJECT_NAME}
11+
VERSION ${SKBUILD_PROJECT_VERSION}
12+
LANGUAGES CXX)
313

414
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
515
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
16+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
17+
set(CMAKE_CXX_STANDARD 17)
618

719
if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
820
set(CMAKE_BUILD_TYPE
@@ -23,11 +35,16 @@ endif()
2335
include_directories(${PROJECT_SOURCE_DIR}/headers/include
2436
${PROJECT_SOURCE_DIR}/headers/include/cubao)
2537

26-
set(CMAKE_CXX_STANDARD 17)
2738
set(PYBIND11_CPP_STANDARD -std=c++17)
2839

29-
add_subdirectory(pybind11)
30-
pybind11_add_module(_pybind11_fast_crossing src/main.cpp)
40+
# https://scikit-build-core.readthedocs.io/en/latest/getting_started.html
41+
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
42+
find_package(pybind11 CONFIG REQUIRED)
43+
include_directories(headers/include)
3144

32-
target_compile_definitions(_pybind11_fast_crossing
33-
PRIVATE VERSION_INFO=${FAST_CROSSING_VERSION_INFO})
45+
file(GLOB SRCS src/*.cpp)
46+
python_add_library(_core MODULE ${SRCS} WITH_SOABI)
47+
target_link_libraries(_core PRIVATE pybind11::headers)
48+
target_include_directories(_core PRIVATE src)
49+
target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})
50+
install(TARGETS _core DESTINATION ${PROJECT_NAME})

Makefile

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PROJECT_SOURCE_DIR ?= $(abspath ./)
22
PROJECT_NAME ?= $(shell basename $(PROJECT_SOURCE_DIR))
3+
NUM_JOBS ?= 8
34

45
all:
56
@echo nothing special
@@ -14,19 +15,15 @@ lint:
1415
pre-commit run -a
1516
lint_install:
1617
pre-commit install
17-
18-
build:
19-
mkdir -p build && cd build && \
20-
cmake .. && make
21-
.PHONY: build
18+
.PHONY: lint lint_install
2219

2320
docs_build:
2421
mkdocs build
2522
docs_serve:
2623
mkdocs serve -a 0.0.0.0:8088
2724

2825
DOCKER_TAG_WINDOWS ?= ghcr.io/cubao/build-env-windows-x64:v0.0.1
29-
DOCKER_TAG_LINUX ?= ghcr.io/cubao/build-env-manylinux2014-x64:v0.0.3
26+
DOCKER_TAG_LINUX ?= ghcr.io/cubao/build-env-manylinux2014-x64:v0.0.5
3027
DOCKER_TAG_MACOS ?= ghcr.io/cubao/build-env-macos-arm64:v0.0.1
3128

3229
test_in_win:
@@ -46,28 +43,31 @@ test_in_dev_container:
4643
-v `pwd`:`pwd` -w `pwd` -it $(DEV_CONTAINER_IMAG) bash
4744

4845
PYTHON ?= python3
46+
build:
47+
$(PYTHON) -m pip install scikit_build_core pyproject_metadata pathspec pybind11
48+
CMAKE_BUILD_PARALLEL_LEVEL=$(NUM_JOBS) $(PYTHON) -m pip install --no-build-isolation -Ceditable.rebuild=true -Cbuild-dir=build -ve.
4949
python_install:
50-
$(PYTHON) setup.py install
51-
python_build:
52-
$(PYTHON) setup.py bdist_wheel
50+
$(PYTHON) -m pip install . --verbose
51+
python_wheel:
52+
$(PYTHON) -m pip wheel . -w build --verbose
5353
python_sdist:
54-
$(PYTHON) setup.py sdist
55-
# tar -tvf dist/fast_crossing-*.tar.gz
54+
$(PYTHON) -m pip sdist . --verbose
5655
python_test: pytest
5756
pytest:
58-
pytest tests --capture=tee-sys
59-
.PHONY: python_install python_build python_sdist python_test pytest
57+
python3 -m pip install pytest
58+
pytest tests/test_basic.py
59+
.PHONY: build
60+
61+
restub:
62+
pybind11-stubgen fast_crossing._core -o stubs
63+
cp -rf stubs/fast_crossing/_core src/fast_crossing
6064

61-
# conda create -y -n py36 python=3.6
62-
# conda create -y -n py37 python=3.7
6365
# conda create -y -n py38 python=3.8
6466
# conda create -y -n py39 python=3.9
6567
# conda create -y -n py310 python=3.10
68+
# conda create -y -n py311 python=3.11
69+
# conda create -y -n py312 python=3.12
6670
# conda env list
67-
python_build_py36:
68-
PYTHON=python conda run --no-capture-output -n py36 make python_build
69-
python_build_py37:
70-
PYTHON=python conda run --no-capture-output -n py37 make python_build
7171
python_build_py38:
7272
PYTHON=python conda run --no-capture-output -n py38 make python_build
7373
python_build_py39:
@@ -76,11 +76,13 @@ python_build_py310:
7676
PYTHON=python conda run --no-capture-output -n py310 make python_build
7777
python_build_py311:
7878
PYTHON=python conda run --no-capture-output -n py311 make python_build
79-
python_build_all: python_build_py36 python_build_py37 python_build_py38 python_build_py39 python_build_py310 python_build_py311
79+
python_build_py312:
80+
PYTHON=python conda run --no-capture-output -n py312 make python_build
81+
python_build_all: python_build_py38 python_build_py39 python_build_py310 python_build_py311 python_build_py312
8082
python_build_all_in_linux:
8183
docker run --rm -w `pwd` -v `pwd`:`pwd` -v `pwd`/build/linux:`pwd`/build -it $(DOCKER_TAG_LINUX) make python_build_all
8284
make repair_wheels && rm -rf dist/*.whl && mv wheelhouse/*.whl dist && rm -rf wheelhouse
83-
python_build_all_in_macos: python_build_py38 python_build_py39 python_build_py310 python_build_py311
85+
python_build_all_in_macos: python_build_py38 python_build_py39 python_build_py310 python_build_py311 python_build_py312
8486
python_build_all_in_windows: python_build_all
8587

8688
repair_wheels:

benchmarks/benchmark_point_in_polygon.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from __future__ import annotations
2+
13
import math
24
import os
35
import random
46
import time
5-
from typing import List, Tuple
67

78
import numpy as np
89
from loguru import logger
@@ -44,8 +45,7 @@ def point_in_polygon_polygons(points: np.ndarray, polygon: np.ndarray) -> np.nda
4445
num_edges_children = 4
4546
num_nodes_children = 4
4647
tree = polygons.build_search_tree(polygon, num_edges_children, num_nodes_children)
47-
mask = polygons.points_are_inside(tree, points).astype(np.int32)
48-
return mask
48+
return polygons.points_are_inside(tree, points).astype(np.int32)
4949

5050

5151
def point_in_polygon_shapely(points: np.ndarray, polygon: np.ndarray) -> np.ndarray:
@@ -68,8 +68,7 @@ def load_points(path: str):
6868

6969
def load_polygon(path: str):
7070
if path.endswith((".npy", ".pcd")):
71-
return load_points(path)
72-
pass
71+
load_points(path)
7372

7473

7574
def write_mask(mask: np.ndarray, path: str):
@@ -90,12 +89,12 @@ def wrapped_fn(input_points: str, input_polygon: str, output_path: str):
9089

9190
# https://stackoverflow.com/questions/8997099/algorithm-to-generate-random-2d-polygon
9291
def generate_polygon(
93-
center: Tuple[float, float],
92+
center: tuple[float, float],
9493
avg_radius: float,
9594
irregularity: float,
9695
spikiness: float,
9796
num_vertices: int,
98-
) -> List[Tuple[float, float]]:
97+
) -> list[tuple[float, float]]:
9998
"""
10099
Start with the center of the polygon at center, then creates the
101100
polygon by sampling points on a circle around the center.
@@ -147,7 +146,7 @@ def generate_polygon(
147146
return points
148147

149148

150-
def random_angle_steps(steps: int, irregularity: float) -> List[float]:
149+
def random_angle_steps(steps: int, irregularity: float) -> list[float]:
151150
"""Generates the division of a circumference in random angles.
152151
153152
Args:

docs/about/release-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ To upgrade `fast-crossing` to the latest version, use pip:
1010
pip install -U fast-crossing
1111
```
1212

13+
## Version 0.1.0 (2024-10-03)
14+
15+
* Add typing stubs
16+
1317
## Version 0.0.9 (2024-09-07)
1418

1519
* Update pybind11 (for python 3.11, 3.12), ditch python 3.6, 3.7

fast_crossing/__init__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

pybind11

Lines changed: 0 additions & 1 deletion
This file was deleted.

pyproject.toml

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,98 @@
11
[build-system]
2-
requires = [
3-
"setuptools>=42",
4-
"wheel",
5-
"ninja",
6-
"cmake>=3.12",
2+
requires = ["scikit-build-core>=0.3.3", "pybind11"]
3+
build-backend = "scikit_build_core.build"
4+
5+
6+
[project]
7+
name = "fast_crossing"
8+
version = "0.1.0"
9+
url = "https://fast-crossing.readthedocs.io"
10+
description="fast crossing"
11+
readme = "README.md"
12+
authors = [
13+
{ name = "district10", email = "dvorak4tzx@gmail.com" },
714
]
8-
build-backend = "setuptools.build_meta"
15+
requires-python = ">=3.7"
16+
classifiers = [
17+
"Development Status :: 4 - Beta",
18+
"License :: OSI Approved :: MIT License",
19+
"Programming Language :: Python :: 3 :: Only",
20+
"Programming Language :: Python :: 3.7",
21+
"Programming Language :: Python :: 3.8",
22+
"Programming Language :: Python :: 3.9",
23+
"Programming Language :: Python :: 3.10",
24+
"Programming Language :: Python :: 3.11",
25+
"Programming Language :: Python :: 3.12",
26+
]
27+
28+
[project.optional-dependencies]
29+
test = ["pytest", "scipy"]
30+
31+
32+
[tool.scikit-build]
33+
wheel.expand-macos-universal-tags = true
934

10-
[tool.isort]
11-
profile = "black"
1235

1336
[tool.pytest.ini_options]
1437
minversion = "6.0"
1538
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
1639
xfail_strict = true
17-
filterwarnings = ["error"]
40+
log_cli_level = "INFO"
41+
filterwarnings = [
42+
"error",
43+
]
1844
testpaths = ["tests"]
1945

46+
2047
[tool.cibuildwheel]
2148
test-command = "pytest {project}/tests"
2249
test-extras = ["test"]
2350
test-skip = ["*universal2:arm64"]
24-
# Setuptools bug causes collision between pypy and cpython artifacts
25-
before-build = "rm -rf {project}/build"
51+
build-verbosity = 1
52+
53+
54+
[tool.ruff]
55+
src = ["src"]
56+
57+
[tool.ruff.lint]
58+
exclude = ["*.pyi", "scripts/*.py"]
59+
extend-select = [
60+
"B", # flake8-bugbear
61+
"I", # isort
62+
"ARG", # flake8-unused-arguments
63+
"C4", # flake8-comprehensions
64+
"EM", # flake8-errmsg
65+
"ICN", # flake8-import-conventions
66+
"G", # flake8-logging-format
67+
"PGH", # pygrep-hooks
68+
"PIE", # flake8-pie
69+
"PL", # pylint
70+
"PT", # flake8-pytest-style
71+
"PTH", # flake8-use-pathlib
72+
"RET", # flake8-return
73+
"RUF", # Ruff-specific
74+
"SIM", # flake8-simplify
75+
"T20", # flake8-print
76+
"UP", # pyupgrade
77+
"YTT", # flake8-2020
78+
"EXE", # flake8-executable
79+
"NPY", # NumPy specific rules
80+
"PD", # pandas-vet
81+
]
82+
ignore = [
83+
"ARG002",
84+
"EM101",
85+
"NPY002",
86+
"PLR09", # Too many X
87+
"PLR2004", # Magic comparison
88+
"PT018",
89+
"PTH100",
90+
"PTH103",
91+
"PTH119",
92+
"PTH120",
93+
"RUF013",
94+
]
95+
isort.required-imports = ["from __future__ import annotations"]
96+
97+
[tool.ruff.lint.per-file-ignores]
98+
"tests/**" = ["T20"]

0 commit comments

Comments
 (0)