classification
Title: All DocTestCase instances compare and hash equal to each other
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ced, exarkun, ezio.melotti, michael.foord, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2009-12-14 02:23 by exarkun, last changed 2011-12-18 19:20 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
doctestcase_eq_hash.patch ced, 2011-01-30 12:42 review
issue7502.patch ced, 2011-12-18 15:05 review
issue7502.patch ced, 2011-12-18 16:00 review
issue7502.patch ced, 2011-12-18 16:29 review
issue7502.patch ced, 2011-12-18 16:36 review
issue7502.patch ced, 2011-12-18 18:06 review
issue7502-hash.patch ced, 2011-12-18 19:00 review
issue7502-hash.patch ced, 2011-12-18 19:16 review
Messages (17)
msg96368 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2009-12-14 02:23
Consider this example, based on two doctests from Twisted:

  from doctest import DocTestSuite
  import twisted.web2.stream
  docTestOne = DocTestSuite(twisted.web2.stream)._tests[0]
  import twisted.web2.test.test_stream
  docTestTwo = DocTestSuite(twisted.web2.test.test_stream)._tests[0]
  print docTestOne.id(), '==', docTestTwo.id(), '?'
  print docTestOne == docTestTwo

One might reasonably expect a false result, since the two DocTestCase
instances represent two different doctests.  One will meet with
surprise, though.
msg96408 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-12-14 21:55
There doesn't seem to be an __eq__ or __cmp__ method in DocTestCase,
which implies that the issue is with unittest.TestCase (from which
DocTestCase inherits).
msg96410 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2009-12-14 22:00
Or an interaction between TestCase and DocTestCase.  TestCase.__eq__ and
TestCase.__hash__ are both implemented in terms of the _testMethodName
attribute.  It looks like this is *always* "runTest" for a DocTestCase
instance.  It probably doesn't make sense to set a _testMethodName for
DocTestCase instances (maybe it doesn't even make sense for DocTestCase
to subclass TestCase, in fact).  So perhaps overriding comparison and
hashing to use the attributes that do make sense (_dt_test, maybe, I
don't know) would make sense.
msg96411 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-12-14 22:06
Agreed, defining __eq__ and __hash__ on DocTestCase sounds like the way
to go.
msg127526 - (view) Author: Cédric Krier (ced) Date: 2011-01-30 12:42
Here is a patch that defines __eq__ and __hash__ on DocTestCase.
msg149763 - (view) Author: Cédric Krier (ced) Date: 2011-12-18 15:05
New patch with test
msg149771 - (view) Author: Cédric Krier (ced) Date: 2011-12-18 16:00
New patch to add __hash__ and __eq__ to DocTest
msg149775 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-18 16:17
The definition of __eq__ is wrong: it shouldn't compare the hashes since the hash() function isn't injective. For example, 0 and "" have equal hashes, yet they are unequal.
msg149776 - (view) Author: Cédric Krier (ced) Date: 2011-12-18 16:29
Update patch to not use hash in __eq__
msg149777 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-18 16:30
Something I forgot: you should also test the "!=" operator in the tests.
msg149780 - (view) Author: Cédric Krier (ced) Date: 2011-12-18 16:36
Add test for "!="
msg149786 - (view) Author: Cédric Krier (ced) Date: 2011-12-18 18:06
Add also __eq__ to Example and add __ne__ method.
msg149792 - (view) Author: Roundup Robot (python-dev) Date: 2011-12-18 18:32
New changeset c478734ded4b by Antoine Pitrou in branch '3.2':
Issue #7502: Fix equality comparison for DocTestCase instances.
http://hg.python.org/cpython/rev/c478734ded4b

New changeset b8cb6f1e4981 by Antoine Pitrou in branch 'default':
Issue #7502: Fix equality comparison for DocTestCase instances.
http://hg.python.org/cpython/rev/b8cb6f1e4981

New changeset 64d670a8b183 by Antoine Pitrou in branch '2.7':
Issue #7502: Fix equality comparison for DocTestCase instances.
http://hg.python.org/cpython/rev/64d670a8b183
msg149793 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-18 18:36
Patch now committed to all 3 branches. Thanks for contributing!
msg149795 - (view) Author: Cédric Krier (ced) Date: 2011-12-18 19:00
Patch to add __hash__ to prevent warnings in 2.7
msg149796 - (view) Author: Cédric Krier (ced) Date: 2011-12-18 19:16
Add test for __hash__
msg149797 - (view) Author: Roundup Robot (python-dev) Date: 2011-12-18 19:20
New changeset 6a95820b9607 by Antoine Pitrou in branch '2.7':
Followup to #7502: add __hash__ method and tests.
http://hg.python.org/cpython/rev/6a95820b9607
History
Date User Action Args
2011-12-18 19:20:54python-devsetmessages: + msg149797
2011-12-18 19:16:29cedsetfiles: + issue7502-hash.patch

messages: + msg149796
2011-12-18 19:00:43cedsetfiles: + issue7502-hash.patch

messages: + msg149795
2011-12-18 18:36:11pitrousetstatus: open -> closed
resolution: fixed
messages: + msg149793

stage: patch review -> resolved
2011-12-18 18:32:13python-devsetnosy: + python-dev
messages: + msg149792
2011-12-18 18:06:53cedsetfiles: + issue7502.patch

messages: + msg149786
2011-12-18 16:36:57cedsetfiles: + issue7502.patch

messages: + msg149780
2011-12-18 16:30:26pitrousetmessages: + msg149777
2011-12-18 16:29:03cedsetfiles: + issue7502.patch

messages: + msg149776
2011-12-18 16:17:35pitrousetmessages: + msg149775
2011-12-18 16:00:05cedsetfiles: + issue7502.patch

messages: + msg149771
2011-12-18 15:05:47cedsetfiles: + issue7502.patch

messages: + msg149763
2011-12-15 14:42:17ezio.melottisetnosy: + ezio.melotti
2011-01-30 19:40:09pitrousetnosy: exarkun, pitrou, ced, michael.foord
stage: needs patch -> patch review
versions: + Python 3.3, - Python 2.6, Python 3.1
2011-01-30 12:42:23cedsetfiles: + doctestcase_eq_hash.patch

nosy: + ced
messages: + msg127526

keywords: + patch
2009-12-14 22:06:53michael.foordsetmessages: + msg96411
2009-12-14 22:00:12exarkunsetmessages: + msg96410
2009-12-14 21:55:39pitrousetpriority: normal
versions: + Python 3.1, Python 2.7, Python 3.2
nosy: + michael.foord, pitrou

messages: + msg96408

stage: needs patch
2009-12-14 02:23:35exarkuncreate