Skip to content

Commit 48e0477

Browse files
author
Pierre Souchay
committed
Proper type hints for @metric_scope decorator to work nicely with mypy
When using @metric_scope on a typed function, mypy produces this kind of error: error: Untyped decorator makes function "handler" untyped This fix avoid this error and will let people using typehint now having those kind of error messages. See https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
1 parent 24cc7e5 commit 48e0477

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

aws_embedded_metrics/metric_scope/__init__.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14+
from typing import Any, Callable, TypeVar, cast
1415
from aws_embedded_metrics.logger.metrics_logger_factory import create_metrics_logger
1516
import inspect
1617
import asyncio
1718
from functools import wraps
1819

20+
F = TypeVar('F', bound=Callable[..., Any])
1921

20-
def metric_scope(fn): # type: ignore
22+
23+
def metric_scope(fn: F) -> F:
2124

2225
if asyncio.iscoroutinefunction(fn):
2326

@@ -33,7 +36,7 @@ async def wrapper(*args, **kwargs): # type: ignore
3336
finally:
3437
await logger.flush()
3538

36-
return wrapper
39+
return cast(F, wrapper)
3740
else:
3841

3942
@wraps(fn)
@@ -49,4 +52,4 @@ def wrapper(*args, **kwargs): # type: ignore
4952
loop = asyncio.get_event_loop()
5053
loop.run_until_complete(logger.flush())
5154

52-
return wrapper
55+
return cast(F, wrapper)

0 commit comments

Comments
 (0)