我正在使用 Flask 用 Python 编写一个应用程序,现在在为端点创建资源类期间,我收到 Pylint“实例属性过多”警告。现在我不再知道我正在做的是否是编写资源的“正确”方式。
我将依赖项注入到资源中,如下所示:
api.add_resource(TicketsQuery, '/tickets/query',
'/ticket/query/<int:ticketID>',
resource_class_kwargs={'cleaner': Cleaner(StrategyResponseTickets()),
'machine_cleaner': Cleaner(StrategyResponseMachines()),
'db': soap_caller,
'cache': cache,
'field_map': app.config['FIELD_FILTER_MAP']['TICKETS'],
'endpoint_permission': TicketsQueryPermission
})
Run Code Online (Sandbox Code Playgroud)
然后它作为 kwargs 参数显示在资源中。我还装饰了 init 内部的函数,因为我需要类中的变量(来进行装饰本身)。
class TicketsQuery(Resource):
def __init__(self, **kwargs):
# Dependencies
self.cleaner = kwargs['cleaner']
self.machine_cleaner = kwargs['machine_cleaner']
self.db = kwargs['db']
self.cache = kwargs['cache']
self.field_map = kwargs['field_map']
self.endpoint_permission = kwargs['endpoint_permission']
# Permissions of endpoint method calls, implemented using wrapper
self.get = authorization_required(self.endpoint_permission, UserType.GENERIC_EMPLOYEE)(self.get)
self.post = authorization_required(self.endpoint_permission, UserType.GENERIC_EMPLOYEE)(self.post)
def get(self, permission_set: TicketsPermissionSet, ticketID=-1):
Run Code Online (Sandbox Code Playgroud)
这是在 Flask 中编写资源的正确方法吗?或者有更好的结构可以遵循吗?任何见解或提示表示赞赏!
我使用默认的 pylintgit actions检查我的项目是否有任何错误。
但有一些错误我想忽略。如果是在 vscode 中,你可以在settings.json. 我如何忽略它们git actions?
name: Pylint
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
Run Code Online (Sandbox Code Playgroud) 如果我从中ctypes.BigEndianStructure派出一个类,如果我不打电话,pylint会发出警告BigEndianStructure.__init__().很好,但是如果我修复我的代码,pylint仍会警告:
import ctypes
class Foo(ctypes.BigEndianStructure):
def __init__(self):
ctypes.BigEndianStructure.__init__(self)
$ pylint mymodule.py
C: 1: Missing docstring
C: 3:Foo: Missing docstring
W: 4:Foo.__init__: __init__ method from base class 'Structure' is not called
W: 4:Foo.__init__: __init__ method from base class 'BigEndianStructure' is not called
R: 3:Foo: Too few public methods (0/2)
Run Code Online (Sandbox Code Playgroud)
起初我以为这是因为Structure来自C模块.如果我从我的一个类中继承,或者说,SocketServer.BaseServer这是纯Python ,我不会收到警告.但是我也没有得到警告,如果我是子类smbus.SMBus,这是在C模块中.
有人知道除了禁用W0231之外的解决方法吗?
Pylint警告可疑访问对象的受保护成员.它知道当访问来自对象时如何不警告,但是当访问来自对象的属性时,不知道如何不警告.
防爆.
class C(object):
def __init__(self):
C.__a = 0
a = property(lambda self: self.__a)
Run Code Online (Sandbox Code Playgroud)
Pylint告诉" W0212(protected-access):访问__a客户端类的受保护成员"
我不想全局禁用W0212,我不满意在本地(*)为每个这样的属性定义重复禁用它.
有没有一种已知的解决方法?
(*)如:
class C(object):
def __init__(self):
C.__a = 0
a = property(lambda self: self.__a) # pylint: disable=W0212
Run Code Online (Sandbox Code Playgroud)
作为一个有趣的旁注,我选择的答案提供了实际Pylint的额外好处(可能在未来的版本中有所改变,我无法分辨):它保留了Pylint检查不存在的成员的能力,因为此测试显示:
class C1(object):
member = 0
class C2(object):
def __init__(self):
self.__a = C1()
def a(self):
return self.__a
@property
def b(self):
return self.__a
c = property(lambda self: self.__a)
def test_member():
o = C2()
print(o.a().member)
print(o.b.member)
print(o.c.member)
def test_nonexistent():
o = C2() …Run Code Online (Sandbox Code Playgroud) 假设我有一个引发异常的方法:
def methodA(x, y):
if y != 0:
z = x / y
return z
else:
raise ZeroDivisionError("zero can not be a denominator")
Run Code Online (Sandbox Code Playgroud)
在方法B中,我调用了methodA而没有处理异常:
def methodB(x, y):
print methodA(x, y)
Run Code Online (Sandbox Code Playgroud)
在这里,我想要一些可以警告我ZeroDivisionError在methodB中存在潜在风险的东西,并且最好抓住它.有没有办法在方法A中添加某些代码,或者我们可以使用一些工具来找到我忽略了一些重要的异常吗?
所以Pylint(1.4.3)报告循环导入,它没有多大意义.首先,报告的文件没有import语句.
其次没有文件导入参考文件.__init__.py文件从development_config(有问题的文件)加载配置值,但没有文件导入该文件.
那么为什么Pylint会给我这个警告呢?
************* Module heart_beat.development_config
R: 1, 0: Cyclic import (heart_beat -> heart_beat.views -> heart_beat.models) (cyclic-import)
R: 1, 0: Cyclic import (heart_beat -> heart_beat.views) (cyclic-import)
Run Code Online (Sandbox Code Playgroud)
development_config""" -------------------------- DATA BASE CONFINGURATION --------------------"""
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db'
SQLALCHEMY_ECHO = False
""" -------------------------- Flask Application Config --------------------"""
THREADS_PER_PAGE = 8
VERSION = "0.1"
Run Code Online (Sandbox Code Playgroud)
__init__.pyfrom flask import Flask
from flask_sqlalchemy import SQLAlchemy
#from register_assets import register_all
app = Flask(__name__, static_url_path='/static')
# the environment …Run Code Online (Sandbox Code Playgroud) 在我正在研究的一些Python代码中,我发现了所谓的" Yoda条件 ".
例如:
if 0 < len(someList): ...
if None != ComputeSomething(): ...
Run Code Online (Sandbox Code Playgroud)
有没有办法让PyLint标记它们?
我一直C0103在Visual Studio中从pylint 收到警告,因为我试图使用2个字符的变量名称,例如hp和gp。该警告在此处描述:link。
约定描述[a-z_][a-z0-9_]{2,30}$为variable-rgx。我实际上不知道如何阅读此正则表达式或它的含义,但看起来该{2,30}部分描述了可能的长度范围,所以(如果我错了,请纠正我)为什么不允许字符长度为2?还是会有其他原因导致诸如这样的变量名gp出错?
当问到这个问题时,人们经常链接到PEP-8,但我不记得阅读过,变量名必须至少具有3个字符的长度。无论如何,我认为这可能是错误的形式,但我不想遵循此约定。在我的程序上下文中,非常清楚地知道2个字符的变量名称,例如gp和hp意味着什么,这似乎对编码样式有很大的限制。
因此,无论如何,我想做的就是专门覆盖此警告。我不想只是禁用C0103。相反,我宁愿在文本编辑器(Visual Studio Code)中更改此设置,例如在可以使用更改pylintargs 的设置中"python.linting.pylintArgs": [...]。那么,如果我想重写约定以允许使用2个字符的变量名,那么正确的更改是什么?还是我必须编写一个新的lintrc文件(不知道该怎么做,我更喜欢一个更简单的解决方案,仅在VSCode中进行更改)。
我将ALE与Pylint和pylint-django一起使用,但无法对其进行配置。当浏览Django项目中的任何文件时,它会显示linter警告:
no-member: User class has no member objects for below code.
Run Code Online (Sandbox Code Playgroud)
在如下代码上:
from django.contrib.auth.models import User
user_list = User.objects.all()
Run Code Online (Sandbox Code Playgroud) 在继续进行Django Project Now时,我在VSC pylint中使用linter遇到了麻烦。首先,我将虚拟环境与pipenv结合使用。并且我正在Windows 10上使用Visual Studio Code版本1.19.1。我遇到的问题是,即使我使用pipenv和pip命令安装了pylint,VSC也无法识别它,而使用Visual Studio Code进行安装却无法工作

首先,我发现python路径设置指向全局设置,而不是虚拟环境。所以我按照以下方式编辑VSC的settings.json。
{
"python.pythonPath": "C:\\Users\\seungjinlee\\AppData\\Local\\lxss\\home\\seungjinlee\\.local\\share\\virtualenvs\\seungjingram-6b3oTnkI\\bin\\python",
}
Run Code Online (Sandbox Code Playgroud)
Windows的bash是否有问题?我猜bash shell为Ubuntu创建了虚拟环境。但是我在Windows 10上使用了VSC编辑器,因此找不到Windows的pylint,... plz可以帮助我..!
pylint ×10
python ×10
django ×2
flask ×1
neovim ×1
pipenv ×1
pylintrc ×1
python-3.x ×1
try-except ×1
vim ×1