Pylint rules : How to solve Undefined variable?

2019-07-13 12:33发布

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
...

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

And I think category_projects = {} is declare the variable

But the next line said Undefined 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

2条回答
forever°为你锁心
2楼-- · 2019-07-13 13:14

You do not have to declare variables in Python. And your class definition is wrong. In Python you dont need parameters and brackets for it. Class Definition Syntax

class MySpider: 
    #your code here
查看更多
▲ chillily
3楼-- · 2019-07-13 13:20

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

on the following file

"""Custom exceptions"""

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

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

I obtain the following result:

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

With those errors:

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

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

查看更多
登录 后发表回答