Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict equality check fails to detect non-overlap of str and Path #16231

Closed
bluenote10 opened this issue Oct 6, 2023 · 2 comments
Closed

strict equality check fails to detect non-overlap of str and Path #16231

bluenote10 opened this issue Oct 6, 2023 · 2 comments
Labels
bug mypy got something wrong topic-overlap Overlapping equality check

Comments

@bluenote10
Copy link
Contributor

Bug Report

At runtime, comparing an instance of Path to str seems to be always negative, i.e., they don't overlap in terms of comparison.

Using the --strict-equality typically errors in such cases that are non-overlapping and cannot possibly yield True. This mechanism doesn't seem to work for Path and str, allowing to silently let such bugs pass.

To Reproduce

The following code has a bug, because the Path to str does not work as intended

from pathlib import Path

a = "/foo"
b = Path("/foo")

if a == b: # can never be true
    ...

Expected Behavior

I'd have expected a type error similar to the one for comparing e.g. str to int:

error: Non-overlapping equality check (left operand type: "str", right operand type: "Path")  [comparison-overlap]

Actual Behavior

--strict-equality has no effect here, and misses the bug.

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: --strict-equality
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11
@bluenote10 bluenote10 added the bug mypy got something wrong label Oct 6, 2023
@AlexWaygood
Copy link
Member

Fixed in python/typeshed#10662 (but you might have to wait until mypy 1.7 for the fix, not sure it'll make mypy 1.6)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Oct 6, 2023
@AlexWaygood
Copy link
Member

Fun fact: merging that typeshed change into mypy uncovered a bug in mypy itself: take a look at the last commit in #16121

@AlexWaygood AlexWaygood added the topic-overlap Overlapping equality check label Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-overlap Overlapping equality check
Projects
None yet
Development

No branches or pull requests

2 participants