5
5
import json
6
6
import logging
7
7
from contextvars import ContextVar
8
- from typing import TYPE_CHECKING , Any , Callable , NamedTuple , cast
8
+ from typing import TYPE_CHECKING , Any , Callable , NamedTuple
9
9
10
10
# Conditional import only executed when type checking, otherwise we'd get circular dependency issues
11
11
if TYPE_CHECKING :
@@ -39,12 +39,12 @@ class LogContext(NamedTuple):
39
39
# Metaclass for resource clients which wraps all their public methods
40
40
# With injection of their details to the log context vars
41
41
class WithLogDetailsClient (type ):
42
- def __new__ (cls : type [ type ] , name : str , bases : tuple , attrs : dict ) -> WithLogDetailsClient :
42
+ def __new__ (cls , name : str , bases : tuple , attrs : dict ) -> WithLogDetailsClient :
43
43
for attr_name , attr_value in attrs .items ():
44
44
if not attr_name .startswith ('_' ) and inspect .isfunction (attr_value ):
45
45
attrs [attr_name ] = _injects_client_details_to_log_context (attr_value )
46
46
47
- return cast ( WithLogDetailsClient , type .__new__ (cls , name , bases , attrs ) )
47
+ return type .__new__ (cls , name , bases , attrs )
48
48
49
49
50
50
# Wraps an unbound method so that its call will inject the details
@@ -87,7 +87,7 @@ def wrapper(resource_client: _BaseBaseClient, *args: Any, **kwargs: Any) -> Any:
87
87
# A filter which lets every log record through,
88
88
# but adds the current logging context to the record
89
89
class _ContextInjectingFilter (logging .Filter ):
90
- def filter (self : _ContextInjectingFilter , record : logging .LogRecord ) -> bool :
90
+ def filter (self , record : logging .LogRecord ) -> bool :
91
91
record .client_method = log_context .client_method .get ()
92
92
record .resource_id = log_context .resource_id .get ()
93
93
record .method = log_context .method .get ()
@@ -105,15 +105,15 @@ class _DebugLogFormatter(logging.Formatter):
105
105
empty_record = logging .LogRecord ('dummy' , 0 , 'dummy' , 0 , 'dummy' , None , None )
106
106
107
107
# Gets the extra fields from the log record which are not present on an empty record
108
- def _get_extra_fields (self : _DebugLogFormatter , record : logging .LogRecord ) -> dict :
108
+ def _get_extra_fields (self , record : logging .LogRecord ) -> dict :
109
109
extra_fields : dict = {}
110
110
for key , value in record .__dict__ .items ():
111
111
if key not in self .empty_record .__dict__ :
112
112
extra_fields [key ] = value # noqa: PERF403
113
113
114
114
return extra_fields
115
115
116
- def format (self : _DebugLogFormatter , record : logging .LogRecord ) -> str :
116
+ def format (self , record : logging .LogRecord ) -> str :
117
117
extra = self ._get_extra_fields (record )
118
118
119
119
log_string = super ().format (record )
0 commit comments