title: Mocking the datetime library in pypy raises TypeError when comparing 2 mocked objects
body: <p>I'm doing some tests for my python library which is supposed to work on all python versions - 27, 33, 34, 35, 36 and <strong>pypy</strong>. In my tests I'm mocking the <code>datetime</code> library and they all work, except when I run them in <code>pypy</code> (see why I bolded that earlier? such a plot twist!).</p>
  
  <p>Here's a MCVE for the trouble I'm having:</p>
  
  <pre><code>import mock
  import datetime as dtl
  mk = mock.Mock(wraps=dtl.datetime)
  p = mock.patch('datetime.datetime', mk)
  p.start()
  from datetime import datetime
  d1 = datetime.now()
  d2 = datetime.now()
  print d1 == d2
  </code></pre>
  
  <p>In all python versions the last line returns <code>False</code>. In <code>pypy</code> the last line throws:</p>
  
  <pre><code>&gt;&gt;&gt;&gt; d1 == d2
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/lib/pypy/lib_pypy/datetime.py", line 1764, in __eq__
      if isinstance(other, datetime):
  TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
  </code></pre>
  
  <p>I dug through <code>pypy</code>'s source code trying to understand the problem, and why on earth are there differences between it and other python versions, and I found the following:</p>
  
  <pre><code># Comparisons of datetime objects with other.
  
  def __eq__(self, other):
      if isinstance(other, datetime):
          return self._cmp(other) == 0
      elif hasattr(other, "timetuple") and not isinstance(other, date):
          return NotImplemented
      else:
          return False
  </code></pre>
  
  <p>This kind of makes sense. The <code>datetime</code> class is now a <code>Mock</code> object - and python insists that the second argument to <code>isinstance</code> will be a type. Neato.</p>
  
  <p>So I decided to look at cpython's implementation of <code>datetime</code>, and surprise surprise:</p>
  
  <pre><code># Comparisons of datetime objects with other.
  
  def __eq__(self, other):
      if isinstance(other, datetime):
          return self._cmp(other, allow_mixed=True) == 0
      elif not isinstance(other, date):
          return NotImplemented
      else:
          return False
  </code></pre>
  
  <p>The same exact verification is done here, yet it does not raise anything <code>¯\_(ツ)_/¯</code>.</p>
  
  <p>I'll add that this happens in python 2.7:</p>
  
  <pre><code>&gt;&gt;&gt; d = datetime.now()
  &gt;&gt;&gt; d
  datetime.datetime(2017, 9, 7, 9, 31, 50, 838155)
  &gt;&gt;&gt; d == d
  True
  &gt;&gt;&gt; datetime
  &lt;Mock id='139788521555024'&gt;
  &gt;&gt;&gt; isinstance(d, datetime)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
  </code></pre>
  
  <p>There are 2 questions I'm struggling with:</p>
  
  <ol>
  <li>Why on earth does it work for cpython? </li>
  <li>How can I mock the datetime object in a way that this will work without reimplementing <code>__eq__</code> or any other magic method :)</li>
  </ol>
  
  <p>Much obliged!</p>