如何使用mypyc编译pydantic BaseModel?

问题描述 投票:0回答:1

在我们已经安装的环境中:

pip install -U pydantic mypy

举个例子

test_basemodel.py

from pydantic import BaseModel


class A(BaseModel):
    pass

我们运行命令:

 mypyc test_basemodel.py
并看到:

running build_ext
building 'test_basemodel' extension
x86_64-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -I/home/guy/repos/DeepracerService/src/local/processor/.venv/lib/python3.12/site-packages/mypyc/lib-rt -I/home/guy/repos/DeepracerService/src/local/processor/.venv/include -I/usr/include/python3.12 -c build/__native.c -o build/temp.linux-x86_64-cpython-312/build/__native.o -O3 -g1 -Werror -Wno-unused-function -Wno-unused-label -Wno-unreachable-code -Wno-unused-variable -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-but-set-variable -Wno-ignored-optimization-argument -Wno-cpp
build/__native.c: In function ‘CPyDef___top_level__’:
build/__native.c:130:16: error: ‘CPyModule_pydantic___main’ undeclared (first use in this function); did you mean ‘CPyModule_pydantic’?
  130 |     cpy_r_r9 = CPyModule_pydantic___main;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~
      |                CPyModule_pydantic
build/__native.c:130:16: note: each undeclared identifier is reported only once for each function it appears in
build/__native.c: At top level:
cc1: note: unrecognized command-line option ‘-Wno-ignored-optimization-argument’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-unknown-warning-option’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-unused-command-line-argument’ may have been intended to silence earlier diagnostics

根据https://mypyc.readthedocs.io/en/latest/native_classes.html#inheritance

大多数非原生类不能用作基类

是否可以使用 mypyc 编译 pydantic BaseModel? Pydantic 与 mypy(不是 mypyc)配合得很好,并且 mypy 与 mypyc 的联系如此紧密,以至于我不敢相信这种关系没有明确记录,更不用说不受支持了。

python mypy pydantic pydantic-v2 mypyc
1个回答
1
投票

感谢@dROOOze

答案是从

import BaseModel
pydantic.main
,而不是 pydantic。我仍然不明白为什么没有明确记录这一点

from pydantic.main import BaseModel


class A(BaseModel):
    pass
© www.soinside.com 2019 - 2024. All rights reserved.