python - Pycharm无法识别logging.basicConfig处理程序参数

标签 python python-3.x logging pycharm

我有一个python应用程序,该应用程序现在使用python logging库一段时间,以便同时在屏幕上和按时间旋转文件的同时打印消息,并且运行良好。

日志记录配置如下:

import logging
from logging.handlers import TimedRotatingFileHandler
logging.basicConfig(level=logging.INFO if debug is not True else logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    handlers=[
                        TimedRotatingFileHandler(log_filename, when='midnight', interval=1),
                        logging.StreamHandler()
                    ]
                    )

我的问题是PyCharm会继续用以下警告突出显示配置的logging.basicConfig部分:

Unexpected argument(s)

Possible callees:

basicConfig(*, filename: Optional[str]=..., filemode: str=..., format: str=..., datefmt: Optional[str]=..., level: Union[int, str, None]=..., stream: IO[str]=...) basicConfig()

Inspection info: Reports discrepancies between declared parameters and actual arguments, as well as incorrect arguments (e.g. duplicate named arguments) and incorrect argument order. Decorators are analyzed, too.



而且只有在删除代码的handlers=[...]部分时,它才会熄灭。
basicConfig的参数是否在特定版本上更改?如果是,实现相同目标的建议方法是什么?

我正在使用python 3.6pycharm 2020.1(但至少在过去3个更新中具有相同的警告)

最佳答案

PyCharm错误跟踪器https://youtrack.jetbrains.com/issue/PY-39762上报告了此问题。简而言之:无法识别Python 3中basicConfig的新关键字参数,例如handler
该问题还提到了一种解决方法:

Put a caret on basicConfig - Right Click - Go to - Declaration or Usages - Click on a star on the left (on a gutter) - logging/__init__.pyi should be opened - Annotate all basicConfig definitions with @overload.


我对其进行了测试,并且效果良好。就我而言,PyCharm不再提示force=True参数。

Did the basicConfig's arguments change on a specific version?


您可以随时检查文档:
https://docs.python.org/3/library/logging.html#logging.basicConfig:

Changed in version 3.2: The style argument was added.

Changed in version 3.3: The handlers argument was added. Additional checks were added to catch situations where incompatible arguments are specified (e.g. handlers together with stream or filename, or stream together with filename).

Changed in version 3.8: The force argument was added.

关于python - Pycharm无法识别logging.basicConfig处理程序参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61226587/

相关文章:

python - 将 Flask models.py 迁移到 MySQL

python - 如何减少由许多条件语句组成的函数?

java - 什么是事件溯源以及如何在 Java 中实现它?

node.js - Express 应用程序中的 morgan 记录器不会为每个条目创建新行

java - google app engine java的应用监控

python - 如何使用 python 列出应用于 azure 资源组的锁

python - 在scoped_session中跨进程边界

python - 在python中向json添加节点

python - Python 3.2 "with/as"是做什么的

python - Twisted 在此平台上不受支持的轮子