Skip to content

Commit

Permalink
Merge pull request #52 from 0Hughman0/improving-typing
Browse files Browse the repository at this point in the history
Improving typing
  • Loading branch information
0Hughman0 committed Aug 26, 2023
2 parents 43915a4 + 723d5f7 commit 7116ee0
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 273 deletions.
22 changes: 20 additions & 2 deletions cassini/__init__.py
@@ -1,7 +1,13 @@
from .defaults.tiers import DEFAULT_TIERS
from .defaults.tiers import (
DEFAULT_TIERS,
Home,
WorkPackage,
Experiment,
Sample,
DataSet,
)
from .environment import Project, env
from .core import TierBase
from cassini.defaults.tiers import Home

try:
__IPYTHON__ # type: ignore[name-defined]
Expand All @@ -10,3 +16,15 @@
register()
except NameError:
pass

__all__ = [
"Project",
"DEFAULT_TIERS",
"env",
"TierBase",
"Home",
"WorkPackage",
"Experiment",
"Sample",
"DataSet",
]
53 changes: 35 additions & 18 deletions cassini/accessors.py
Expand Up @@ -6,28 +6,45 @@
Any,
cast,
Dict,
List,
Tuple,
TypeVar,
Generic,
Optional,
Type,
overload,
ClassVar,
TYPE_CHECKING,
)
from typing_extensions import Self

if TYPE_CHECKING:
from .core import TierBase

Check warning on line 21 in cassini/accessors.py

View check run for this annotation

Codecov / codecov/patch

cassini/accessors.py#L21

Added line #L21 was not covered by tests

T = TypeVar("T")
V = TypeVar("V")


def _null_func(val):
return val
JSONPrimative = Union[str, int, float, bool, None]
JSONType = Union[dict, list, tuple, JSONPrimative]
JSONProcessor = Callable[[JSONType], T]

JOut = TypeVar(
"JOut",
Dict[JSONPrimative, JSONType],
List[JSONType],
Tuple[JSONType, ...],
str,
int,
float,
bool,
None,
)


JSONType = Union[dict, list, str, int, float, bool, None]
JSONProcessor = Callable[[JSONType], Any]
def _null_func(val: Any) -> Any:
return cast(JSONType, val)


class MetaAttr:
class MetaAttr(Generic[JOut, T]):
"""
Accessor for getting values from a Tier class's meta as an attribute.
Expand Down Expand Up @@ -58,27 +75,27 @@ class MetaAttr:

def __init__(
self,
post_get: JSONProcessor = _null_func,
pre_set: Callable[[Any], JSONType] = _null_func,
post_get: Callable[[JOut], T] = _null_func,
pre_set: Callable[[T], JOut] = _null_func,
name: Union[str, None] = None,
default: Any = None,
default: Union[T, None] = None,
):
self.name: str = cast(str, name)
self.post_get = post_get
self.pre_set = pre_set
self.post_get: Callable[[JOut], T] = post_get
self.pre_set: Callable[[T], JOut] = pre_set
self.default = default

def __set_name__(self, owner, name: str):
def __set_name__(self, owner: object, name: str) -> None:
if self.name is None:
self.name = name

def __get__(self, instance, owner):
def __get__(self, instance: "TierBase", owner: object) -> Union[T, None]:
try:
return self.post_get(instance.meta[self.name])
return self.post_get(cast(JOut, instance.meta[self.name]))
except KeyError:
return self.default

def __set__(self, instance, value):
def __set__(self, instance: "TierBase", value: T) -> None:
setattr(instance.meta, self.name, self.pre_set(value))


Expand Down Expand Up @@ -142,7 +159,7 @@ def __get__(self, instance: Optional[T], owner: Type[T]) -> Union[V, Self]:

return val

def __set__(self, instance, value):
def __set__(self, instance: Optional[T], value: Any) -> None:
raise AttributeError("Trying to set a cached property - naughty!")


Expand Down Expand Up @@ -175,7 +192,7 @@ def __get__(self, instance: T, owner: Type[T]) -> V:

return val

def __set__(self, instance, value):
def __set__(self, instance: T, value: Any) -> Any:
raise AttributeError("Trying to set a cached class property - naughty!")


Expand Down
15 changes: 12 additions & 3 deletions cassini/compat/update.py
@@ -1,9 +1,16 @@
import nbformat
import re
from typing import List, Callable, TypeVar

Check warning on line 2 in cassini/compat/update.py

View check run for this annotation

Codecov / codecov/patch

cassini/compat/update.py#L2

Added line #L2 was not covered by tests

import nbformat
from nbformat import NotebookNode

Check warning on line 5 in cassini/compat/update.py

View check run for this annotation

Codecov / codecov/patch

cassini/compat/update.py#L4-L5

Added lines #L4 - L5 were not covered by tests

CellProcessor = Callable[[NotebookNode], None]

Check warning on line 7 in cassini/compat/update.py

View check run for this annotation

Codecov / codecov/patch

cassini/compat/update.py#L7

Added line #L7 was not covered by tests


class BaseUpdater:
def cell_processor(self, f):
cell_processors: List[CellProcessor]

Check warning on line 11 in cassini/compat/update.py

View check run for this annotation

Codecov / codecov/patch

cassini/compat/update.py#L11

Added line #L11 was not covered by tests

def cell_processor(self, f: CellProcessor):

Check warning on line 13 in cassini/compat/update.py

View check run for this annotation

Codecov / codecov/patch

cassini/compat/update.py#L13

Added line #L13 was not covered by tests
self.cell_processors.append(f)
return f

Expand All @@ -12,7 +19,9 @@ class V0_1to0_2(BaseUpdater):
def __init__(self):
from .. import env

self.home = env()
assert env.project

Check warning on line 22 in cassini/compat/update.py

View check run for this annotation

Codecov / codecov/patch

cassini/compat/update.py#L22

Added line #L22 was not covered by tests

self.home = env.project.home

Check warning on line 24 in cassini/compat/update.py

View check run for this annotation

Codecov / codecov/patch

cassini/compat/update.py#L24

Added line #L24 was not covered by tests
self.cell_processors = []

@self.cell_processor
Expand Down

0 comments on commit 7116ee0

Please sign in to comment.