Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper type hints for @metric_scope decorator to work nicely with mypy #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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)