Skip to content

Fix hashing completion objects with formatted text - #2154

Open
FanWu-ai wants to merge 1 commit into
prompt-toolkit:mainfrom
FanWu-ai:fix/completion-hash
Open

FanWu-ai wants to merge 1 commit into
prompt-toolkit:mainfrom
FanWu-ai:fix/completion-hash

Conversation

@FanWu-ai

Copy link
Copy Markdown

Summary

Completion.__hash__ hashes self.display, but the constructor always normalizes that field to FormattedText, a list subclass. Consequently even hash(Completion("example")) raises TypeError, preventing completion objects from being used as dictionary keys or in sets.

Hash the completion text and start position instead. Equal completions already share those fields, so the equality/hash contract is maintained without requiring display or metadata to be hashable and without evaluating lazy metadata. Unequal completions with matching text/position may share a hash, which is permitted and is resolved by the existing equality implementation.

Tests cover default displays and dictionary/set use, formatted display plus formatted metadata, and lazy metadata that must not be evaluated.

Validation

  • Pristine complete suite: 156 passed.
  • New regressions against original code: all 3 fail with unhashable FormattedText.
  • Fixed complete suite, with coverage: 159 passed.
  • All configured pre-commit hooks passed via uv run prek run --all-files.
  • CI commands ruff check, ruff format --check, typos, and strict mypy for win32/linux/darwin passed.
  • Source distribution and wheel built with uv build --no-sources.

AI assistance was used to investigate, implement, and test this change. No human review or remote CI success is claimed.

@FanWu-ai
FanWu-ai marked this pull request as ready for review October 21, 2026 22:15
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