python / python/mypy

Mypy allows to compare NewTypes created from Decimal

Open
#15,095 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy does not produce an error when comparing NewTypes based on Decimal.

To Reproduce

from decimal import Decimal
from typing import NewType

# Comparing Decimal NewTypes is OK 
FirstDecimal = NewType("FirstDecimal", Decimal)
SecondDecimal = NewType("SecondDecimal", Decimal)

FirstDecimal(Decimal("0.1")) == SecondDecimal(Decimal("0.1"))  # ! mypy ok

# But comparing other NewTypes is not
FirstInt = NewType("FirstInt", int)
SecondInt = NewType("SecondInt", int)

FirstInt(1) == SecondInt(3)  # mypy error

FirstString = NewType("FirstString", str)
SecondString = NewType("SecondString", str)

FirstString("bam") == SecondString("boo")  # mypy error

Expected Behavior

Mypy produces Non-overlapping equality check for all three comparisons.

Actual Behavior

Mypy produces Non-overlapping equality check only for int and str NewTypes.

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
strict = true
show_error_context = true
show_error_codes = true
  • Python version used: 3.10.8

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 minimal Python reproduction with mypy 1.1.1 and --strict, comparing the Decimal-based NewTypes with the int and str cases. Trace the type-checking path for non-overlapping equality checks; the issue is done when equivalent NewTypes consistently produce the expected diagnostic and regression coverage confirms all three comparisons.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.