question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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`

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
kayhayencommented, Jan 5, 2022

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.

0reactions
kayhayencommented, Feb 20, 2022

This is part of the 0.7 release now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Pydantic Error: TypeError: __init__() takes exactly 1 ...
You have to give Pydantic which key you are providing a value for: token_data = schemas.TokenData(username=username). Otherwise Pydantic has ...
Read more >
Writing Robust and Error-Free Python Code Using Pydantic
Python is a dynamically typed language which means that type checking is performed at run-time (when executed). If there is an error in...
Read more >
What does "'Float' object cannot be interpreted as an integer ...
I was trying to follow simple instructions on how to build a game in python but for some reason it is not working...
Read more >
python-magic - OSCHINA - 中文开源技术交流社区
特此记录一下报错解决步骤错误1:ERROR: pyasn1-modules 0.2.8 has requirement ... git clone https://github.com/samuelcolvin/pydantic PyPi ( 9.7M / month ...
Read more >
srsly | Modern high-performance serialization utilities - kandi
Implement srsly with how-to, Q&A, fixes, code snippets. kandi ratings - Low support, No Bugs, No Vulnerabilities. Permissive License, Build available.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found