python / python/typeshed

Strict equality not working between string and uuid

Open
#13,632 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

Bug Report

--strict-equality seems to only work in very reduced cases. In particular, it is not working when comparing strings and uuids.

To Reproduce

import uuid
from typing import TYPE_CHECKING, reveal_type

a = uuid.uuid4()
b = str(a)

if TYPE_CHECKING:
  reveal_type(a)
  reveal_type(b)
print(a == b)

c = "c"
d = 500
print(c == d)

https://mypy-play.net/?mypy=latest&python=3.12&flags=show-error-codes%2Cstrict-equality&gist=6c8273316b52930967ad0f8062c57aa3

Expected Behavior

The comparison in line 10 should raise a comparison overlap error. Something like

test_strict_equality.py:10: error: Non-overlapping equality check (left operand type: "str", right operand type: "uuid.UUID")  [comparison-overlap]

Actual Behavior

Mypy does not flag the string/UUID comparison as incompatible

test_strict_equality.py:8: note: Revealed type is "uuid.UUID"
test_strict_equality.py:9: note: Revealed type is "builtins.str"
test_strict_equality.py:14: error: Non-overlapping equality check (left operand type: "str", right operand type: "int")  [comparison-overlap]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: --strict-equality --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12.9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python reproducer with mypy 1.15.0, Python 3.12.9, --strict-equality, and --show-error-codes. Investigate why the string/uuid.UUID equality comparison is not reported while string/int is, then add or update coverage for this case. Done means the UUID comparison produces a comparison-overlap error without changing the existing string/int behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.