在Python 3中,我有以下代码:
class A:
def __init__(self):
pass
class B(A):
def __init__(self):
super().__init__()
Run Code Online (Sandbox Code Playgroud)
这会产生Pylint警告:
- 定义了旧式的类.(旧式类)
- 在老式班(超老班)上使用超级
根据我的理解,在Python 3中不再存在旧式类,这段代码没问题.
即使我明确地使用这个代码的新式类
class A(object):
def __init__(self):
pass
class B(A):
def __init__(self):
super().__init__()
Run Code Online (Sandbox Code Playgroud)
我得到Pylint警告,因为在Python 3中调用父构造函数的语法不同:
- 缺少对super()的参数(missing-super-argument)
那么,我怎么能告诉Pylint我想检查Python 3代码以避免这些消息(不禁用Pylint检查)?
我使用Django、DRF、drf-yasg和Swagger Codegen自动构建 TypeScript 代码来访问我的 REST API。
在 Django 后端中,我添加了一条与 DRF 一起提供服务的路径:
rest_router = routers.DefaultRouter()
rest_router.register(r'source/(?P<source_id>[0-9]+)/document', DocumentViewSet)
Run Code Online (Sandbox Code Playgroud)
DocumentViewSet是一个 DRF ModelViewSet。
可以看到,source_id参数是数字类型。但是,生成的 API 描述将source_id参数定义为 String 类型。
显然路径设置中的数字正则表达式是不够的,所以我想我需要在类中使用一些类型注释DocumentViewSet?我尝试了以下代码,但这没有效果:
@swagger_auto_schema(
manual_parameters=[
openapi.Parameter(name="source_id",
required=True,
type="integer",
in_="path",
description="Source reference",
),
],
)
class DocumentViewSet(viewsets.ModelViewSet):
serializer_class = rest_serializers.DocumentSerializer
queryset = models.Document.objects.all().order_by('id')
Run Code Online (Sandbox Code Playgroud)
如何告诉 drf-yasg 将source_id参数设置为 Integer 类型?
对Pylint的一些检查取决于检查的源代码是Python 2还是Python 3.例如,请参阅如何避免Python 3中继承类的构造函数的Pylint警告?.
在我的设置中,我在开发服务器上运行Pylint,它使用的是Python 2.7.但是,我使用Pylint在每次提交时自动检查Python 3代码.
如何配置Pylint以执行Python 3的检查,即使它在Python 2下运行?
pylint ×2
python ×2
python-3.x ×2
constructor ×1
django ×1
drf-yasg ×1
inheritance ×1
python-2.7 ×1