Skip to content

Commit b94e6e0

Browse files
authored
Merge pull request #199 from projectsyn/feat/python-type-stubs
Setup Python type stubs for `reclass_rs`
2 parents 941da71 + 306887f commit b94e6e0

7 files changed

Lines changed: 138 additions & 15 deletions

File tree

.github/workflows/python.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ jobs:
9494
python3 -m venv .venv
9595
source .venv/bin/activate
9696
pip install reclass-rs --find-links dist --force-reinstall
97-
pip install pytest kapicorp-reclass
97+
pip install pytest mypy kapicorp-reclass
98+
stubtest reclass_rs
99+
mypy --check-untyped-defs --ignore-missing-imports tests/
98100
pytest
99101
- name: pytest
100102
if: ${{ !startsWith(matrix.platform.target, 'x86') && matrix.platform.target != 'ppc64' }}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ dynamic = ["version"]
1414

1515

1616
[tool.maturin]
17+
python-source = "python"
1718
features = ["pyo3/extension-module"]

python/reclass_rs/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .reclass_rs import *
2+
3+
__doc__ = reclass_rs.__doc__
4+
if hasattr(reclass_rs, "__all__"):
5+
__all__ = reclass_rs.__all__

python/reclass_rs/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
reclass_rs.pyi

python/reclass_rs/py.typed

Whitespace-only changes.

python/reclass_rs/reclass_rs.pyi

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from enum import Enum
2+
from typing import Any, Optional, final
3+
from datetime import datetime
4+
5+
__all__: list[str] = [
6+
"CompatFlag",
7+
"Config",
8+
"Inventory",
9+
"NodeInfo",
10+
"NodeInfoMeta",
11+
"Reclass",
12+
"buildinfo",
13+
]
14+
15+
@final
16+
class CompatFlag(Enum):
17+
ComposeNodeNameLiteralDots = "ComposeNodeNameLiteralDots"
18+
19+
@final
20+
class Config:
21+
@property
22+
def class_mappings(self) -> list[str]: ...
23+
@property
24+
def class_mappings_match_path(self) -> bool: ...
25+
@property
26+
def classes_path(self) -> str: ...
27+
@property
28+
def compatflags(self) -> set[CompatFlag]: ...
29+
@property
30+
def compose_node_name(self) -> bool: ...
31+
@classmethod
32+
def from_dict(
33+
cls, inventory_path: str, config: dict[str, Any], verbose: bool = False
34+
) -> Config: ...
35+
@property
36+
def ignore_class_notfound(self) -> bool: ...
37+
@property
38+
def ignore_class_notfound_regexp(self) -> list[str]: ...
39+
@property
40+
def ignore_overwritten_missing_references(self) -> bool: ...
41+
@property
42+
def inventory_path(self) -> str: ...
43+
@property
44+
def nodes_path(self) -> str: ...
45+
@property
46+
def verbose_warnings(self) -> bool: ...
47+
48+
@final
49+
class NodeInfoMeta:
50+
@property
51+
def environment(self) -> str: ...
52+
@property
53+
def name(self) -> str: ...
54+
@property
55+
def node(self) -> str: ...
56+
@property
57+
def render_time(self) -> datetime: ...
58+
@property
59+
def uri(self) -> str: ...
60+
61+
@final
62+
class NodeInfo:
63+
@property
64+
def __reclass__(self) -> NodeInfoMeta: ...
65+
@property
66+
def applications(self) -> list[str]: ...
67+
def as_dict(self) -> dict[str, Any]: ...
68+
@property
69+
def classes(self) -> list[str]: ...
70+
@property
71+
def exports(self) -> dict[str, Any]: ...
72+
@property
73+
def parameters(self) -> dict[str, Any]: ...
74+
def reclass_as_dict(self) -> dict[str, Any]: ...
75+
76+
@final
77+
class Inventory:
78+
@property
79+
def applications(self) -> dict[str, list[str]]: ...
80+
def as_dict(self) -> dict[str, Any]: ...
81+
@property
82+
def classes(self) -> dict[str, list[str]]: ...
83+
@property
84+
def nodes(self) -> dict[str, NodeInfo]: ...
85+
86+
@final
87+
class Reclass:
88+
@property
89+
def config(self) -> Config: ...
90+
def __new__(
91+
cls,
92+
inventory_path: Optional[str] = ".",
93+
nodes_path: Optional[str] = None,
94+
classes_path: Optional[str] = None,
95+
ignore_class_notfound: Optional[bool] = None,
96+
): ...
97+
@classmethod
98+
def from_config_file(
99+
cls, inventory_path: str, config_file: str, verbose: bool = False
100+
) -> Reclass: ...
101+
@classmethod
102+
def from_config(cls, config: Config) -> Reclass: ...
103+
def nodeinfo(self, nodename: str) -> NodeInfo: ...
104+
def inventory(self) -> Inventory: ...
105+
@classmethod
106+
def set_thread_count(cls, count: int): ...
107+
def set_compat_flag(self, flag: CompatFlag): ...
108+
def unset_compat_flag(self, flag: CompatFlag): ...
109+
def clear_compat_flags(self): ...
110+
@property
111+
def nodes(self) -> dict[str, str]: ...
112+
@property
113+
def classes(self) -> dict[str, str]: ...
114+
def set_ignore_class_notfound_regexp(self, patterns: list[str]): ...
115+
116+
def buildinfo() -> dict[str, str]: ...

src/lib.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -413,20 +413,18 @@ fn buildinfo() -> HashMap<&'static str, &'static str> {
413413
}
414414

415415
#[pymodule]
416-
fn reclass_rs(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
417-
// Register the top-level `Reclass` Python class which is used to configure the library
418-
m.add_class::<Reclass>()?;
419-
// Register the `Config` class and `CompatFlag` enum
420-
m.add_class::<Config>()?;
421-
m.add_class::<CompatFlag>()?;
422-
// Register the NodeInfoMeta and NodeInfo classes
423-
m.add_class::<NodeInfoMeta>()?;
424-
m.add_class::<NodeInfo>()?;
425-
// Register the Inventory class
426-
m.add_class::<Inventory>()?;
427-
// Register the buildinfo method
428-
m.add_function(wrap_pyfunction!(buildinfo, m)?)?;
429-
Ok(())
416+
mod reclass_rs {
417+
#[pymodule_export]
418+
use super::Reclass;
419+
#[pymodule_export]
420+
use super::buildinfo;
421+
422+
#[pymodule_export]
423+
use crate::config::{CompatFlag, Config};
424+
#[pymodule_export]
425+
use crate::inventory::Inventory;
426+
#[pymodule_export]
427+
use crate::node::{NodeInfo, NodeInfoMeta};
430428
}
431429

432430
#[cfg(test)]

0 commit comments

Comments
 (0)