-
-
Notifications
You must be signed in to change notification settings - Fork 446
Fix AttributeError when using Nuitka-compiled models #2053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
waketzheng
merged 2 commits into
tortoise:develop
from
shkey:fix-nuitka-compiled-model-attribute-error
Jan 12, 2026
Merged
Fix AttributeError when using Nuitka-compiled models #2053
waketzheng
merged 2 commits into
tortoise:develop
from
shkey:fix-nuitka-compiled-model-attribute-error
Jan 12, 2026
+5
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When using tortoise-orm with Nuitka-compiled Python code, the _get_comments function fails with an AttributeError because inspect.getsourcefile() returns None for compiled modules, and the code attempts to call .endswith() on None. This error occurs in the following stack trace: - _get_comments() calls inspect.getsource(cls) - inspect.getsource() -> inspect.getsourcelines() -> inspect.findsource() - inspect.findsourcefile() returns None for Nuitka-compiled modules - inspect.findsource() tries to call filename.endswith(), causing AttributeError The fix adds AttributeError to the exception handling in _get_comments(), allowing the function to gracefully return an empty dict when source code cannot be retrieved from compiled modules. Fixes: AttributeError: 'NoneType' object has no attribute 'endswith'
abondar
previously approved these changes
Jan 9, 2026
Pull Request Test Coverage Report for Build 20873379847Details
💛 - Coveralls |
Member
|
Could you please add description to changelog.rst (under 0.26.0 section) and, if you want, add yourself to contributors.rst |
- Added changelog entry for Nuitka compilation fix in 0.26.0 section - Added Qian Chen (@shkey) to contributors list
2635e52 to
97e1476
Compare
Contributor
Author
|
Hi @abondar, Thank you for approving the PR! I've updated the branch with the requested changes:
The latest commit includes:
Please let me know if you need any other changes. Thanks again for maintaining this excellent ORM! |
henadzit
approved these changes
Jan 11, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix AttributeError when using Nuitka-compiled models
Description
This PR fixes an
AttributeErrorthat occurs when using tortoise-orm with Nuitka-compiled Python code. The issue happens during model initialization when the_get_commentsfunction attempts to retrieve source code from compiled modules.The Problem:
When code is compiled with Nuitka, the original Python source files are not available at runtime. The
_get_commentsfunction callsinspect.getsource(cls), which internally callsinspect.getsourcefile(). For Nuitka-compiled modules, this returnsNoneinstead of a file path. Subsequently, wheninspect.findsource()tries to callfilename.endswith()on thisNonevalue, it raises anAttributeError.The Solution:
Add
AttributeErrorto the exception handling in the_get_commentsfunction (line 154 oftortoise/models.py), alongside the existingTypeErrorandOSErrorexceptions. This allows the function to gracefully return an empty dictionary when source code cannot be retrieved from compiled modules.Code Changes:
Before:
After:
Error Stack Trace:
Motivation and Context
This change is required to make tortoise-orm compatible with Nuitka-compiled Python applications. Currently, any application that uses tortoise-orm and is compiled with Nuitka will crash immediately during startup with an
AttributeError.Why this happens:
_get_comments()callsinspect.getsource(cls)inspect.getsource()→inspect.getsourcelines()→inspect.findsource()inspect.getsourcefile()returnsNonefor Nuitka-compiled modules (no source file available)inspect.findsource()tries to callfilename.endswith()on theNonevalue, causingAttributeErrorImpact of this fix:
_get_commentsfunction is only used for extracting field comments, which are optional metadataHow Has This Been Tested?
Testing Environment:
Test Scenarios:
Nuitka-compiled application with tortoise-orm
AttributeErrorRegular Python environment (regression testing)
Model operations
Checklist:
Notes on unchecked items: