gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:39:12 31 4
gpt4 key购买 nike

我有一个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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com