使用没有pylint警告的信号模块(W0621 & W0613) [英] Using signal module without pylint's warnings (W0621 & W0613)

查看:73
本文介绍了使用没有pylint警告的信号模块(W0621 & W0613)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了 python 的 signal 模块,我为我的第一个实现编写了这个脚本:

I am discovering python's signal module and I wrote this script for my first implementation:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" First implementation of signal module """
import time
import signal
import os
import sys


def cls():
    """ console clearing """
    os.system('clear')
    return


def handler(signal, frame):
    """ Catch <ctrl+c> signal for clean stop"""
    print("{}, script stops".format(time.strftime('%H:%M:%S')))
    sys.exit(0)


signal.signal(signal.SIGINT, handler)

START_TIME = time.strftime('%H:%M:%S')
PROGRESSION = str()


while True:
    time.sleep(2)
    PROGRESSION += "."
    cls()
    print("{}, script starts\n{}".format(START_TIME, PROGRESSION))

除了在中断后出现烦人的^C字符串,脚本按预期工作:

Except the annoying ^C string appearing after interrupt, the script work as expected:

14:38:01, script starts
......
^C14:38:14, script stops

但是pylint3检查给了我这个回报:

However pylint3 checking gives me this return:

testsignal.py:16: [W0621(redefined-outer-name), handler] Redefining name 'signal' from outer scope (line 5)
testsignal.py:16: [W0613(unused-argument), handler] Unused argument 'signal'
testsignal.py:16: [W0613(unused-argument), handler] Unused argument 'frame'

根据信号文档,我做了没错.

如果我更改第 16 行,在 signal 参数中带有尾随下划线(如 PEP8,我解决了警告W0621.

If I change line 16, with a trailing underscore in signal argument (as mentioned in PEP8, I solve the warning W0621.

这是pylint副作用还是我遗漏了什么?

Is it a side effect of pylint or did I miss something?

顺便说一句,如果有人知道如何避免 ^C 字符串,我也会很高兴.

By the way, if someone knows how to avoid the ^C string, I'll be glad too.

pylint3 --version
pylint3 1.5.2, 
astroid 1.4.4
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1]

推荐答案

pylint 警告您,您有一个带有两个参数的函数,您没有在函数内部使用,这是正确的,并且是常见错误的味道.

pylint warns you that you have a function with two parameters you are not using inside the function, which is true and is a smell for common errors.

它还警告使用与外部作用域名称相同的局部函数名称,这有时会导致错误,因为您可能会无意中隐藏外部名称.有时你是故意这样做的,所以 pylint 只是有点烦人,但你也可以重命名本地,就像你所做的那样,摆脱危险.

It also warns about using a local function name that is equal to an outer scope name, which can sometimes lead to errors, because you might be inadvertently hiding the outer name. Sometimes you do it on purpose, so pylint just annoys a bit, but you can also rename the local, as you did and get rid of the danger.

那些只是警告而不是错误.通常,对可能存在的问题进行警告是很好的,即使它们不存在.

Those are just warnings not errors. Usually it is good to be warned about possible problems, even if they don't exist.

静态检查器不知道信号库将如何调用您的处理程序.但警告与此无关.静态工具刚刚注意到您声称接收了两个参数,但您并未在处理程序主体中使用它们.通常当您收到一个参数时,您想使用它对吗?除了在库中为回调注册处理程序时,您必须遵守库协议",否则从那里完成回调时会出现运行时错误.静态工具不知道您不关心接收到的信号信息,而只是打印其他内容;它只是对你说:这看起来很奇怪,你确定吗?

The static checker does not know how will your handler be called by the signals library. But the warning hasn't to do with that. The static tool just noticed you claim to receive two parameters but you are not using them in the handler body. Usually when you receive a parameter you want to use it right? Except that as you are registering a handler in a library for callback, you have to respect the library "protocol", or you'll get a runtime error when the callback is done from there. The static tool does not know you don't care about the signals received info, and are just printing something else; It is just saying to you: that looks strange, are you sure?

这篇关于使用没有pylint警告的信号模块(W0621 &amp; W0613)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆