Skip to content

Commit

Permalink
Proper type hints for @metric_scope decorator to work nicely with mypy
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Pierre Souchay authored and pierresouchay committed Jun 7, 2022
1 parent e1940a3 commit d147a36
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions aws_embedded_metrics/metric_scope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Callable, TypeVar, cast
from aws_embedded_metrics.logger.metrics_logger_factory import create_metrics_logger
import inspect
import asyncio
from functools import wraps

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

def metric_scope(fn): # type: ignore

def metric_scope(fn: F) -> F:

if asyncio.iscoroutinefunction(fn):

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

return wrapper
return cast(F, wrapper)
else:

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

return wrapper
return cast(F, wrapper)

0 comments on commit d147a36

Please sign in to comment.