GDub
GDub

Reputation: 638

How to compile a pydantic BaseModel using mypyc?

In an environment where we have installed:

pip install -U pydantic mypy

Given the example test_basemodel.py:

from pydantic import BaseModel


class A(BaseModel):
    pass

We run the command: mypyc test_basemodel.py and see:

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

According to https://mypyc.readthedocs.io/en/latest/native_classes.html#inheritance

Most non-native classes can’t be used as base classes

Is it possible to compile pydantic BaseModels using mypyc? Pydantic works so well with mypy(not mypyc) and mypy is so strongly tied with mypyc that I can't believe the relationship isn't explicitly documented, let alone unsupported.

Upvotes: 4

Views: 311

Answers (1)

GDub
GDub

Reputation: 638

Thanks to @dROOOze.

The answer was to import BaseModel from pydantic.main instead of pydantic. I still don't understand why this isn't explicitly documented

from pydantic.main import BaseModel


class A(BaseModel):
    pass

Upvotes: 3

Related Questions