Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ jobs:
pip install -r requirements.txt -r test-requirements.txt
- name: Type-check with mypy
run: mypy
- name: Type-check the public API as a strict consumer
# Errors inside logtail itself stay silent, as they are for an installed package.
run: mypy --strict --follow-imports=silent tests/typecheck
2 changes: 2 additions & 0 deletions logtail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
__version__ = '0.5.0'

context = DEFAULT_CONTEXT

__all__ = ['LogtailHandler', 'LogtailContext', 'DEFAULT_CONTEXT', 'LogtailFormatter', 'context', '__version__']
3 changes: 1 addition & 2 deletions logtail/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def __enter__(self) -> 'LogtailContext':
return self

def __exit__(self, type_: Optional[type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> None:
if type_ is None:
self.extras.pop()
self.extras.pop()

def exists(self) -> bool:
return bool(self.extras)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def test_does_not_suppress_exceptions(self):
with c(user={'name': 'a'}):
raise ValueError('should be thrown')

def test_exception_leaves_the_context(self):
c = LogtailContext()
with c(user={'name': 'a'}):
with self.assertRaises(ValueError):
with c(request={'id': 'r'}):
raise ValueError('leaves the inner block')
self.assertEqual(c.collapse(), {'user': {'name': 'a'}})
self.assertFalse(c.exists())

def test_nested_collapse(self):
c = LogtailContext()
self.assertEqual(c.collapse(), {})
Expand Down
17 changes: 17 additions & 0 deletions tests/typecheck/public_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Checked with `mypy --strict` in CI, never run: imports and calls the public API the
# way applications do, so it fails if a name stops being exported from `logtail`.
import json
import logging

import logtail
from logtail import DEFAULT_CONTEXT, LogtailContext, LogtailFormatter, LogtailHandler

handler = LogtailHandler(source_token='token', host='in.logs.betterstack.com', timeout=(3.05, 27), flush_timeout=None)
handler.setFormatter(LogtailFormatter(context=DEFAULT_CONTEXT, json_default=str, json_encoder=json.JSONEncoder))
logging.getLogger(__name__).addHandler(handler)

request_context: LogtailContext = logtail.context
with request_context(request={'id': 'abc'}):
logging.getLogger(__name__).info('typed')

version: str = logtail.__version__
Loading