为什么不推断第三部分库函数的类型是mypy?

发布于 2025-02-13 17:04:09 字数 1554 浏览 0 评论 0原文

考虑到此代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文