这是indexloc提供的服务,不要输入任何密码
Skip to content

Inconsistent __iter__ in batch #922

@MischaPanch

Description

@MischaPanch

Batch is essentially a wrapper around a dict. However, iterating over a batch leads to potentially unexpected behavior.

While list({...}) == list({..}.keys()), for batch list(Batch(...)) gives a sequence of Batch objects and list(Batch(...).keys()) gives a list of keys.

This collides with e.g. ruff's recommendation of not using in-dict-keys. I had to disable this rule in #921 because both for b in batch and for key in batch.keys() are currently used with their respective semantics throughout the code.

Fixing this would mean defining __iter__ in Batch to return keys and something like creating a new method in Batch called iter_entries that would replace the current iteration semantics. This would be a breaking change.

@Trinkle23897 what do you think? One can also live with the current status of the "inconsistency", keep ignoring the ruff recommendation, and just document the current behavior somewhere. I would also add dedicated unit tests in that case

Metadata

Metadata

Assignees

Labels

Batch and BufferImprovements in internal data structures, temporary labelbreaking changesChanges in public interfaces. Includes small changes or changes in keysrefactoringNo change to functionality

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions