From 1969754237c8d6440731467c52aa1fc69258ea35 Mon Sep 17 00:00:00 2001 From: Nathaniel Starkman Date: Thu, 18 May 2023 13:36:08 -0400 Subject: [PATCH] Add HasConstantsNamespace Protocol for the presence of a constants namespace. Signed-off-by: Nathaniel Starkman --- src/cosmology/api/_core.py | 3 ++- src/cosmology/api/_namespace.py | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/cosmology/api/_core.py b/src/cosmology/api/_core.py index 768a7a1..7a58d7b 100644 --- a/src/cosmology/api/_core.py +++ b/src/cosmology/api/_core.py @@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Protocol, TypeVar, runtime_checkable from cosmology.api._array_api import Array +from cosmology.api._namespace import HasConstantsNamespace if TYPE_CHECKING: from cosmology.api._constants import CosmologyConstantsNamespace @@ -18,7 +19,7 @@ @runtime_checkable -class Cosmology(Protocol[Array, InputT]): # type: ignore[misc] +class Cosmology(HasConstantsNamespace, Protocol[Array, InputT]): # type: ignore[misc] """Cosmology API Protocol.""" @property diff --git a/src/cosmology/api/_namespace.py b/src/cosmology/api/_namespace.py index f5fad78..47a4d01 100644 --- a/src/cosmology/api/_namespace.py +++ b/src/cosmology/api/_namespace.py @@ -19,7 +19,17 @@ @runtime_checkable -class CosmologyNamespace(Protocol): +class HasConstantsNamespace(Protocol): + """Protocol for objects that have a constants module.""" + + @property + def constants(self) -> CosmologyConstantsNamespace: + """The constants namespace for this object.""" + ... + + +@runtime_checkable +class CosmologyNamespace(HasConstantsNamespace, Protocol): """Runtime-checkable Protocol for the Cosmology API namespace. Examples @@ -53,8 +63,3 @@ class CosmologyNamespace(Protocol): be a `~cosmology.api.CosmologyConstantsNamespace`, but `isinstance` will not check this. """ - - @property - def constants(self) -> CosmologyConstantsNamespace: - """The cosmology constants API.""" - ...