Nuitka: [bug] Nuitka produces runtime TypeError with Pydantic's BaseModel

A module utilizing Pydantic’s BaseModel class is compiled via Nuitka. The plain source code runs fine when executed in test_my_module.py, however the compiled code fails to pass. The resulting error when trying to invoke b.bar(bar_arg='some argument') causes the following exception. (Code is included down below in the body of the issue template).

Traceback (most recent call last):
  File "/home/[user1]/.pyenv/versions/3.9.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/[user1]/.pyenv/versions/3.9.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/mnt/c/Users/[user1]/github/nuitka-pydantic-bug/test_my_module.py", line 19, in <module>
    test_minimum_failure()
  File "/mnt/c/Users/[user1]/github/nuitka-pydantic-bug/test_my_module.py", line 8, in test_minimum_failure
    b.bar(bar_arg='some argument')
TypeError: bar() missing 1 required positional argument: 'self'

  • Nuitka version, full Python version, flavor, OS, etc. as output by this command (it does more than you think, and we are adding more all the time):

    python -m nuitka --version

(.venv) [user1]@LAPTOP:/mnt/c/Users/[user1]/github/nuitka-pydantic-bug$ python -m nuitka --version
0.6.18.6
Commercial: None
Python: 3.9.9 (main, Dec 29 2021, 12:20:17)
Flavor: pyenv
Executable: /mnt/c/Users/[user1]/github/nuitka-pydantic-bug/.venv/bin/python
OS: Linux
Arch: x86_64
Distribution: Ubuntu "16.04.6
  • How did you install Nuitka and Python
Installed via pip into a virtualenv (WSL Ubuntu). Also tested on plain Windows with pip + venv. Confirming that Python.exe is not from the Windows store.
  • The specific PyPI names and versions

    It should be taken from this output if there specific packages involved, e.g. numpy, you are expected to shorten this to the relevant ones.

    python -m pip freeze

Nuitka==0.6.18.6
pydantic==1.9.0
typing_extensions==4.0.1
  • Many times when you get an error from Nuitka, your setup may be special

    Then even a print("hello world") program will not work, please try that and report that instead.

Confirming simple "hello world" works

See here for the repo with the example below.

my_module/init.py:

from pydantic import BaseModel

class A:
    def foo(self, foo_arg):
        print(f'A.foo({foo_arg})')

class B(BaseModel):
    member_a: A

    def bar(self, bar_arg: str) -> None:
        self.member_a.foo(bar_arg)

    class Config:
        arbitrary_types_allowed = True


class B2:
    def __init__(self, member_a: A) -> None:
        self.member_a = member_a

    def bar(self, bar_arg: str) -> None:
        self.member_a.foo(bar_arg)

test_my_module.py:

import my_module as mm

def test_minimum_failure() -> None:
    a = mm.A()
    b = mm.B(member_a=a)
    print(f'b: {b}', type(b))
    print(f'b.member_a: {b.member_a}', type(b.member_a))
    b.bar(bar_arg='some argument')

def test_working() -> None:
    a = mm.A()
    b2 = mm.B2(member_a=a)
    print(f'b2: {b2}', type(b2))
    print(f'b2.member_a: {b2.member_a}', type(b2.member_a))
    b2.bar(bar_arg='some argument')

if __name__ == '__main__':
    test_working()
    test_minimum_failure()
  • Provide in your issue the Nuitka options used
python -m nuitka --module my_module --include-package my_module
  • Note if this is a regression

    If it used to work in an earlier version of Nuitka, please note what you know about that.

I have tried the following versions: `0.6.19rc8`, `0.6.18.6` and `0.6.15.3`

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (11 by maintainers)

Most upvoted comments

I am suspecting this is #471 which has a solution, check it out, will need 0.6.19 to be really good, but 0.6.18 series has something for it, but I definitely recommend to use develop for now. The 0.6.19 release is due soon.