Skip to content

Hash SpanLevel by name and warn that hashing it is discouraged - #2768

Merged
alexmojaki merged 2 commits into
pydantic:mainfrom
chrikrah:fix/spanlevel-hash-by-name
Nov 10, 2026
Merged

alexmojaki merged 2 commits into
pydantic:mainfrom
chrikrah:fix/spanlevel-hash-by-name

Conversation

@chrikrah

@chrikrah chrikrah commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2758.

SpanLevel.__hash__ now hashes the level name and warns, as alexmojaki suggested on the issue, so level in {'warn', 'error'} agrees with level == 'warn'.

The cost is level in {13}, which matched before and now misses; 'warning' still misses a set that holds 'warn'. The warning names both and points at level.name.

# base f3536ef, Python 3.12, logfire imported from the exported tree
$ python -m pytest tests/test_tail_sampling.py -q -p no:cacheprovider
16 passed
# logfire-sdk/logfire/types.py from base, tests kept
2 failed, 14 passed
$ ruff 0.17.3 format --check && ruff check && pyright logfire-sdk/logfire/types.py
2 files already formatted / All checks passed! / 0 errors
# not run: the full suite, the integration extras

@alexmojaki I went with UserWarning, because Python's default filters hide a DeprecationWarning raised from a sampler module, which is where these lookups live. Would you rather have it as a deprecation anyway?

View guided diff

`SpanLevel` compares equal to a level name but hashed as its number, so
`level in {'warn', 'error'}` was False for a warning span while the list
form was True. Hash the name instead, and warn on every hash, since no
single hash can agree with the name, the number and `'warning'` at once.

Fixes #2758

Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com>
@veria-ai

veria-ai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire
  types.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki
alexmojaki added this pull request to the merge queue Nov 10, 2026
Merged via the queue into pydantic:main with commit 71f141d Nov 10, 2026
30 checks passed
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.

SpanLevel equals a level name, but a set lookup by that name misses it

2 participants