为什么不推断第三部分库函数的类型是mypy?
考虑到此代码:
class ExportView(IndexView):
model_admin: Optional[ModelAdmin] = None
def export_csv(self) -> HttpResponse | StreamingHttpResponse:
fields = getattr(self.model_admin, "csv_export_fields", [])
return render_to_csv_response(self.queryset.all().values(*fields))
我在return
行上从mypy中获得以下错误:
错误:返回从函数返回的任何声明为返回“联合” [httpresponse,streamhttpresponse]”
render_to_csv_response
is django-quertyset-csv 的方法,并且Pyright正确地推断其返回类型为Union [httpresponse, streamhttpresponse]
。
我以为Mypy没有阅读第三部分库,就好像使用- last-imports = skip
,但是我的pyproject.toml
配置已经设置了Silent
,的行为与普通的方式相同
[tool.mypy]
plugins = ["mypy_django_plugin.main"]
disallow_untyped_defs = true
ignore_missing_imports = true
follow_imports = 'silent'
no_incremental = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
warn_unreachable = true
enable_error_code = 'ignore-without-code'
show_error_codes = true
将其更改为strorm
也不会解决问题。 知道如何让Mypy找到合适的类型,例如Pyright开箱即用?
Considering this code:
class ExportView(IndexView):
model_admin: Optional[ModelAdmin] = None
def export_csv(self) -> HttpResponse | StreamingHttpResponse:
fields = getattr(self.model_admin, "csv_export_fields", [])
return render_to_csv_response(self.queryset.all().values(*fields))
I get the following error from Mypy on the return
line:
error: Returning Any from function declared to return "Union[HttpResponse, StreamingHttpResponse]" [no-any-return]
render_to_csv_response
is a method from django-queryset-csv, and Pyright correctly infer its return type as being a Union[HttpResponse, StreamingHttpResponse]
.
I thought that Mypy was not reading the 3rd-part library, as if running with --follow-imports=skip
, but my pyproject.toml
config has set silent
, which behaves in the same way as normal
[tool.mypy]
plugins = ["mypy_django_plugin.main"]
disallow_untyped_defs = true
ignore_missing_imports = true
follow_imports = 'silent'
no_incremental = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
warn_unreachable = true
enable_error_code = 'ignore-without-code'
show_error_codes = true
Changing it to normal
doesn't fix the issue either.
Any idea how to get Mypy to find the proper type, like Pyright does out of the box?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论