Remove integer indexing from game collections in pygambit#942
Conversation
iteration and tuple unpacking in quickstart, stripped-down poker, and OpenSpiel tutorials.
Players, outcomes, strategies, infosets, actions, infoset members and Node.children are indexed by str label only. Integer indexing raises TypeError with a 16.7.0 migration message; unknown labels raise KeyError; lookup is by exact match, with no whitespace stripping. Contingency indexing on Game is unchanged.
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
Design question — should label lookup strip whitespace, or match exactly? Before this PR, the collection matches = [x for x in self if x.label == index.strip()]This appears at 10 sites ( In this PR I matched labels exactly. ~~ (superseded — see update) UPD: @rahulsavani and I agreed label lookup should keep stripping surrounding |
…e we need a better way to do this.
tturocy
left a comment
There was a problem hiding this comment.
A few places where we should be using Cython type hints.
Description
Game object collections in
pygambitare now indexed by string label only.Game.players,Game.outcomes,Game.strategies,Game.infosets,Game.actions,Player.strategies,Player.infosets,Player.actions,Infoset.actions,Infoset.members,and
Node.children. Indexing by an integer raisesTypeError.Positional access remains available by iterating a collection (e.g.
pl1, pl2 = game.players).Indexing a
Gameby a contingency (e.g.game[0, 1]) is unchanged.A label matching no object uniformly raises
KeyError(Node.childrenpreviously raisedValueError)._resolve_by_labelhelper; all the collection__getitem__methods use it.The
labelparameter is type-hintedstr, so Cython rejects non-string indices at the call boundary.with new tests added pinning the integer-rejection across all collections
(
COLLECTION_GETTERSintests/test_game.py).