Skip to content

Commit

Permalink
feat: [Python] 「存在しないコンストラクタ」についてケア
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Feb 8, 2025
1 parent 7127d4a commit 48d177e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from os import PathLike
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING, NoReturn, Union
from uuid import UUID

if TYPE_CHECKING:
Expand All @@ -18,6 +18,9 @@ class VoiceModelFile:
"""
音声モデルファイル。"""

def __new__(
cls, *args: tuple[object], **kwargs: dict[object, object]
) -> NoReturn: ...
@staticmethod
async def open(path: str | PathLike[str]) -> VoiceModelFile:
"""
Expand Down Expand Up @@ -95,6 +98,9 @@ class Onnxruntime:
LIB_UNVERSIONED_FILENAME: str
""":attr:`LIB_NAME` からなる動的ライブラリのファイル名。"""

def __new__(
cls, *args: tuple[object], **kwargs: dict[object, object]
) -> NoReturn: ...
@staticmethod
def get() -> Union["Onnxruntime", None]:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from os import PathLike
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING, NoReturn, Union
from uuid import UUID

if TYPE_CHECKING:
Expand All @@ -18,6 +18,9 @@ class VoiceModelFile:
"""
音声モデルファイル。"""

def __new__(
cls, *args: tuple[object], **kwargs: dict[object, object]
) -> NoReturn: ...
@staticmethod
def open(path: str | PathLike[str]) -> VoiceModelFile:
"""
Expand Down Expand Up @@ -95,6 +98,9 @@ class Onnxruntime:
LIB_UNVERSIONED_FILENAME: str
""":attr:`LIB_NAME` からなる動的ライブラリのファイル名。"""

def __new__(
cls, *args: tuple[object], **kwargs: dict[object, object]
) -> NoReturn: ...
@staticmethod
def get() -> Union["Onnxruntime", None]:
"""
Expand Down
47 changes: 44 additions & 3 deletions crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ mod blocking {

use camino::Utf8PathBuf;
use pyo3::{
exceptions::{PyIndexError, PyValueError},
exceptions::{PyIndexError, PyTypeError, PyValueError},
pyclass, pymethods,
types::{IntoPyDict as _, PyBytes, PyDict, PyList},
types::{IntoPyDict as _, PyBytes, PyDict, PyList, PyTuple, PyType},
Py, PyAny, PyObject, PyRef, PyResult, Python,
};
use uuid::Uuid;
Expand All @@ -316,6 +316,16 @@ mod blocking {

#[pymethods]
impl VoiceModelFile {
#[new]
#[classmethod]
#[pyo3(signature = (*_args, **_kwargs))]
fn new(_cls: &PyType, _args: &PyTuple, _kwargs: Option<&PyDict>) -> PyResult<Self> {
Err(PyTypeError::new_err((
"`VoiceModelFile` does not have a normal constructor. Use \
`VoiceModelFile.load_once` to construct",
)))
}

#[staticmethod]
fn open(py: Python<'_>, path: PathBuf) -> PyResult<Self> {
let model = voicevox_core::blocking::VoiceModelFile::open(path).into_py_result(py)?;
Expand Down Expand Up @@ -384,6 +394,16 @@ mod blocking {
const LIB_UNVERSIONED_FILENAME: &'static str =
voicevox_core::blocking::Onnxruntime::LIB_UNVERSIONED_FILENAME;

#[new]
#[classmethod]
#[pyo3(signature = (*_args, **_kwargs))]
fn new(_cls: &PyType, _args: &PyTuple, _kwargs: Option<&PyDict>) -> PyResult<Self> {
Err(PyTypeError::new_err((
"`Onnxruntime` does not have a normal constructor. Use `Onnxruntime.load_once` or \
`Onnxruntime.get` to construct",
)))
}

#[staticmethod]
fn get(py: Python<'_>) -> PyResult<Option<Py<Self>>> {
let result = ONNXRUNTIME.get_or_try_init(|| {
Expand Down Expand Up @@ -883,8 +903,9 @@ mod asyncio {

use camino::Utf8PathBuf;
use pyo3::{
exceptions::PyTypeError,
pyclass, pymethods,
types::{IntoPyDict as _, PyBytes, PyDict, PyList},
types::{IntoPyDict as _, PyBytes, PyDict, PyList, PyTuple, PyType},
Py, PyAny, PyErr, PyObject, PyRef, PyResult, Python, ToPyObject as _,
};
use uuid::Uuid;
Expand All @@ -904,6 +925,16 @@ mod asyncio {

#[pymethods]
impl VoiceModelFile {
#[new]
#[classmethod]
#[pyo3(signature = (*_args, **_kwargs))]
fn new(_cls: &PyType, _args: &PyTuple, _kwargs: Option<&PyDict>) -> PyResult<Self> {
Err(PyTypeError::new_err((
"`VoiceModelFile` does not have a normal constructor. Use \
`VoiceModelFile.load_once` to construct",
)))
}

#[staticmethod]
fn open(py: Python<'_>, path: PathBuf) -> PyResult<&PyAny> {
pyo3_asyncio::tokio::future_into_py(py, async move {
Expand Down Expand Up @@ -986,6 +1017,16 @@ mod asyncio {
const LIB_UNVERSIONED_FILENAME: &'static str =
voicevox_core::nonblocking::Onnxruntime::LIB_UNVERSIONED_FILENAME;

#[new]
#[classmethod]
#[pyo3(signature = (*_args, **_kwargs))]
fn new(_cls: &PyType, _args: &PyTuple, _kwargs: Option<&PyDict>) -> PyResult<Self> {
Err(PyTypeError::new_err((
"`Onnxruntime` does not have a normal constructor. Use `Onnxruntime.load_once` or \
`Onnxruntime.get` to construct",
)))
}

#[staticmethod]
fn get(py: Python<'_>) -> PyResult<Option<Py<Self>>> {
let result =
Expand Down

0 comments on commit 48d177e

Please sign in to comment.