Skip to content

Conversation

@bdabrowski
Copy link
Contributor

@bdabrowski bdabrowski commented Aug 19, 2024

Fixes #615

@bdabrowski bdabrowski marked this pull request as ready for review August 19, 2024 14:13
@bckohan bckohan force-pushed the fix-transmogrify-init branch 2 times, most recently from f2f31a1 to e34db88 Compare December 4, 2025 05:34
@bckohan bckohan requested a review from Copilot December 4, 2025 05:34
Copilot finished reviewing on behalf of bckohan December 4, 2025 05:39
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in the polymorphic object retrieval mechanism where custom __init__ methods were not being called when transmogrifying objects between related classes. The issue occurred because the code was checking for __init__ in the instance's __dict__ (which would never contain class methods) instead of the class's __dict__. The fix changes the check from obj.__dict__ to obj.__class__.__dict__, ensuring that custom initialization logic is properly executed when converting polymorphic instances.

Key Changes:

  • Fixed the __init__ existence check in the transmogrify() function to correctly identify custom initialization methods
  • Added test models (BlueHeadDuck, HomeDuck, PurpleHeadDuck) to demonstrate the bug scenario with proxy models and multiple inheritance
  • Added a regression test to ensure custom __init__ methods are called during polymorphic object retrieval

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/polymorphic/query.py Fixed the bug by changing the __init__ check from obj.__dict__ to obj.__class__.__dict__ in the transmogrify() function
src/polymorphic/tests/models.py Added test models (BlueHeadDuck, HomeDuck, PurpleHeadDuck) to support testing the fix with proxy models and custom __init__ methods
src/polymorphic/tests/test_orm.py Added test_transmogrify_with_init test case and updated imports to include new test models
src/polymorphic/tests/migrations/0001_initial.py Regenerated migration file to include the new test models (Duck, BlueHeadDuck, PurpleHeadDuck)
Comments suppressed due to low confidence (1)

src/polymorphic/tests/test_orm.py:1416

  • Normal methods should have 'self', rather than 'db', as their first parameter.
    def test_transmogrify_with_init(db):

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.40%. Comparing base (347ef6d) to head (59cfde0).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #614      +/-   ##
==========================================
+ Coverage   74.96%   75.40%   +0.44%     
==========================================
  Files          21       21              
  Lines        1342     1342              
  Branches      211      211              
==========================================
+ Hits         1006     1012       +6     
+ Misses        261      257       -4     
+ Partials       75       73       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bckohan bckohan force-pushed the fix-transmogrify-init branch 2 times, most recently from 9412de5 to 59cfde0 Compare December 4, 2025 06:04
@bckohan bckohan changed the title Fixed bug in checking the existence of custom __init__ method in an o… Fixed custom __init__ check in transmogrify Dec 4, 2025
@bckohan bckohan self-assigned this Dec 4, 2025
@bckohan bckohan self-requested a review December 4, 2025 06:07
Copy link
Contributor

@bckohan bckohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom __init__ transmogrify code was dead because it erroneously looked at the object dict to locate an __init__ implementation instead of the class __dict__.

I think there is still an edge case here where values set differently by the base class and subclass's __init__ may be overridden after the subclass instantiation - but its hard to solve for that and this code is certainly more correct.

Thank you!

… object. The __init__ wasn't called previously.

- Added unit-tests for jazzband#615
- update changelog

Co-authored-by: Bartosz Dabrowski <bartosz.dabrowski@adverity.com>
Co-authored-by: Brian Kohan <bckohan@gmail.com>
@bckohan bckohan force-pushed the fix-transmogrify-init branch from 59cfde0 to 8b11bf0 Compare December 4, 2025 06:35
@bckohan bckohan merged commit 0d820d0 into jazzband:master Dec 4, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The objects which were transmogrified aren't initialized correctly if they implement __init__ method.

2 participants