Skip to content

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Oct 16, 2024
1 parent f860dbc commit 539622e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
8 changes: 5 additions & 3 deletions spinn_utilities/abstract_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""
A trimmed down version of standard Python Abstract Base classes.
"""
from typing import TypeVar
from typing import Any, cast, Dict, Type, TypeVar, Tuple
#: :meta private:
T = TypeVar("T")

Expand Down Expand Up @@ -58,7 +58,8 @@ def my_abstract_method(self, ...):
...
"""

def __new__(mcs, name, bases, namespace, **kwargs):
def __new__(mcs, name: str, bases: Tuple[Type, ...],
namespace: Dict[str, Any], **kwargs: Any) -> "AbstractBase":
# Actually make the class
abs_cls = super().__new__(mcs, name, bases, namespace, **kwargs)

Expand All @@ -74,5 +75,6 @@ def __new__(mcs, name, bases, namespace, **kwargs):
abstracts.add(nm)

# Lock down the set
abs_cls.__abstractmethods__ = frozenset(abstracts)
abs_cls.__abstractmethods__ = frozenset( # type: ignore[attr-defined]
abstracts)
return abs_cls
8 changes: 5 additions & 3 deletions spinn_utilities/classproperty.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Optional, Type

class _ClassPropertyDescriptor(object):
"""
A class to handle the management of class properties.
"""

def __init__(self, fget):
def __init__(self, fget: classmethod) -> None:
self.fget = fget

def __get__(self, obj, klass=None):
def __get__(
self, obj: Optional[Any], klass: Optional[Type] = None) -> Any:
if klass is None:
klass = type(obj)
return self.fget.__get__(obj, klass)()


def classproperty(func):
def classproperty(func: classmethod) -> _ClassPropertyDescriptor:
"""
Defines a property at the class-level.
Expand Down
27 changes: 19 additions & 8 deletions spinn_utilities/testing/log_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from logging import LogRecord
from typing import List

_WRITE_LOGS_TO_STDOUT = True


def _assert_logs_contains(level, log_records, submessage):
def _assert_logs_contains(
level: str, log_records: List[LogRecord], submessage: str) -> None:
for record in log_records:
if record.levelname == level and submessage in record.getMessage():
return
Expand All @@ -25,7 +29,8 @@ def _assert_logs_contains(level, log_records, submessage):
raise AssertionError(f"\"{submessage}\" not found in any {level} logs")


def _assert_logs_not_contains(level, log_records, submessage):
def _assert_logs_not_contains(
level: str, log_records: List[LogRecord], submessage: str) -> None:
for record in log_records:
if _WRITE_LOGS_TO_STDOUT: # pragma: no cover
print(record)
Expand All @@ -34,7 +39,8 @@ def _assert_logs_not_contains(level, log_records, submessage):
f"\"{submessage}\" found in any {level} logs")


def assert_logs_contains_once(level, log_records, message):
def assert_logs_contains_once(
level: int, log_records: List[LogRecord], message: str) -> None:
"""
Checks if the log records contain exactly one record at the given
level with the given sub-message.
Expand Down Expand Up @@ -65,7 +71,8 @@ def assert_logs_contains_once(level, log_records, message):
raise AssertionError(f"\"{message}\" not found in any {level} logs")


def assert_logs_error_contains(log_records, submessage):
def assert_logs_error_contains(
log_records: List[LogRecord], submessage: str) -> None:
"""
Checks it the log records contain an ERROR log with this sub-message
Expand All @@ -80,7 +87,8 @@ def assert_logs_error_contains(log_records, submessage):
_assert_logs_contains('ERROR', log_records, submessage)


def assert_logs_warning_contains(log_records, submessage):
def assert_logs_warning_contains(
log_records: List[LogRecord], submessage: str) -> None:
"""
Checks it the log records contain an WARNING log with this sub-message
Expand All @@ -95,7 +103,8 @@ def assert_logs_warning_contains(log_records, submessage):
_assert_logs_contains('WARNING', log_records, submessage)


def assert_logs_info_contains(log_records, sub_message):
def assert_logs_info_contains(
log_records: List[LogRecord], sub_message: str) -> None:
"""
Checks it the log records contain an INFO log with this sub-message
Expand All @@ -110,7 +119,8 @@ def assert_logs_info_contains(log_records, sub_message):
_assert_logs_contains('INFO', log_records, sub_message)


def assert_logs_error_not_contains(log_records, submessage):
def assert_logs_error_not_contains(
log_records: List[LogRecord], submessage: str) -> None:
"""
Checks it the log records do not contain an ERROR log with this
sub-message.
Expand All @@ -126,7 +136,8 @@ def assert_logs_error_not_contains(log_records, submessage):
_assert_logs_not_contains('ERROR', log_records, submessage)


def assert_logs_info_not_contains(log_records, submessage):
def assert_logs_info_not_contains(
log_records: List[LogRecord], submessage: str) -> None:
"""
Checks it the log records do not contain an INFO log with this
sub-message.
Expand Down

0 comments on commit 539622e

Please sign in to comment.