Skip to content

Commit

Permalink
Fix type hints.
Browse files Browse the repository at this point in the history
For the model backend stub, we know that the type won't match, but we don't really care. We could make an abc that both the stub and the real class could inherit from, or we could subclass for the stub, but those seem overkill for this specific use case. It seems unlikely that type hints would show any real-life issues here.
  • Loading branch information
tonyandrewmeyer committed Sep 22, 2023
1 parent c4e3266 commit df3c2cc
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions test/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io
import logging
import typing
import unittest
from unittest.mock import patch

Expand All @@ -26,15 +27,15 @@
class FakeModelBackend:

def __init__(self):
self._calls = []
self._calls : typing.List[typing.Tuple[str, str]] = []

def calls(self, clear=False):
def calls(self, clear: bool = False):
calls = self._calls
if clear:
self._calls = []
return calls

def juju_log(self, level, message):
def juju_log(self, level: str, message: str):
for line in _ModelBackend.log_split(message):
self._calls.append((level, line))

Expand All @@ -48,7 +49,7 @@ def tearDown(self):
logging.getLogger().handlers.clear()

def test_default_logging(self):
ops.log.setup_root_logging(self.backend)
ops.log.setup_root_logging(self.backend) # type: ignore

logger = logging.getLogger()
self.assertEqual(logger.level, logging.DEBUG)
Expand All @@ -71,7 +72,7 @@ def test_default_logging(self):
def test_handler_filtering(self):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(ops.log.JujuLogHandler(self.backend, logging.WARNING))
logger.addHandler(ops.log.JujuLogHandler(self.backend, logging.WARNING)) # type: ignore
logger.info('foo')
self.assertEqual(self.backend.calls(), [])
logger.warning('bar')
Expand All @@ -80,7 +81,7 @@ def test_handler_filtering(self):
def test_no_stderr_without_debug(self):
buffer = io.StringIO()
with patch('sys.stderr', buffer):
ops.log.setup_root_logging(self.backend, debug=False)
ops.log.setup_root_logging(self.backend, debug=False) # type: ignore
logger = logging.getLogger()
logger.debug('debug message')
logger.info('info message')
Expand All @@ -98,7 +99,7 @@ def test_no_stderr_without_debug(self):
def test_debug_logging(self):
buffer = io.StringIO()
with patch('sys.stderr', buffer):
ops.log.setup_root_logging(self.backend, debug=True)
ops.log.setup_root_logging(self.backend, debug=True) # type: ignore
logger = logging.getLogger()
logger.debug('debug message')
logger.info('info message')
Expand All @@ -120,7 +121,7 @@ def test_debug_logging(self):
)

def test_reduced_logging(self):
ops.log.setup_root_logging(self.backend)
ops.log.setup_root_logging(self.backend) # type: ignore
logger = logging.getLogger()
logger.setLevel(logging.WARNING)
logger.debug('debug')
Expand All @@ -132,7 +133,7 @@ def test_long_string_logging(self):
buffer = io.StringIO()

with patch('sys.stderr', buffer):
ops.log.setup_root_logging(self.backend, debug=True)
ops.log.setup_root_logging(self.backend, debug=True) # type: ignore
logger = logging.getLogger()
logger.debug('l' * MAX_LOG_LINE_LEN)

Expand Down

0 comments on commit df3c2cc

Please sign in to comment.