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

Open Beginner friendly
#77 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
tooling

Research direction

Start in src/bdsim/run_sim.py at blockdiagram(), especially the assertion and factory-method-binding loop around the linked lines. Reproduce the BDSim(load=False) example, then verify that blockdiagram() works without the library while setting bd.runtime, and that the documented non-factory construction path remains usable.

Written by the indexing model from the issue text.

Description

bug tech-debt

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.

Dominant language
Python
Stars
267
Forks
39
Avg merge
11d 18h
Merged PRs (30d)
6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from petercorke/bdsim

All issues in petercorke/bdsim

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.