Skip to content

Commit

Permalink
Support Pydantic V2 (#265)
Browse files Browse the repository at this point in the history
* Support Pydantic V2

This updates the code to be compatible with Pydantic V2. We also
introduce a smoke test for importing `kiss_icp`. It's still just a smoke
test, but does a bit more than only calling `kiss_icp_pipeline
--version`. The latter would also pass with Pydantic V2 being installed,
while the import would fail.

Co-authored-by: pnikutta <[email protected]>

* Add make target

* Remove debug msg

The config file is in the output directory anyways

* Remove unused import

---------

Co-authored-by: pnikutta <[email protected]>
Co-authored-by: Ignacio Vizzo <[email protected]>
  • Loading branch information
3 people authored Jan 19, 2024
1 parent 3848ee9 commit 8e5b7db
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ jobs:
- name: Test installation
run: |
kiss_icp_pipeline --version
- name: Run unittests
run: |
python -m pip install --verbose './python[test]'
pytest -rA --verbose ./python/
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ editable:
@pip install scikit-build-core pyproject_metadata pathspec pybind11 ninja cmake
@pip install --no-build-isolation -ve ./python/

test:
@pytest -rA --verbose ./python/

cpp:
@cmake -Bbuild cpp/kiss_icp/
@cmake --build build -j$(nproc --all)
47 changes: 19 additions & 28 deletions python/kiss_icp/config/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pathlib import Path
from typing import Any, Dict, Optional

from pydantic import BaseSettings, PrivateAttr
from pydantic_settings import BaseSettings

from kiss_icp.config.config import AdaptiveThresholdConfig, DataConfig, MappingConfig

Expand All @@ -38,32 +38,23 @@ class KISSConfig(BaseSettings):
data: DataConfig = DataConfig()
mapping: MappingConfig = MappingConfig()
adaptive_threshold: AdaptiveThresholdConfig = AdaptiveThresholdConfig()
_config_file: Optional[Path] = PrivateAttr()

def __init__(self, config_file: Optional[Path] = None, *args, **kwargs):
self._config_file = config_file
super().__init__(*args, **kwargs)

def _yaml_source(self) -> Dict[str, Any]:
data = None
if self._config_file is not None:
try:
yaml = importlib.import_module("yaml")
except ModuleNotFoundError:
print(
"Custom configuration file specified but PyYAML is not installed on your system,"
' run `pip install "kiss-icp[all]"`. You can also modify the config.py if your '
"system does not support PyYaml "
)
sys.exit(1)
with open(self._config_file) as cfg_file:
data = yaml.safe_load(cfg_file)
return data or {}

class Config:
@classmethod
def customise_sources(cls, init_settings, env_settings, file_secret_settings):
return init_settings, KISSConfig._yaml_source


def _yaml_source(config_file: Optional[Path]) -> Dict[str, Any]:
data = None
if config_file is not None:
try:
yaml = importlib.import_module("yaml")
except ModuleNotFoundError:
print(
"Custom configuration file specified but PyYAML is not installed on your system,"
' run `pip install "kiss-icp[all]"`. You can also modify the config.py if your '
"system does not support PyYaml "
)
sys.exit(1)
with open(config_file) as cfg_file:
data = yaml.safe_load(cfg_file)
return data or {}


def load_config(
Expand All @@ -72,7 +63,7 @@ def load_config(
"""Load configuration from an Optional yaml file. Additionally, deskew and max_range can be
also specified from the CLI interface"""

config = KISSConfig(config_file=config_file)
config = KISSConfig(**_yaml_source(config_file))

# Override defaults from command line
if deskew is not None:
Expand Down
9 changes: 8 additions & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ dependencies = [
"natsort",
"numpy",
"plyfile",
"pydantic <2",
"pydantic>=2",
"pydantic-settings",
"pyquaternion",
"rich",
"tqdm",
Expand All @@ -53,6 +54,9 @@ all = [
"PyYAML",
"trimesh",
]
test = [
"pytest",
]
visualizer = [
"open3d>=0.13",
]
Expand Down Expand Up @@ -89,3 +93,6 @@ skip = ["*-musllinux*", "pp*", "cp36-*"]
[tool.cibuildwheel.macos]
environment = "MACOSX_DEPLOYMENT_TARGET=10.14"
archs = ["auto64", "arm64"]

[tool.pytest.ini_options]
testpaths = ['tests']
4 changes: 4 additions & 0 deletions python/tests/test_kiss_icp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_import():
from kiss_icp.kiss_icp import KissICP

assert KissICP is not None

0 comments on commit 8e5b7db

Please sign in to comment.