Skip to content

Commit

Permalink
fix: expose correct python types (#17)
Browse files Browse the repository at this point in the history
* refactor: rename CitationContext to SyntaxContext for better semantic clarity

* feat: expose Location and SyntaxContext classes in public API for better type hints
  • Loading branch information
rorybyrne authored Jan 15, 2025
1 parent 710e16f commit 8877cd6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 28 deletions.
19 changes: 17 additions & 2 deletions python/anot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import sys
from ._anot import Annotation, extract_annotations, format_annotations, run_cli

__all__ = ["Annotation", "extract_annotations", "format_annotations", "run_cli"]
from ._anot import (
Annotation,
Location,
SyntaxContext,
extract_annotations,
format_annotations,
run_cli,
)

__all__ = [
"Annotation",
"Location",
"SyntaxContext",
"extract_annotations",
"format_annotations",
"run_cli",
]


def main():
Expand Down
30 changes: 26 additions & 4 deletions python/anot/_anot.pyi
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
from pathlib import Path
from typing import List
from typing import List, Optional

__all__ = ["Annotation", "extract_annotations", "format_annotations", "run_cli"]
__all__ = [
"Annotation",
"Location",
"SyntaxContext",
"extract_annotations",
"format_annotations",
"run_cli",
]

class SyntaxContext:
node_type: str
parent_type: str
associated_name: Optional[str]
variable_name: Optional[str]
def __init__(
self,
*,
node_type: str,
parent_type: str,
associated_name: Optional[str],
variable_name: Optional[str],
) -> None: ...

class Location:
file: Path
line: int
inline: bool
def __init__(self, file: Path, line: int, inline: bool) -> None: ...
def __init__(self, *, file: Path, line: int, inline: bool) -> None: ...

class Annotation:
kind: str
content: str
location: Location
def __init__(self, kind: str, content: str) -> None: ...
contest: SyntaxContext
def __init__(self, *, kind: str, content: str, location: Location) -> None: ...

def extract_annotations(content: str, file_type: str) -> List[Annotation]: ...
def format_annotations(annotations: List[Annotation], format: str) -> str: ...
Expand Down
12 changes: 7 additions & 5 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ struct PyLocation {
inline: bool,
}

#[pyclass(name = "CitationContext")]
#[pyclass(name = "SyntaxContext")]
#[derive(Clone)]
struct PyCitationContext {
struct PySyntaxContext {
#[pyo3(get)]
node_type: String,
#[pyo3(get)]
Expand All @@ -41,13 +41,13 @@ struct PyAnnotation {
#[pyo3(get)]
location: PyLocation,
#[pyo3(get)]
context: PyCitationContext,
context: PySyntaxContext,
}

#[pymethods]
impl PyAnnotation {
#[new]
fn new(kind: String, content: String, context: PyCitationContext) -> Self {
fn new(kind: String, content: String, context: PySyntaxContext) -> Self {
Self {
kind,
content,
Expand Down Expand Up @@ -84,7 +84,7 @@ fn extract_annotations(content: &str, file_type: &str) -> PyResult<Vec<PyAnnotat
line: a.location.line,
inline: a.location.inline,
},
context: PyCitationContext {
context: PySyntaxContext {
node_type: a.context.node_type,
parent_type: a.context.parent_type,
associated_name: a.context.associated_name,
Expand Down Expand Up @@ -135,6 +135,8 @@ fn run_cli(args: Vec<String>) -> PyResult<()> {

#[pymodule]
fn _anot(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PySyntaxContext>()?;
m.add_class::<PyLocation>()?;
m.add_class::<PyAnnotation>()?;
m.add_function(wrap_pyfunction!(extract_annotations, m)?)?;
m.add_function(wrap_pyfunction!(format_annotations, m)?)?;
Expand Down
Loading

0 comments on commit 8877cd6

Please sign in to comment.