Pylint 规则:如何解决未定义的变量? [英] Pylint rules : How to solve Undefined variable?

查看:87
本文介绍了Pylint 规则:如何解决未定义的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 pylint 规则中得到了一些信息:

I get some message from pylint rules :

from scrapy.spiders import Spider
class MySpider(Spider):           #Undefined variable "Spider"
    name = "get"
    start_urls = [""]

    def __init__(self,**kwargs):
        self.page_num = 1         #Undefined variable "self"
        super(MySpider, self).__init__()

    def parse(self, response):
        sel = Selector(response)                #Undefined variable "response"
        sites = sel.css("")                     #Undefined variable "sel"
        category_projects_list = []
        for site in sites:                      #Undefined variable "site"
            project_count = site.css("") 
            category_name = site.css("").extract()
            category_projects = {}
            category_projects['project_count'] = project_count[0]  #Undefined variable "category_projects"  #Undefined variable "project_count"  

我有点困惑如何编辑代码
这意味着我在使用之前已经声明了它??

I am a little confused how to edit the code
It is means I have declare it before I used??

Spider=None
self=None
response= None
sel=None
site=None
...

但是 Spider 是来自 from scrapy.spiders import Spider 我该如何声明??

But the Spider is from from scrapy.spiders import Spider How should I declare it??

而且我认为 category_projects = {} 是声明变量

And I think category_projects = {} is declare the variable

但下一行说未定义变量category_projects"

我想知道如何编辑代码以匹配规则??
以便我可以参考修改其他代码

I want to know how to edit the code to match the rules??
So that I can have a reference to modify other code

推荐答案

pytest 好像有 bug 我用 tox 跑了以下测试:

It seems that there is a bug in pytest I ran the following tests with tox:

[tox]
skipsdist = True
envlist = py{27,34}-pylint{141,142,143,144,145}

[testenv]
whitelist_externals = pylint
deps =
  pylint141: pylint==1.4.1
  pylint142: pylint==1.4.2
  pylint143: pylint==1.4.3
  pylint144: pylint==1.4.4
  pylint145: pylint==1.4.5
commands = pylint -r n test.py

在下面的文件

"""Custom exceptions"""

class MyException(Exception):
    """My custom exception"""

    def __init__(self, message):
        super(MyException, self).__init__(message)

我得到以下结果:

ERROR:   py27-pylint141: commands failed
ERROR:   py27-pylint142: commands failed
ERROR:   py27-pylint143: commands failed
ERROR:   py27-pylint144: commands failed
  py27-pylint145: commands succeeded
ERROR:   py34-pylint141: commands failed
ERROR:   py34-pylint142: commands failed
ERROR:   py34-pylint143: commands failed
ERROR:   py34-pylint144: commands failed
  py34-pylint145: commands succeeded

有这些错误:

************* Module test
E:  7,27: Undefined variable 'self' (undefined-variable)
E:  7,42: Undefined variable 'message' (undefined-variable)

假设是这样,最好的方法是至少迁移到 pylint 1.4.5 版本.

Assuming this, the best way is to move to at least the version 1.4.5 of pylint.

这篇关于Pylint 规则:如何解决未定义的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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