pschanely / CrossHair

An analysis tool for Python that blurs the line between testing and type systems.
Other
996 stars 47 forks source link

Confusing findings for dictionary constructors #257

Closed tjs-intel closed 5 months ago

tjs-intel commented 6 months ago

Expected vs actual behavior I expect all the dictionary constructors in the example to have indistinguishable behavior. I found that the dict constructor with a generator of tuples yields false positives.

To Reproduce

> crosshair check example.py
***/example.py:31: error: false when calling test_func_broken({'': ''}) (which returns {})
> cat example.py
from typing import Mapping, Hashable, TypeVar

V = TypeVar("V")

def test_func_valid_0(d: Mapping[str, V]) -> dict[str, V]:
  '''
  post: d == __return__
  '''
  return {k: v for k, v in d.items()}

def test_func_valid_1(d: Mapping[str, V]) -> dict[str, V]:
  '''
  post: d == __return__
  '''
  return dict(d)

def test_func_valid_2(d: Mapping[str, V]) -> dict[str, V]:
  '''
  post: d == __return__
  '''
  return dict(d.items())

def test_func_valid_3(d: Mapping[str, V]) -> dict[str, V]:
  '''
  post: d == __return__
  '''
  return dict([(k, v) for k, v in d.items()])

def test_func_broken(d: Mapping[str, V]) -> dict[str, V]:
  '''
  post: d == __return__
  '''
  return dict((k, v) for k, v in d.items())
tjs-intel commented 6 months ago

@pschanely

pschanely commented 6 months ago

Interesting! Thank you for the detailed report. I will investigate today and get back to you!

pschanely commented 5 months ago

I can reproduce - I imagine this may have been a regression since v0.0.50. The fix is nearly ready and I'll be able to release it shortly.

pschanely commented 5 months ago

This should be fixed since v0.0.53! I'd love it if you could try again and see how it goes.

tjs-intel commented 5 months ago

Seems to work now! Thanks for the quick fix!