Skip to content

Don't orphan SplitContainer content when the assignment is invalid - #4633

Merged
freakboy3742 merged 6 commits into
beeware:mainfrom
Dev9269:fix-splitcontainer-content-setter
Nov 3, 2026
Merged

freakboy3742 merged 6 commits into
beeware:mainfrom
Dev9269:fix-splitcontainer-content-setter

Conversation

@Dev9269

@Dev9269 Dev9269 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

SplitContainer.content setter detached the currently-shown widgets before validating the new value:

for old_content in self._content:
    if old_content is not None:
        old_content.app = None
        old_content.window = None
try:
    if len(content) != 2:
        raise TypeError()
    ...

If the assignment was invalid (wrong length, duplicate widget, wrong element type), the exception propagated but the old content had already been orphaned — app/window were reset to None, so the previously-visible widgets detached from the widget tree even though content still reported the old value.

This PR moves the detach to after the new content is fully validated, so a failed assignment leaves the current content attached and intact.

Fixes #4631

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool

Assisted-by: Claude Opus 4.5

@Dev9269
Dev9269 force-pushed the fix-splitcontainer-content-setter branch from 6622e84 to 45ba723 Compare August 5, 2026 22:05

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch - thanks for the report and PR.

However - this has clearly broken CI. I'm guessing this is because the test app lifecycle isn't being honoured because you're manually constructing the SplitContainer widget rather than using the fixture.

The previous test hand-constructed toga.App() and toga.Window(), bypassing
the widget lifecycle that CI relies on. Attach the splitcontainer to the
app and window fixtures instead, which is what a real assignment does. The
regression still proves the bug: with the detached-before-validation code
it fails (content1 is orphaned from app.widgets); with the fix it passes.
@Dev9269

Dev9269 commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Fix pushed (e175b26). You were right about the lifecycle — the old test hand-constructed toga.App()/toga.Window(), bypassing the fixture that CI expects. Now uses the app/window/splitcontainer fixtures and attaches via the widget lifecycle.

Verified locally against the toga_dummy backend:

  • The regression test FAILS against the pre-fix code (content1 is orphaned from app.widgets after the failed assignment) and PASSES with the fix.
  • Full core/tests/widgets/test_splitcontainer.py (40 tests) passes.

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've pushed a minor cleanup to the tests, but otherwise this looks great. Thanks for the fix!

@freakboy3742
freakboy3742 merged commit 22d1ed2 into beeware:main Nov 3, 2026
120 of 122 checks 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.

SplitContainer.content setter orphans current content when the assignment is invalid

2 participants