Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix from __future__ import annotations behaviour #58

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Cachew gives the best of two worlds and makes it both **easy and efficient**. Th
- first your objects get [converted](src/cachew/marshall/cachew.py#L34) into a simpler JSON-like representation
- after that, they are mapped into byte blobs via [`orjson`](https://github.com/ijl/orjson).

When the function is called, cachew [computes the hash of your function's arguments ](src/cachew/__init__.py:#L511)
When the function is called, cachew [computes the hash of your function's arguments ](src/cachew/__init__.py:#L589)
and compares it against the previously stored hash value.

- If they match, it would deserialize and yield whatever is stored in the cache database
Expand All @@ -145,7 +145,7 @@ and compares it against the previously stored hash value.

* primitive: `str`, `int`, `float`, `bool`, `datetime`, `date`, `Exception`

See [tests.test_types](src/cachew/tests/test_cachew.py#L697), [tests.test_primitive](src/cachew/tests/test_cachew.py#L731), [tests.test_dates](src/cachew/tests/test_cachew.py#L651), [tests.test_exceptions](src/cachew/tests/test_cachew.py#L1101)
See [tests.test_types](src/cachew/tests/test_cachew.py#L697), [tests.test_primitive](src/cachew/tests/test_cachew.py#L731), [tests.test_dates](src/cachew/tests/test_cachew.py#L651), [tests.test_exceptions](src/cachew/tests/test_cachew.py#L1127)
* [@dataclass and NamedTuple](src/cachew/tests/test_cachew.py#L613)
* [Optional](src/cachew/tests/test_cachew.py#L515) types
* [Union](src/cachew/tests/test_cachew.py#L837) types
Expand All @@ -165,7 +165,7 @@ You can find some of my performance tests in [benchmarks/](benchmarks) dir, and


# Using
See [docstring](src/cachew/__init__.py#L290) for up-to-date documentation on parameters and return types.
See [docstring](src/cachew/__init__.py#L296) for up-to-date documentation on parameters and return types.
You can also use [extensive unit tests](src/cachew/tests/test_cachew.py) as a reference.

Some useful (but optional) arguments of `@cachew` decorator:
Expand Down Expand Up @@ -271,7 +271,7 @@ Now you can use `@mcachew` in place of `@cachew`, and be certain things don't br
## Settings


[cachew.settings](src/cachew/__init__.py#L66) exposes some parameters that allow you to control `cachew` behaviour:
[cachew.settings](src/cachew/__init__.py#L67) exposes some parameters that allow you to control `cachew` behaviour:
- `ENABLE`: set to `False` if you want to disable caching for without removing the decorators (useful for testing and debugging).
You can also use [cachew.extra.disabled_cachew](src/cachew/extra.py#L21) context manager to do it temporarily.
- `DEFAULT_CACHEW_DIR`: override to set a different base directory. The default is the "user cache directory" (see [appdirs docs](https://github.com/ActiveState/appdirs#some-example-output)).
Expand Down
11 changes: 8 additions & 3 deletions src/cachew/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def orjson_dumps(*args, **kwargs): # type: ignore[misc]
from .backend.file import FileBackend
from .backend.sqlite import SqliteBackend
from .common import SourceHash
from .logging_helper import makeLogger
from .logging_helper import make_logger
from .marshall.cachew import CachewMarshall, build_schema
from .utils import (
CachewException,
Expand Down Expand Up @@ -85,7 +85,7 @@ class settings:


def get_logger() -> logging.Logger:
return makeLogger(__name__)
return make_logger(__name__)


BACKENDS: Dict[Backend, Type[AbstractBackend]] = {
Expand Down Expand Up @@ -213,7 +213,12 @@ def infer_return_type(func) -> Union[Failure, Inferred]:
>>> infer_return_type(unsupported_list)
"can't infer type from typing.List[cachew.Custom]: can't cache <class 'cachew.Custom'>"
"""
hints = get_type_hints(func)
try:
hints = get_type_hints(func)
except Exception as ne:
# get_type_hints might fail if types are forward defined or missing
# see test_future_annotation for an example
return str(ne)
rtype = hints.get('return', None)
if rtype is None:
return f"no return type annotation on {func}"
Expand Down
36 changes: 20 additions & 16 deletions src/cachew/logging_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def test() -> None:
M("\n Also exception logging is kinda lame, doesn't print traceback by default unless you remember to pass exc_info:")
l.exception(ex) # type: ignore[possibly-undefined] # pylint: disable=used-before-assignment

M("\n\n With makeLogger you get a reasonable logging format, colours (via colorlog library) and other neat things:")
M("\n\n With make_logger you get a reasonable logging format, colours (via colorlog library) and other neat things:")

ll = makeLogger('test') # No need for basicConfig!
ll = make_logger('test') # No need for basicConfig!
ll.info("default level is INFO")
ll.debug("... so this shouldn't be displayed")
ll.warning("warnings are easy to spot!")
Expand Down Expand Up @@ -105,6 +105,14 @@ def setup_logger(logger: str | logging.Logger, *, level: LevelIsh = None) -> Non
# if it's already set, the user requested a different logging level, let's respect that
logger.setLevel(lvl)

_setup_handlers_and_formatters(name=logger.name)


# cached since this should only be done once per logger instance
@lru_cache(None)
def _setup_handlers_and_formatters(name: str) -> None:
logger = logging.getLogger(name)

logger.addFilter(AddExceptionTraceback())

ch = logging.StreamHandler()
Expand All @@ -128,10 +136,12 @@ def setup_logger(logger: str | logging.Logger, *, level: LevelIsh = None) -> Non
else:
# log_color/reset are specific to colorlog
FORMAT_COLOR = FORMAT.format(start='%(log_color)s', end='%(reset)s')
fmt = FORMAT_COLOR if ch.stream.isatty() else FORMAT_NOCOLOR
# colorlog should detect tty in principle, but doesn't handle everything for some reason
# see https://github.com/borntyping/python-colorlog/issues/71
formatter = colorlog.ColoredFormatter(fmt)
if ch.stream.isatty():
formatter = colorlog.ColoredFormatter(FORMAT_COLOR)
else:
formatter = logging.Formatter(FORMAT_NOCOLOR)

ch.setFormatter(formatter)

Expand All @@ -142,16 +152,13 @@ def setup_logger(logger: str | logging.Logger, *, level: LevelIsh = None) -> Non
# todo also amend by post about defensive error handling?
class AddExceptionTraceback(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
s = super().filter(record)
if s is False:
return False
if record.levelname == 'ERROR':
exc = record.msg
if isinstance(exc, BaseException):
if record.exc_info is None or record.exc_info == (None, None, None):
exc_info = (type(exc), exc, exc.__traceback__)
record.exc_info = exc_info
return s
return True


# todo also save full log in a file?
Expand Down Expand Up @@ -189,8 +196,7 @@ def emit(self, record: logging.LogRecord) -> None:
self.handleError(record)


@lru_cache(None) # cache so it's only initialized once
def makeLogger(name: str, *, level: LevelIsh = None) -> logging.Logger:
def make_logger(name: str, *, level: LevelIsh = None) -> logging.Logger:
logger = logging.getLogger(name)
setup_logger(logger, level=level)
return logger
Expand All @@ -201,16 +207,14 @@ def makeLogger(name: str, *, level: LevelIsh = None) -> logging.Logger:
# OK, when stdout is not a tty, enlighten doesn't log anything, good
def get_enlighten():
# TODO could add env variable to disable enlighten for a module?
from unittest.mock import Mock

# Mock to return stub so cients don't have to think about it
from unittest.mock import Mock # Mock to return stub so cients don't have to think about it

# for now hidden behind the flag since it's a little experimental
if os.environ.get('ENLIGHTEN_ENABLE', None) is None:
return Mock()

try:
import enlighten # type: ignore[import]
import enlighten # type: ignore[import-untyped]
except ModuleNotFoundError:
warnings.warn("You might want to 'pip install enlighten' for a nice progress bar")

Expand All @@ -230,6 +234,6 @@ def get_enlighten():


## legacy/deprecated methods for backwards compatilibity
LazyLogger = makeLogger
logger = makeLogger
LazyLogger = make_logger
logger = make_logger
##
26 changes: 26 additions & 0 deletions src/cachew/tests/test_cachew.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,32 @@ def orig2():
assert list(fun()) == [123]


@pytest.mark.parametrize('throw', [False, True])
def test_future_annotations(tmp_path: Path, throw: bool) -> None:
"""
this will work in runtime without cachew if from __future__ import annotations is used
so should work with cachew decorator as well
"""
src = tmp_path / 'src.py'
src.write_text(f'''
from __future__ import annotations

from cachew import settings, cachew
settings.THROW_ON_ERROR = {throw}

@cachew
def fun() -> BadType:
print("called!")
return 0

fun()
'''.lstrip())

ctx = pytest.raises(Exception) if throw else nullcontext()
with ctx:
assert check_output([sys.executable, src], text=True).strip() == "called!"


def test_recursive_simple(tmp_path: Path) -> None:
d0 = 0
d1 = 1000
Expand Down