From 27f93e5e091422e35697e67f42296198975e95a3 Mon Sep 17 00:00:00 2001 From: JHM Darbyshire <24256554+attack68@users.noreply.github.com> Date: Tue, 10 Dec 2024 16:50:09 +0100 Subject: [PATCH] TYP: dual typing fixes (#543) Co-authored-by: JHM Darbyshire (win11) --- python/rateslib/dual/__init__.py | 7 +++++-- python/rateslib/rs.pyi | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/python/rateslib/dual/__init__.py b/python/rateslib/dual/__init__.py index 0c042a78..b105670a 100644 --- a/python/rateslib/dual/__init__.py +++ b/python/rateslib/dual/__init__.py @@ -131,13 +131,16 @@ def gradient( return dual.dual elif vars is not None and not keep_manifold: return dual.grad1(vars) - - _ = dual.grad1_manifold(vars) + elif isinstance(dual, Dual): # and keep_manifold: + raise TypeError("Dual type cannot perform `keep_manifold`.") + _ = dual.grad1_manifold(dual.vars if vars is None else vars) return np.asarray(_) elif order == 2: if isinstance(dual, Variable): dual = Dual2(dual.real, vars=dual.vars, dual=dual.dual, dual2=[]) + elif isinstance(dual, Dual): + raise TypeError("Dual type cannot derive second order automatic derivatives.") if vars is None: return 2.0 * dual.dual2 diff --git a/python/rateslib/rs.pyi b/python/rateslib/rs.pyi index abf5982d..8037e1a3 100644 --- a/python/rateslib/rs.pyi +++ b/python/rateslib/rs.pyi @@ -189,7 +189,7 @@ class Dual2: dual2: list[float] | Arr1dF64, ) -> Dual2: ... def grad1(self, vars: Sequence[str]) -> Arr1dF64: ... - def gra1_manifold(self, vars: Sequence[str]) -> list[Dual2]: ... + def grad1_manifold(self, vars: Sequence[str]) -> list[Dual2]: ... def grad2(self, vars: list[str]) -> Arr2dF64: ... def ptr_eq(self, other: Dual2) -> bool: ... def __repr__(self) -> str: ...