Skip to content

Commit

Permalink
Merge pull request #184 from 0Hughman0/overloading-meta-attr
Browse files Browse the repository at this point in the history
Overloading MetaAttr to improve type annotation.
  • Loading branch information
0Hughman0 authored Oct 14, 2024
2 parents 2d202dc + 7580da5 commit 98893d9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
- name: Typecheck with mypy
run: |
poetry run mypy cassini
poetry run mypy tests --exclude=tests/extensions/cassini_lib/mock_libraries/ --exclude=tests/project_cases
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
Expand Down
2 changes: 1 addition & 1 deletion binder/cas_project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cassini import Project, DEFAULT_TIERS
from cassini import Project, DEFAULT_TIERS, WorkPackage

project = Project(DEFAULT_TIERS, __file__)

Expand Down
11 changes: 9 additions & 2 deletions cassini/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
Generic,
KeysView,
List,
Optional,
Mapping,
overload,
TypeVar,
Union,
Literal,
Expand Down Expand Up @@ -288,6 +287,14 @@ def __set_name__(self, owner: object, name: str) -> None:
if self.name is None:
self.name = name

@overload
def __get__(self, instance: None, owner: object) -> Self:
pass

@overload
def __get__(self, instance: object, owner: object) -> AttrType:
pass

def __get__(
self, instance: Union[Any, None], owner: object
) -> Union[AttrType, Self]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cassini"
version = "0.3.0a12"
version = "0.3.0a13"
description = "A tool to structure experimental work, data and analysis using Jupyter Lab."
authors = ["0Hughman0 <rammers2@hotmail.co.uk>"]
license = "GPL-3.0-only"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_highlights.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest # type: ignore[import]

from cassini import DEFAULT_TIERS
from cassini import DEFAULT_TIERS, Project
from cassini.testing_utils import get_Project, patch_project

@pytest.fixture
Expand All @@ -26,8 +26,8 @@ def mk_project(get_Project, tmp_path):
return project


def test_add_highlight(mk_project) -> None:
project: Project = mk_project
def test_add_highlight(mk_project):
project = mk_project

wp = project['WP1']

Expand Down
16 changes: 16 additions & 0 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pathlib
import datetime

from typing_extensions import assert_type

import pytest # type: ignore[import]
from cassini import HomeTierBase, NotebookTierBase
from cassini.meta import MetaAttr, Meta, MetaCache, MetaValidationError
Expand Down Expand Up @@ -134,6 +136,20 @@ def __init__(self):
assert obj.with_default == 'squid'


# this test has to be run with mypy... which is confusing.
def test_meta_attr_inferred_types(tmp_path) -> None:
class MyClass:
a_str = MetaAttr(str, str)

def __init__(self):
self.meta = Meta.create_meta(tmp_path / 'meta.json', owner=self)

m = MyClass()

assert_type(m.a_str, str)
assert_type(MyClass.a_str, MetaAttr[str, str])


def test_jsonable(mk_meta):
meta = mk_meta

Expand Down

0 comments on commit 98893d9

Please sign in to comment.