Fix Pattern/Molecule equality and mutable default arguments#81
Open
wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
Open
Fix Pattern/Molecule equality and mutable default arguments#81wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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