Skip to content

Mypy allows to compare NewTypes created from Decimal  #15095

@kulych

Description

@kulych

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions