Skip to content

Commit

Permalink
typing fixes; updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kantholtz committed Jun 5, 2024
1 parent 1134b6f commit 02858f7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- run: poetry install
- run: echo "$(poetry env info --path)/bin" >> $GITHUB_PATH

- uses: jakebailey/pyright-action@v2
- run: poetry run pyright src
- run: poetry run coverage run -m pytest
- run: poetry run coverage report

Expand Down
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ Kantholtz' personal Python toolbox. Check out the [documentation here](https://k

## Installation

Python 3.9 is required.
Python 3.11 is required.

``` console
$ conda create -n ktz python=3.9
$ conda activate ktz
$ pip install ktz
```console
pip install ktz
```


For a local installation with all dev dependencies:

``` console
$ git clone https://github.com/kantholtz/ktz.git
$ cd ktz
$ conda create -n ktz python=3.9
$ conda create -n ktz python=3.11
$ conda activate ktz
$ pip install .[dev]
$ poetry install --with dev

# run tests
$ pytest
$ poetry run pytest

# to continually run tests
$ ptw -c
$ poetry run ptw -c

# static typing
$ poetry run pyright src

# to check code coverage
$ coverage run -m pytest
$ coverage report
$ coverage html
$ poetry run coverage run -m pytest
$ poetry run coverage report
$ poetry run coverage html

# build package
$ poetry build
```
31 changes: 30 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pytest = "^8.2.1"
pytest-cov = "^5.0.0"
pytest-watch = "^4.2.0"
pytest-pudb = "^0.7.0"
pyright = "^1.1.365"

[build-system]
requires = ["poetry-core"]
Expand Down
7 changes: 4 additions & 3 deletions src/ktz/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def buckets(
def buckets(
col: Collection[A],
key: Callable[[Index, A], tuple[C, D]],
mapper: None = ...,
) -> dict[C, D]:
...

Expand All @@ -50,8 +51,8 @@ def buckets(
@overload
def buckets(
col: Collection[tuple[A, B]],
key: None,
mapper: Callable[[tuple[C, ...]], D],
key: None = ...,
mapper: Callable[[tuple[C, ...]], D] = ...,
) -> dict[A, D]:
...

Expand Down Expand Up @@ -510,7 +511,7 @@ def _dconv(dic: dict, fns):
return res


def dconv(dic: dict, *convert: Callable[[A, B], C]):
def dconv(dic: dict, *convert: Callable[[A], C] | Callable[[A, B], C]):
"""
Convert a dictionary deeply.
Expand Down
26 changes: 20 additions & 6 deletions src/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import tempfile
from contextlib import ExitStack
from typing import Any, Callable, Iterable

import pytest
import yaml
Expand Down Expand Up @@ -51,11 +52,17 @@ def test_buckets_index(self):
def test_buckets_mapper(self):
ref = {1: 9, 5: 13}
ret = buckets(
((1, 2), (1, 3), (1, 4), (5, 6), (5, 7)),
col=((1, 2), (1, 3), (1, 4), (5, 6), (5, 7)),
mapper=sum,
)
assert ref == ret

def f(self, y: Callable[[Iterable[Any]], int], *args) -> int:
return y(*args)

def g(self):
return self.f(sum, 1, 2, 3)

#
# with key and with mapper
#
Expand Down Expand Up @@ -382,7 +389,7 @@ def plus1(v):
assert d2 == dict(a=2, b=3, c=[])

def test_key(self):
def fn(v, k):
def fn(_, k):
if k == "foo":
return True
return False
Expand Down Expand Up @@ -428,8 +435,10 @@ def test_multi(self):
yaml.dump(d2, fd2)

ret = ryaml(fd1.name, fd2.name)
assert ret == dict(foo=3, bar=2, xyz=4)
return

assert ret == dict(foo=3, bar=2, xyz=4)
assert False

def test_multi_deep(self):
d1 = dict(foo=dict(a=1, b=2), bar=2)
Expand All @@ -443,8 +452,10 @@ def test_multi_deep(self):
yaml.dump(d2, fd2)

ret = ryaml(fd1.name, fd2.name)
assert ret == dict(foo=dict(a=3, b=2, c=4), bar=2, xyz=5)
return

assert ret == dict(foo=dict(a=3, b=2, c=4), bar=2, xyz=5)
assert False

def test_overwrites(self):
d1 = dict(foo=1, bar=2)
Expand All @@ -458,8 +469,10 @@ def test_overwrites(self):
yaml.dump(d2, fd2)

ret = ryaml(fd1.name, fd2.name, xyz=5)
assert ret == dict(foo=3, bar=2, xyz=5)
return

assert ret == dict(foo=3, bar=2, xyz=5)
assert False

def test_overwrites_deep(self):
d1 = dict(foo=1, bar=2)
Expand All @@ -473,5 +486,6 @@ def test_overwrites_deep(self):
yaml.dump(d2, fd2)

ret = ryaml(fd1.name, fd2.name, xyz=dict(a=3, c=3))
assert ret == dict(foo=3, bar=2, xyz=dict(a=3, b=2, c=3))

assert ret == dict(foo=3, bar=2, xyz=dict(a=3, b=2, c=3))
assert False
7 changes: 3 additions & 4 deletions src/tests/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pathlib import Path

import pytest

from ktz import functools as ft


Expand All @@ -26,7 +25,7 @@ def test_single_function(self, cachedir):
outside = 1

@run.cache("x")
def foo():
def foo(): # type: ignore
return outside

# from function execution
Expand Down Expand Up @@ -185,7 +184,7 @@ def test_get(self, cachedir):
run = ft.Cascade(prefix=cachedir, x="x")

@run.cache("x")
def f():
def f(): # type: ignore
return outside

# from computed value
Expand All @@ -201,7 +200,7 @@ def f(x):
return outside # pragma: no cover

# from cache
assert f() == 1
assert f() == 1 # type: ignore
assert run.get("x") == 1

def test_get_raises(self, cachedir):
Expand Down

0 comments on commit 02858f7

Please sign in to comment.