From c1760166cfd45cbe5d101f0807be3ca1d126d118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Tr=C4=99bski?= Date: Mon, 4 Jan 2021 00:39:11 +0100 Subject: [PATCH] Accept numbers (int,float) for loguru calls (#79) * Accept numbers (int,float) for loguru calls Partially: #49 --- loguru_mypy/__init__.py | 13 ++++++++++--- typesafety/test_issue_49.yml | 9 +++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 typesafety/test_issue_49.yml diff --git a/loguru_mypy/__init__.py b/loguru_mypy/__init__.py index a50da81..7b009bf 100644 --- a/loguru_mypy/__init__.py +++ b/loguru_mypy/__init__.py @@ -7,7 +7,9 @@ from mypy.nodes import ( CallExpr, Expression, + FloatExpr, FuncDef, + IntExpr, LambdaExpr, MemberExpr, NameExpr, @@ -96,9 +98,14 @@ def _loguru_logger_call_handler( log_msg_expr = ctx.args[0][0] logger_opts = loggers.get(ctx.type) or DEFAULT_OPTS - assert isinstance(log_msg_expr, StrExpr), type(log_msg_expr) - - _check_str_format_call(log_msg_expr, ctx) + if isinstance(log_msg_expr, StrExpr): + _check_str_format_call(log_msg_expr, ctx) + elif isinstance(log_msg_expr, (IntExpr, FloatExpr)): + # nothing to be done, this is valid log + # and callee is not expected to provide anything useful over here + return ctx.default_return_type + else: + raise TypeError(f'No idea (yet) how to handle {type(log_msg_expr)}') if logger_opts.lazy: # collect call args/kwargs diff --git a/typesafety/test_issue_49.yml b/typesafety/test_issue_49.yml new file mode 100644 index 0000000..108ac98 --- /dev/null +++ b/typesafety/test_issue_49.yml @@ -0,0 +1,9 @@ +--- +- case: numbers + main: | + from loguru import logger + + logger.info(123) + logger.debug(123.3) + logger.trace(1) + logger.error(.3)