Skip to content

Commit

Permalink
Improve typing for decorator tests (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 7, 2024
1 parent 416a3b2 commit 35f6c28
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 59 deletions.
61 changes: 33 additions & 28 deletions tests/test_cached_property.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
from operator import not_
from types import ModuleType
from typing import Protocol, Type

import pytest

from propcache.api import cached_property

def test_cached_property(propcache_module: ModuleType) -> None:

class APIProtocol(Protocol):

cached_property: Type[cached_property]


def test_cached_property(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
self._cache = {}
def __init__(self) -> None:
"""Init."""

@propcache_module.cached_property
def prop(self):
def prop(self) -> int:
return 1

a = A()
assert a.prop == 1


def test_cached_property_class(propcache_module: ModuleType) -> None:
def test_cached_property_class(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
def __init__(self) -> None:
"""Init."""
# self._cache not set because its never accessed in this test

@propcache_module.cached_property
def prop(self):
def prop(self) -> None:
"""Docstring."""

assert isinstance(A.prop, propcache_module.cached_property)
assert A.prop.__doc__ == "Docstring."


def test_cached_property_without_cache(propcache_module: ModuleType) -> None:
def test_cached_property_without_cache(propcache_module: APIProtocol) -> None:
class A:

__slots__ = ()

def __init__(self):
def __init__(self) -> None:
pass

@propcache_module.cached_property
def prop(self):
def prop(self) -> None:
"""Mock property."""

a = A()
Expand All @@ -49,59 +56,57 @@ def prop(self):
a.prop = 123


def test_cached_property_check_without_cache(propcache_module: ModuleType) -> None:
def test_cached_property_check_without_cache(propcache_module: APIProtocol) -> None:
class A:

__slots__ = ()

def __init__(self):
pass
def __init__(self) -> None:
"""Init."""

@propcache_module.cached_property
def prop(self):
def prop(self) -> None:
"""Mock property."""

a = A()
with pytest.raises((TypeError, AttributeError)):
assert a.prop == 1


def test_cached_property_caching(propcache_module: ModuleType) -> None:

def test_cached_property_caching(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
self._cache = {}
def __init__(self) -> None:
"""Init."""

@propcache_module.cached_property
def prop(self):
def prop(self) -> int:
"""Docstring."""
return 1

a = A()
assert 1 == a.prop

assert a.prop == 1

def test_cached_property_class_docstring(propcache_module: ModuleType) -> None:

def test_cached_property_class_docstring(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
def __init__(self) -> None:
"""Init."""

@propcache_module.cached_property
def prop(self):
def prop(self) -> None:
"""Docstring."""

assert isinstance(A.prop, propcache_module.cached_property)
assert "Docstring." == A.prop.__doc__


def test_set_name(propcache_module: ModuleType) -> None:
def test_set_name(propcache_module: APIProtocol) -> None:
"""Test that the __set_name__ method is called and checked."""

class A:

@propcache_module.cached_property
def prop(self):
def prop(self) -> None:
"""Docstring."""

A.prop.__set_name__(A, "prop")
Expand All @@ -111,7 +116,7 @@ def prop(self):
A.prop.__set_name__(A, "something_else")


def test_get_without_set_name(propcache_module: ModuleType) -> None:
def test_get_without_set_name(propcache_module: APIProtocol) -> None:
"""Test that get without __set_name__ fails."""
cp = propcache_module.cached_property(not_)

Expand Down
70 changes: 39 additions & 31 deletions tests/test_under_cached_property.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
from types import ModuleType
from typing import Any, Dict, Protocol, Type

import pytest

from propcache.api import under_cached_property

def test_under_cached_property(propcache_module: ModuleType) -> None:

class APIProtocol(Protocol):

under_cached_property: Type[under_cached_property]


def test_under_cached_property(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
self._cache = {}
def __init__(self) -> None:
self._cache: Dict[str, int] = {}

@propcache_module.under_cached_property
def prop(self):
def prop(self) -> int:
return 1

a = A()
assert a.prop == 1


def test_under_cached_property_class(propcache_module: ModuleType) -> None:
def test_under_cached_property_class(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
def __init__(self) -> None:
"""Init."""
# self._cache not set because its never accessed in this test

@propcache_module.under_cached_property
def prop(self):
def prop(self) -> None:
"""Docstring."""

assert isinstance(A.prop, propcache_module.under_cached_property)
assert A.prop.__doc__ == "Docstring."


def test_under_cached_property_assignment(propcache_module: ModuleType) -> None:
def test_under_cached_property_assignment(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
self._cache = {}
def __init__(self) -> None:
self._cache: Dict[str, Any] = {}

@propcache_module.under_cached_property
def prop(self):
def prop(self) -> None:
"""Mock property."""

a = A()
Expand All @@ -45,13 +51,14 @@ def prop(self):
a.prop = 123


def test_under_cached_property_without_cache(propcache_module: ModuleType) -> None:
def test_under_cached_property_without_cache(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
pass
def __init__(self) -> None:
"""Init."""
self._cache: Dict[str, int] = {}

@propcache_module.under_cached_property
def prop(self):
def prop(self) -> None:
"""Mock property."""

a = A()
Expand All @@ -61,43 +68,44 @@ def prop(self):


def test_under_cached_property_check_without_cache(
propcache_module: ModuleType,
propcache_module: APIProtocol,
) -> None:
class A:
def __init__(self):
pass
def __init__(self) -> None:
"""Init."""
# Note that self._cache is intentionally missing
# here to verify AttributeError

@propcache_module.under_cached_property
def prop(self):
def prop(self) -> None:
"""Mock property."""

a = A()
with pytest.raises(AttributeError):
assert a.prop == 1
_ = a.prop # type: ignore[call-overload]


def test_under_cached_property_caching(propcache_module: ModuleType) -> None:

def test_under_cached_property_caching(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
self._cache = {}
def __init__(self) -> None:
self._cache: Dict[str, int] = {}

@propcache_module.under_cached_property
def prop(self):
def prop(self) -> int:
"""Docstring."""
return 1

a = A()
assert 1 == a.prop
assert a.prop == 1


def test_under_cached_property_class_docstring(propcache_module: ModuleType) -> None:
def test_under_cached_property_class_docstring(propcache_module: APIProtocol) -> None:
class A:
def __init__(self):
def __init__(self) -> None:
"""Init."""

@propcache_module.under_cached_property
def prop(self):
def prop(self) -> Any:
"""Docstring."""

assert isinstance(A.prop, propcache_module.under_cached_property)
Expand Down

0 comments on commit 35f6c28

Please sign in to comment.