Skip to content

Fix Pattern/Molecule equality and mutable default arguments#81

Open
wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
wshlavacek:modelapi-pattern-equality-and-defaults
Open

Fix Pattern/Molecule equality and mutable default arguments#81
wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
wshlavacek:modelapi-pattern-equality-and-defaults

Conversation

@wshlavacek
Copy link
Copy Markdown

Summary

Two related fixes in `bionetgen/modelapi/pattern.py`:

Equality is one-way

`Pattern.eq` and `Molecule.eq` only check subset membership of molecules / components, so a pattern is considered equal to a strict superset (a one-molecule pattern compares equal to a two-molecule pattern that happens to contain it). Add explicit length checks so the directions are symmetric.

Mutable default args

`Pattern.init`, `Molecule.init`, `Molecule._add_component`, and `Molecule.add_component` use mutable list literals as default arguments (`molecules=[]`, `components=[]`, `states=[]`). The classic Python pitfall: every call without those args reuses the SAME list, so `add_component` calls on different molecules can silently leak components into each other.

Replace the literals with the standard `None` plumbing so each instance gets its own list.

Also drops a stale `# TODO: Implement contains` comment — `contains` has been implemented for some time.

Test plan

  • Existing CI passes
  • Construct two `Pattern`s with different molecule counts; `==` returns False both directions
  • Construct two `Molecule`s, call `add_component` on one, verify the other doesn't see the new component

`Pattern.__eq__` and `Molecule.__eq__` only checked subset membership of
molecules / components, so a pattern was considered equal to a strict
superset (a one-molecule pattern would compare equal to a two-molecule
pattern that happened to contain it). Add explicit length checks so the
directions are symmetric.

Replace mutable default args (`molecules=[]`, `components=[]`,
`states=[]`) on `Pattern.__init__`, `Molecule.__init__`,
`Molecule._add_component`, and `Molecule.add_component` with the standard
`None` plumbing, so fresh `Pattern` / `Molecule` instances and
`add_component` calls no longer share the same list across instances.

Also drops a stale `# TODO: Implement __contains__` comment on `Pattern`
— `__contains__` has been implemented for some time.
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.

1 participant