Skip to content

Fix Plot crashing when given a MultiFit inside a list (fixes #246) - #260

Open
cverstege wants to merge 2 commits into
devfrom
worktree-fix-issue-246-multifit-plot
Open

Fix Plot crashing when given a MultiFit inside a list (fixes #246)#260
cverstege wants to merge 2 commits into
devfrom
worktree-fix-issue-246-multifit-plot

Conversation

@cverstege

Copy link
Copy Markdown
Member

Summary

  • k2.plot(multi_fit) crashed with TypeError: 'NoneType' object is not callable
    (see wrapped plotting function does not work with multifit #246). Root cause: the plot() wrapper always wraps its argument in a list before
    constructing Plot(...). Since a MultiFit isn't iterable, plot(multi_fit) builds
    Plot([multi_fit]) — and Plot.__init__ only special-cased a MultiFit passed directly
    (isinstance(fit_objects, MultiFit)), so a MultiFit hidden inside a list fell through
    to the regular per-fit path, where MultiFit.PLOT_ADAPTER_TYPE is None, causing the crash.
  • Plot.__init__ (kafe2/fit/_base/plot.py) now also unwraps a lone MultiFit found inside
    a sequence, the same way it already unwraps one passed directly. So both Plot(multi_fit)
    and Plot([multi_fit]) (and therefore k2.plot(multi_fit)) work correctly.
  • If a MultiFit is mixed with other fits in the same sequence (e.g.
    Plot([multi_fit, other_fit])) — a case that isn't supported today, since Plot can only
    track a single combined "global" fit-info block — this now raises a clear
    NotImplementedError instead of the confusing TypeError from before.
  • Added a TestMultiFitPlot test class in kafe2/test/fit/test_plot.py covering all of the
    above (direct MultiFit, MultiFit in a list, the plot() wrapper, and the mixed-list
    error case).
  • Also includes an unrelated black reformat of a few files
    (kafe2/fit/_base/plot.py, several kafe2/test/fit/test_representers_*_yaml.py) needed to
    get CI's Linting job green again — black's formatting rules for multi-line
    textwrap.dedent(...) calls/string concatenations changed between the version pinned when
    those files were last touched and the version pip install currently resolves (the repo
    doesn't pin a black version). No functional changes.

Test plan

  • make test (pytest + coverage/unittest run): 850 passed, 80 skipped, 0 failed.
  • make lint (isort, black, flake8): clean.
  • Ran all 32 scripts under examples/ with MPLBACKEND=Agg: all exit 0, including the
    examples/011_multifit/*.py scripts.
  • Reproduced the exact snippet from issue wrapped plotting function does not work with multifit #246 (k2.MultiFit([...]).do_fit() then
    k2.plot(mfit)) — no longer crashes.
  • Verified Plot([multi_fit, other_fit]) now raises a clear NotImplementedError instead
    of a confusing TypeError.
  • CI on the branch: PyTest — success, Linting — success.

🤖 Generated with Claude Code

cverstege and others added 2 commits July 27, 2026 12:05
Plot.__init__ only recognized a MultiFit if it was passed directly;
a MultiFit wrapped in a sequence (e.g. Plot([multi_fit])) fell through
to the regular fit-object path, where MultiFit.PLOT_ADAPTER_TYPE is
None, causing a confusing "'NoneType' object is not callable" crash.
This is exactly what the plot() wrapper function does internally, so
k2.plot(multi_fit) was broken even though Plot(multi_fit) worked.

Plot now unwraps a lone MultiFit found inside a sequence the same way
it unwraps one passed directly, and raises a clear NotImplementedError
if a MultiFit is mixed with other fits in the same sequence (not yet
supported).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Multi-line textwrap.dedent(...) calls and string concatenations that
used to need wrapping now fit on one line under the black version
pip currently installs, so make lint (and CI's Linting job) flagged
them as needing reformatting. No functional changes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a regression/edge case where k2.plot(multi_fit) (via the wrapper) would crash when a MultiFit instance was wrapped inside a one-element sequence, by teaching Plot.__init__ to detect and unwrap that case (and to fail fast with a clear error for unsupported mixed lists).

Changes:

  • Update Plot.__init__ to unwrap a lone MultiFit found inside an iterable (e.g. [multi_fit]) and raise NotImplementedError if a MultiFit is mixed with other fits.
  • Add regression tests covering Plot(multi_fit), Plot([multi_fit]), the wrapper.plot(multi_fit) path, and the mixed-list error case.
  • Apply formatting-only updates (Black-driven) to several YAML representer test files and plot.py string constants.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
kafe2/fit/_base/plot.py Unwraps MultiFit when passed inside a one-element sequence; adds explicit error for unsupported mixed sequences; minor formatting changes.
kafe2/test/fit/test_plot.py Adds regression tests ensuring MultiFit plotting works through both Plot and the wrapper plot() helper.
kafe2/test/fit/test_representers_parametric_model_yaml.py Formatting-only updates to multiline YAML string concatenations.
kafe2/test/fit/test_representers_model_function_yaml.py Formatting-only updates to multiline YAML string concatenations (including raw strings).
kafe2/test/fit/test_representers_format_yaml.py Formatting-only updates to multiline YAML string concatenations.
kafe2/test/fit/test_representers_fit_yaml.py Formatting-only updates to multiline YAML string concatenations.
kafe2/test/fit/test_representers_container_yaml.py Formatting-only updates to multiline YAML string concatenations.
kafe2/test/fit/test_representers_constraint_yaml.py Formatting-only updates to multiline YAML string concatenations.

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

@cverstege
cverstege marked this pull request as ready for review July 27, 2026 11:37
@cverstege cverstege self-assigned this Jul 27, 2026
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.

2 participants