Skip to content

BDSim(load=False) + sim.blockdiagram() raises AssertionError, defeating the point of load=False #77

Description

@petercorke

BDSim(load=False) is documented (see Coding-patterns) as the way to construct a block diagram without dynamically loading the block library, and instead build it from block classes imported directly (from bdsim.blocks import Step, Gain, ...) with bd=bd passed to each constructor.

In practice, sim.blockdiagram() unconditionally asserts the block library exists, even when load=False was explicitly passed to skip loading it:

https://github.com/petercorke/bdsim/blob/main/src/bdsim/run_sim.py#L2124

import bdsim

sim = bdsim.BDSim(load=False)
bd = sim.blockdiagram()   # AssertionError

sim.blockdiagram() needs the block library only to bind the upper-case factory methods (.GAIN, .STEP, etc.) onto the returned BlockDiagram — methods the "non factory" pattern deliberately doesn't use. The assert fires regardless, defeating the point of load=False.

Workaround (used to keep the wiki's Coding-patterns page accurate): construct BlockDiagram directly, bypassing sim.blockdiagram(), and set the one attribute it would otherwise have set for you:

from bdsim.blockdiagram import BlockDiagram

bd = BlockDiagram(name="main")
bd.runtime = sim   # normally set inside sim.blockdiagram()

This works, but it's a rough edge — reaching into an attribute that's commented # set by BDSim before compilation shouldn't be necessary for a documented usage pattern.

Suggested fix: in blockdiagram(), skip the factory-method-binding loop (rather than asserting) when self._blocklibrary is None, and set bd.runtime = self either way.

Found while auditing the wiki against current main — the Coding-patterns page's "Non factory" example predates this and was silently broken.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtech-debtKnown technical debt, tracked for a deliberate future revisit

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions