[Python][Flask] PyLint实例属性太多,这个资源写对了吗?
[Python][Flask] PyLint too many instance attributes, is this resource correctly written?
我正在使用 Flask 在 Python 中编写应用程序,现在在为端点创建资源 class 期间,我收到 Pylint 'too-many-instance-attributes' 警告。现在我不知道我正在做的是不是 'correct' 编写资源的方式。
我像这样将依赖项注入资源:
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
})
然后在资源中显示为 kwargs 参数。我还装饰了 init 中的函数,因为我需要来自 class 的变量(自己进行装饰)。
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):
这是在 Flask 中编写 Resources 的正确方法吗?还是有更好的结构可以坚持?感谢任何见解或提示!
不要因为 linter 警告而气馁 ~ 它会提醒您坚持特定的风格。如果项目的根文件夹中还没有 .pylintrc
,那么 linter 将使用默认规则;这只是针对“实例中太多变量”的风格警告~即太多 self.*
- 它 100% 是 pylint 的东西,与 Flask 无关 .
您可以通过在 class 的 def __init__(...):
.
上方添加评论 # pylint: disable=too-many-instance-attributes
来抑制对此的警告,但对任何其他警告保持激活状态
如果您想要更 pylintesque 的解决方法,运行 pylint --generate-rcfile
(假设您已经 pip install pylint
这些消息不仅仅来自项目根文件夹中的 IDE) 以生成 .pylintrc
.
从那里您可以将错误代码 R0902
添加到 disable=
列表中 ~ 即 disable=R0902
或 disable=A*,B*,R0902
~ 如果您要禁用多个警告(这接受 globbing,所以你 可以 只是禁用 ALL R*
消息,但最好只在你确定你知道的情况下才这样做你要关闭什么警告)。 OR,你可以在 [DESIGN]
和 set/up 下找到行 max-attributes=
到你认为更合理的更高值。
作为参考,如果您想进一步探索,可以找到此信息的资源〜here's a list of the warnings you can get from pylint which you can search either by the name of the error too-many-instance-attributes
or by its code R0902
, and here's a sample pylintrc,您可以主要通过代码浏览,即R0902
在其中找到影响该警告的 属性。
最后,如果你想要更多示例,这里有另一篇关于相同 pylint 错误消息的 SO 文章 ~ How to deal with Pylint's "too-many-instance-attributes" message?。
我正在使用 Flask 在 Python 中编写应用程序,现在在为端点创建资源 class 期间,我收到 Pylint 'too-many-instance-attributes' 警告。现在我不知道我正在做的是不是 'correct' 编写资源的方式。
我像这样将依赖项注入资源:
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
})
然后在资源中显示为 kwargs 参数。我还装饰了 init 中的函数,因为我需要来自 class 的变量(自己进行装饰)。
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):
这是在 Flask 中编写 Resources 的正确方法吗?还是有更好的结构可以坚持?感谢任何见解或提示!
不要因为 linter 警告而气馁 ~ 它会提醒您坚持特定的风格。如果项目的根文件夹中还没有 .pylintrc
,那么 linter 将使用默认规则;这只是针对“实例中太多变量”的风格警告~即太多 self.*
- 它 100% 是 pylint 的东西,与 Flask 无关 .
您可以通过在 class 的 def __init__(...):
.
# pylint: disable=too-many-instance-attributes
来抑制对此的警告,但对任何其他警告保持激活状态
如果您想要更 pylintesque 的解决方法,运行 pylint --generate-rcfile
(假设您已经 pip install pylint
这些消息不仅仅来自项目根文件夹中的 IDE) 以生成 .pylintrc
.
从那里您可以将错误代码 R0902
添加到 disable=
列表中 ~ 即 disable=R0902
或 disable=A*,B*,R0902
~ 如果您要禁用多个警告(这接受 globbing,所以你 可以 只是禁用 ALL R*
消息,但最好只在你确定你知道的情况下才这样做你要关闭什么警告)。 OR,你可以在 [DESIGN]
和 set/up 下找到行 max-attributes=
到你认为更合理的更高值。
作为参考,如果您想进一步探索,可以找到此信息的资源〜here's a list of the warnings you can get from pylint which you can search either by the name of the error too-many-instance-attributes
or by its code R0902
, and here's a sample pylintrc,您可以主要通过代码浏览,即R0902
在其中找到影响该警告的 属性。
最后,如果你想要更多示例,这里有另一篇关于相同 pylint 错误消息的 SO 文章 ~ How to deal with Pylint's "too-many-instance-attributes" message?。