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

Doc: remove errors about mixed-type comparisons. #56276

Closed
terryjreedy opened this issue May 12, 2011 · 54 comments
Closed

Doc: remove errors about mixed-type comparisons. #56276

terryjreedy opened this issue May 12, 2011 · 54 comments
Labels
docs Documentation in the Doc dir easy tests Tests in the Lib/test dir type-feature A feature request or enhancement

Comments

@terryjreedy
Copy link
Member

terryjreedy commented May 12, 2011

BPO 12067
Nosy @rhettinger, @terryjreedy, @mdickinson, @benjaminp, @ezio-melotti, @stevendaprano, @bitdancer, @cjerdonek, @berkerpeksag, @vadmium, @andy-maier, @csabella, @humbdrag
PRs
  • gh-56276: Add tests to test_compare #3199
  • bpo-12067: Add tests to test_compare #30624
  • bpo-12067: Add tests to test_compare #30625
  • bpo-12067: Add tests to test_compare #30651
  • bpo-12067: Add tests to test_compare #30667
  • Dependencies
  • bpo-29321: Wrong documentation (Language Ref) for unicode and str comparison
  • Files
  • issue12067-expressions.diff
  • issue12067-expressions_v2.diff: Minor wording changes.
  • issue12067-expressions_v3.diff: Andy's version of the patch for the 3.5 tip
  • issue12067-expressions_v4.diff: Updates (see message), for v3.5 tip
  • issue12067-expressions-py34_v5.diff: v5 of the patch, targeting 3.4.
  • issue12067-expressions-py34_v6.diff: v6 (same as v5, just now created properly), targeting 3.4.
  • issue12067-expressions-py34_v7.diff: v7 (same as v5, just now created properly), targeting 3.4.
  • issue12067-expressions-py34_v8.diff: v8 (improved doc and added tests), targeting 3.4.
  • try_eq.py: Test program for py34 for equality of collections
  • try_eq.out: Output of try_eq.py with cpython 3.4.1
  • issue12067-expressions-py34_v9.diff: v9 of the patch, targeting 3.4.
  • issue12067-expressions-py34_v10.diff: v10 of the patch, targeting 3.4
  • issue12067-expressions-py34_delta-v9-v10.diff: delta between v9 and v10 of the patch
  • issue12067-expressions-py34_v11.diff: v11 of the patch, targeting 3.4
  • issue12067-expressions-py34_v12.diff: v12 of the patch, targeting 3.4
  • issue12067-expressions-py3.5_v13.diff
  • issue12067-expressions-py3.5_v14.diff: v14 of the patch, targeting 3.5 (default)
  • issue12067-expressions-py3.6_v15.diff
  • issue12067-expressions-py3.6_v16.diff
  • expressions-py2.7.diff
  • expressions-py2.7_v17.diff
  • expressions-py3.7_v17.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2011-05-12.21:59:33.279>
    labels = ['easy', 'type-feature', 'tests', 'docs']
    title = 'Doc: remove errors about mixed-type comparisons.'
    updated_at = <Date 2022-01-26.02:09:04.532>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2022-01-26.02:09:04.532>
    actor = 'terry.reedy'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation', 'Tests']
    creation = <Date 2011-05-12.21:59:33.279>
    creator = 'terry.reedy'
    dependencies = ['29321']
    files = ['27256', '27442', '35843', '35845', '35850', '35851', '35853', '35924', '35938', '35939', '35971', '36896', '36897', '36920', '36978', '38183', '38303', '39970', '40052', '46372', '46398', '46399']
    hgrepos = []
    issue_num = 12067
    keywords = ['patch', 'easy']
    message_count = 53.0
    messages = ['135873', '135875', '135890', '148760', '148774', '170887', '170936', '170952', '170953', '170966', '171012', '172148', '183786', '222204', '222209', '222254', '222257', '222270', '222275', '222276', '222278', '222310', '222442', '222747', '222772', '222938', '223217', '226496', '226522', '226529', '228769', '229217', '229229', '229236', '229240', '229245', '229327', '229328', '229721', '236269', '237069', '247079', '247088', '247559', '251405', '251406', '285948', '286128', '286439', '295145', '295222', '300787', '411699']
    nosy_count = 17.0
    nosy_names = ['rhettinger', 'terry.reedy', 'mark.dickinson', 'benjamin.peterson', 'ezio.melotti', 'steven.daprano', 'r.david.murray', 'cvrebert', 'chris.jerdonek', 'docs@python', 'python-dev', 'berker.peksag', 'martin.panter', 'mikehoy', 'andymaier', 'cheryl.sabella', 'humbdrag']
    pr_nums = ['3199', '30624', '30625', '30651', '30667']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue12067'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5', 'Python 3.6']

    Linked PRs

    @terryjreedy
    Copy link
    Member Author

    Current 3.2 doc, 5.9. Comparisons, has this paragraph about mixed-type comparisons.

    "The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, the == and != operators *always* consider objects of different types to be unequal, while the <, >, >= and <= operators raise a TypeError when comparing objects of different types that do not implement these operators for the given pair of types. You can control comparison behavior of objects of non-built-in types by defining rich comparison methods like __gt__(), described in section Basic customization."

    Sentence 3: "If both are numbers, they are converted to a common type." I suspect it would be more true to say 'common internal type' as I would not think it a language requirement to produce Python objects.

    In any case, I think it is only true for built-in number types, and I do not see that qualification anywhere previously.

    That aside, it does not appear to be true for Decimals and Fractions in 2.7.1.

    Sentence 4: first clause is only true for built-in types. That qualification is not obvious to everyone, as evidenced by a current python-list sub thread.

    For 2.7, which has a different continuation, I suggest adding 'built-in' before 'objects of'.
    For 3.2/3, I suggest deleting '*always*' and adding a comma after 'TypeError' so that the 'when' condition applies to equality comparisons also.

    After discussion about same-type comparisons, there is another paragraph about mixed-type comparison:

    "Comparison of objects of the differing types depends on whether either of the types provide explicit support for the comparison. Most numeric types can be compared with one another, but comparisons of float and Decimal are not supported to avoid the inevitable confusion arising from representation issues such as float('1.1') being inexactly represented and therefore not exactly equal to Decimal('1.1') which is. When cross-type comparison is not supported, the comparison method returns NotImplemented. This can create the illusion of non-transitivity between supported cross-type comparisons and unsupported comparisons. For example, Decimal(2) == 2 and 2 == float(2) but Decimal(2) != float(2)."

    I suggest deleting this entirely. The first sentence and first clause of the second repeat what was said above. The rest is obsolete as float/decimal comparisons *are* implemented in 2.7.1 and 3.2.0.

    @terryjreedy terryjreedy added docs Documentation in the Doc dir easy labels May 12, 2011
    @ezio-melotti
    Copy link
    Member

    Can you provide a patch?

    @mdickinson
    Copy link
    Member

    [Docs]
    "If both are numbers, they are converted to a common type."

    [Terry]
    "In any case, I think it is only true for built-in number types,"

    It's not even true for built-in number types. When comparing an int with a float, it's definitely *not* the case that the int is converted to a float and the floats compared. And that's for good reason: the int -> float conversion is lossy for large integers, so if int <-> float comparisons just converted the int to a float before comparing, we'd have (for example):

    >> 10**16 == 1e16 == 10**16 + 1

    leading to broken transitivity of equality, and strange dict and set behaviour.

    So int <-> float comparisons do a complicated dance under the hood to compare the exact numerical values of the two objects and produce the correct result.

    I'm not sure what the intent of the original sentence was, or how to reword it. The key point is simply that it *is* possible to compare an int with a float, and that the result is sensible, based on numeric values.

    @ezio-melotti
    Copy link
    Member

    Would it be ok to state that:

    1. <, >, ==, >=, <=, and != compare the values of two objects;
    2. the two objects don't necessarily have to be of the same type;
    3. with == and !=, objects of different types compare unequal, unless they define a specific __eq__ and/or __ne__;
    4. with <, >, <=, and >=, the comparison of objects of different types raises a TypeError, unless they define specific __lt__, __gt__, __le__, and __ge__;
    5. some built-in types define these operations, so it's possible to compare e.g. int and floats;

    This should summarize the possible behaviors. There's no reason IMHO to expose implementation details and to special case built-in types (unless their comparison is actually different and doesn't depend on __eq__, __ne__, etc.).

    @terryjreedy
    Copy link
    Member Author

    In Python 3, where all classes inherit from object, the default rules are, by experiment, (which someone can verify from the code) simpler than you stated.

    1. By default, == and /= compare identities.
    2. By default, order comparisons raise TypeError.
      ob <= ob raises even though ob == ob because ob is ob.

    I am not sure of the method look-up rules for rich comparisons, but perhaps the following are true:

    1. with == and !=, an object is equal to itself and different objects (a is not b) compare unequal, unless the class of the first define a specific __eq__ and __ne__;

    2. with <, >, <=, and >=, comparison raises a TypeError, unless the class of the first object defines specific __lt__, __gt__, __le__, and __ge__, or the class of the second defines the reflected method (ge reflects __lt__, etcetera);

    What is not clear to me is whether the reflected method is called if the first raises TypeError. The special method names doc (reference 3.3) says "A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments.
    ...
    There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other’s reflection, __le__() and __ge__() are each other’s reflection, and __eq__() and __ne__() are their own reflection."

    Does 'not supported' mean 'raises TypeError', 'returns NotImplemented', or both? If the last, I don't really understand the reason for NotImplemented versus TypeError. That point should be clarified in 3.3 also. And 3.3 should be referenced in the comparisons section.

    I think point 5 should say a bit more: builtin numbers compare as expected, even when of different types; builtin sequences compare lexicographically.

    @ezio-melotti ezio-melotti added the type-feature A feature request or enhancement label Sep 21, 2012
    @mikehoy
    Copy link
    Mannequin

    mikehoy mannequin commented Sep 21, 2012

    http://bugs.python.org/issue15997 is this issue related to what Terry has mentioned:

    Does 'not supported' mean 'raises TypeError', 'returns NotImplemented', or both? If the last, I don't really understand the reason for NotImplemented versus TypeError. That point should be clarified in 3.3 also. And 3.3 should be referenced in the comparisons section.

    I am working on this patch and need confirmation as to whether or not this has to be included in my patch. I'm not clear on it so I may just pass on making a patch if it is required for this issue.

    @terryjreedy
    Copy link
    Member Author

    After further thought: This section is about the syntax operators, not the special methods. The syntax operators never evaluate to NotImplemented as they (apparently) interpret its return from a special method the same as a raising of TypeError, and always raise TypeError when neither the op or its reflection is supported. So there should be no mention of NotImplemented here. Just a reference to 3.3. bpo-15997 is related to my 'wonder' but not directly relevant to a patch for this. Please submit a draft patch when you have one.

    I determined that 'raise TypeError' and 'return NotImplemented' both result in the call of the reflected method, at least for a couple of cases. (And same seems true for arithmetic ops too.)

    class C():
        def __ge__(self, other):
            # print("in C.__ge__", end='')
            return True
        def __add__(self, other):
            return 44
        __radd__ = __add__
    
    class O():
        def __le__(self, other):
            # print ("in O.__le__")
            return NotImplemented
        def __add__(self, other):
            return NotImplemented
        
    c = C()
    o = O()
    ob = object() 
    print(c >= o, o <= c, ob <= c)
    # True True True
    # print(ob <= ob) # raises TypeError
    print(c + o, o + c, ob + c)
    # 44 44 44
    # print(ob + ob)  # raises TypeError
    # print(ob >= o)  # with O.__le__ print uncommented
    # in O.__le__  # so non-implemented reflected o <= ob *is* called
    # TypeError: unorderable types: object() >= O()

    @mikehoy
    Copy link
    Mannequin

    mikehoy mannequin commented Sep 22, 2012

    I've attempted to incorporate both Terry's and Ezio's suggestions. Here is a patch to get started with. There is a section that has been deleted. Patch uploaded.

    @cjerdonek
    Copy link
    Member

    Some minor comments:

    -The operators <, >, ==, >=, <=, and != compare the
    +<, >, ==, >=, <=, and != compare the values of two

    I think it reads better to start a sentence (and in this case a paragraph) with a word rather than a symbol.

    -values of two objects. The objects need not have the same type. If both are
    +objects. The two objects don't necessarily have to be of the same type. With

    The replacement sentence seems wordier to me.

    +(:meth:`__ge__()` reflects :meth:`__lt__()`, etcetera). Builtin numbers compare
    +as expected, even when of different types. Builtin sequences compare

    "Built-in" is hyphenated in the docs. See, for example, here:

    http://docs.python.org/dev/library/functions.html

    @mdickinson
    Copy link
    Member

    I determined that 'raise TypeError' and 'return NotImplemented' both
    result in the call of the reflected method

    Are you sure? raise TypeError *should* result in the operation being abandoned, with the reflected operation not tried.

    Python 3.3.0rc2+ (default:3504cbb3e1d8, Sep 20 2012, 22:08:44) 
    [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class A:
    ...     def __add__(self, other):
    ...         raise TypeError("Don't know how to add")
    ...     def __le__(self, other):
    ...         raise TypeError("Can't compare")
    ... 
    [65945 refs]
    >>> class B:
    ...     def __radd__(self, other):
    ...         return 42
    ...     def __ge__(self, other):
    ...         return False
    ... 
    [66016 refs]
    >>> A() <= B()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 5, in __le__
    TypeError: Can't compare
    [66064 refs]
    >>> A() + B()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in __add__
    TypeError: Don't know how to add
    [66065 refs]

    @terryjreedy
    Copy link
    Member Author

    You are right, I did not look deep enough. I was fooled by the conversion of NotImplemented, returned from object.__le__, etc, to TypeError. Sorry for that noise.

    For comparison and arithmetic, the actual alternative to defining a function that returns NotImplemented seems to be to not define it at all.

    class C():
        def __ge__(self, other):
            return True
        def __add__(self, other):
            return 44
        __radd__ = __add__
    
    class O():
        pass  # removed NotImplemented defs
        
    c = C()
    o = O()
    print(c >= o, o <= c)
    # True True
    print(c + o, o + c)
    # 44 44

    (I looked at the codes for binary_op1 in abstract.c and do_richcompare in object.c and do not yet see any effective difference between not defined and a NotImplemented return.)

    I'll take a look at the patch later.

    @mikehoy
    Copy link
    Mannequin

    mikehoy mannequin commented Oct 5, 2012

    Changed patch to include suggestions by Chris Jerdonek.

    http://bugs.python.org/issue12067#msg170953

    @mikehoy
    Copy link
    Mannequin

    mikehoy mannequin commented Mar 9, 2013

    Considering that the docs have changed does this issue still need to be open?

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 3, 2014

    Hi, I'd like to revive this issue.

    IMHO, the changes in bpo-12067-expressions_v2.diff go too far. I don't think that deleting the entire section about the details of comparing objects of the same type makes sense.

    I agree with Terry's statement in msg170936 that the chapter is about the operators and not about the ways to customize them, so some of what the patch introduces in that area should not be introduced.

    So far, that means that I'm pretty much against that patch entirely...

    Having said that, I do believe that there are still issues:

    1. both the 2.7 and 3.x sections about the comparison operators are sufficiently convoluted and could be organized better by grouping the various statements that are made, into categories like this:
    • comparisons involving objects of user-defined types
    • comparing objects of same built-in type
    • comparing objects of differing built-in type
    1. There are still some errors, ambiguities and omissions that need to be fixed. For example, in the 3.x version:

    a) omission about treatment of NaN for numbers of different type (could in theory be implied from the statement "are converted to same type", but that statement is problematic as was pointed out in Mark's comment on this issue).

    b) Amgiguous statement "Most numeric types [of same type] can be compared with one another.". I think what is true is that all built-in numeric types (including Fraction and Decimal) compare mathematically correct in 3.x, and that the only non-support is that complex numbers are not considered orderable and an attempt to use an ordering operator raises TypeError.

    c) Ambiguous statement "When cross-type comparison is not supported, the comparison method returns NotImplemented.". If this is about the customization methods, it should not be here, but there. Here, it is relevant that a TypeError is raised when using the operator.

    d) Terminology in "Bytes objects are compared lexicographically using the numeric values of their elements.": Chapter [4.8.1. Bytes] defines bytes objects as immutable sequences of single bytes (not elements).

    e) Terminology in "Tuples and lists are compared lexicographically using comparison of corresponding elements.": lists and tuples contain "items" not "elements", and an item-wise comparison should not be called "lexicographically" because that makes sense only when the items are characters.

    f) Ambiguity in "If not equal, the sequences are ordered the same as their first differing elements.": "Are ordered" could be interpreted (e.g. by non-native speakers) to mean that the sequence is changed to achieve that ordering, which is not the case of course.

    g) Editorial: In the list item about sets and froze sets, the example set {2,3} is not in example font.

    h) Omission: Range types are not covered.

    i) Omission: The section "Comparison of objects of differing types..." is silent about which built-in types support comparison across types (except for numeric types where that is covered). I think that should be explicitly listed for those built-in types that are also listed explicitly in the section about comparing objects of same type.

    I'll try to come up with a patch for 3.x, and once that is agreed, with one for 2.x.

    Andy

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 3, 2014

    Uploaded bpo-12067-expressions_v3.diff for the 3.5 tip.
    Please review.

    @rhettinger rhettinger assigned rhettinger and unassigned docspython Jul 3, 2014
    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 4, 2014

    Uploaded bpo-12067-expressions_v4.diff to improve the unicode footnote 3, and to revert to using the term "lexicographical" for sequences (after learning that it applies there as well). Also, this version was produced using "hg diff" to make it properly reviewable.

    Please review.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 4, 2014

    PS: The v4 patch does not address comments f) and h) from msg222204, and it seems to me they do not need to be addressed.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 4, 2014

    Terry,
    I'd like to comment on your statement:

    1. By default, == and /= compare identities.
      in msg148774.

    What experiment lead you to that conclusion?

    Here is one that contradicts it (using cpython 3.4.1):

    >>> i1 = 42
    >>> f1 = 42.0
    >>> i1 == f1
    True
    >>> i1 is f1
    False

    Is it possible, that your experiment got influenced by the optimization that attempts to reuse existing objects of immutable types?
    Like in this:

    >>> i1 = 42
    >>> i2 = 40 + 2
    >>> i1 == i2
    True
    >>> i1 is i2
    True

    Andy

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 4, 2014

    Uploaded v5 of the patch.

    Changes:

    1. The statement that comparison of different built-in types (always) raises TypeError, was too general. Changed to distinguish equal and order operators, as summarized by Ezio in items 3) and 4) of msg148760.

    2. Ensured max line length of 80, in text areas affected by the patch.

    Andy

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 4, 2014

    It seems I still need to practice creating patches ... uploading v6 which should create a review link. No other changes.
    Sorry for that.
    Andy

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 4, 2014

    Another attempt. Really sorry...

    @terryjreedy
    Copy link
    Member Author

    In py3, *everything* is an instance of class object. This makes like simple than in 2.x. The default comparison rules are set by the rich comparison methods of object. 'By experiment' meant by experiments with instances of object, which use those default methods, rather than by inspection of the relevant .c source code. Instances of subclasses taht do not override the defaults would act the same. Here is what seem to be the default code, from object.c, do_compare. It verifies what I said (v, w are pointers, which represent identity):

        /* If neither object implements it, provide a sensible default
           for == and !=, but raise an exception for ordering. */
        switch (op) {
        case Py_EQ:
            res = (v == w) ? Py_True : Py_False;
            break;
        case Py_NE:
            res = (v != w) ? Py_True : Py_False;
            break;
        default:
            /* XXX Special-case None so it doesn't show as NoneType() */
            PyErr_Format(PyExc_TypeError,
                         "unorderable types: %.100s() %s %.100s()",
                         v->ob_type->tp_name,
                         opstrings[op],
                         w->ob_type->tp_name);
            return NULL;
        }
        Py_INCREF(res);
        return res;

    Subclasses can and ofter do override the default methods. In particular, the number subclasses compare by value, across number types.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 7, 2014

    I see.
    But I don't think it is a sensible default, as the source code states.

    The Python doc (v2 and v3) is quite consistent in stating that == compares the values of two objects, while is compares object identity.

    Having a default implementation on the object type that implements == by comparing object identity is not consistent with that.

    -> Can someone please elaborate what the reason for that is?

    -> Where is the discrepancy between the documentation of == and its default implementation on object documented?

    To me, a sensible default implementation for == on object would be (in Python):

      if v is w:
        return True;
      elif type(v) != type(w):
        return False
      else:
        raise ValueError("Equality cannot be determined in default implementation")

    Andy

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Jul 11, 2014

    Uploaded v8 of the patch for 3.4 and default.

    It reflects hopefully everything that was said in this issue thread, and on the python-dev mailing list (subject: == on object tests identity in 3.x), at least to the extent it was related to comparisons.

    Besides the doc changes it contained previously, it now also contains improvements for the test suite for comparisons (lib/test/test_compare.py).

    -> Please review both.

    Andy

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Oct 13, 2014

    Uploading v10 of the patch, which addresses all review comments made on v9.

    There is one open question back to Martin Panter about which different types of byte sequences can be compared in Py 3.4.

    I also believe this patch addresses all of bpo-22001. Let me know if you find that that is not the case.

    If we continue to scope this patch to only the comparison chapter of the language reference, then I think we are done (see msg229229 about other places that need review and possibly updates).

    Please review the patch v10.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Oct 13, 2014

    Here is the delta between v9 and v10 of the patch, if people want to see just that.

    @vadmium
    Copy link
    Member

    vadmium commented Oct 13, 2014

    About the byte sequence comparisons, I wondered if it was misleading to say that a list(), tuple() or range() can only be compared to the same type, without mentioning that bytes() and bytearray() can be compared to each other.

    BTW just noticed you say range() supports lexicographical ordering, which I think is incorrect.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Oct 14, 2014

    I have addressed the comments by Jim Jewett, Martin Panter and of myself in a new version v11, which got posted.

    For the expression.rst doc file, this version of the patch has its diff sections in a logical order, so that the original text and the patched text are close by each other.

    Please review.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Oct 14, 2014

    I also made sure in both files that the line length of any changed or new lines is max 80. Sorry if that creates extra changes when looking at deltas between change sets.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Oct 20, 2014

    I have posted v12 of the patch, which addresses all comments since v11.

    This Python 3.4 patch can be applied to the "default" (3.5 dev) branch as well.

    I will start working on a similar patch for Python 2.7 now.

    @vadmium
    Copy link
    Member

    vadmium commented Feb 20, 2015

    bpo-4395 is already open for the != delegating to __eq__ issue that Guido pointed out earlier.

    Yet another issue that this doc patch should solve: bpo-22000.

    I am posting v13 of the patch that works with the current “default” (3.5) branch. Minor modifications to the documentation part:

    • Undo some paragraph reflowing to reduce the size of the diff hunks
    • Use math.nan instead of nan = float('NaN')
    • Tweaked the new subheadings to better differentiate “Value comparisons” (which this bug is concerned with) from “Identity comparisons”

    Changes I made to the added test cases:

    • Drop Python 3 version check
    • Use TestCase.subTest() so it’s easier to see everything that [is] was failing
    • Merge some tests for != and fix changed expectations due to bpo-21408 being fixed
    • Drop _inst_str() and __str__(), apparently unused

    It would be nice to see at least the documentation part reviewed and committed, even if the tests require more work. (After having to hack the tests to get them working, I might point out some odd bits in the code review.) Though I guess they are just tests, so it doesn’t matter so much as long as they pass.

    @andy-maier
    Copy link
    Mannequin

    andy-maier mannequin commented Mar 2, 2015

    I have posted v14 of the patch (for the 3.5 'default' branch), based on Martin's v13. v14 addresses all comments Martin made, as described in my responses to them (see patch set 10).

    On bpo-4395: That issue should be pursued in addition to this issue; it seems Martin's patch for it is complementary to the patch for this issue here.

    On bpo-22000: I agree that that issue is addressed by the patch for this issue here.

    All: Please review the v14 patch.

    @vadmium
    Copy link
    Member

    vadmium commented Jul 22, 2015

    Patch v15. No doc changes, but I refactored the test code:

    • Manually merged with recent changes
    • Separate assert_equality_only() and assert_total_order() test methods. Hopefully this is a bit simpler for people to understand and review, and avoids suggesting that partial ordering is tested.
    • Dropped subTest() instances with identical parameters. The basic stack trace can already distinguish these.
    • Eliminated is_value_comparable(); remove the ”meth” parameters from the call sites instead

    @berkerpeksag
    Copy link
    Member

    I think we can commit documentation and tests separately. I just did a quick review of the test changes and I will add some review comments later (sorry, lack of time :)).

    @vadmium
    Copy link
    Member

    vadmium commented Jul 29, 2015

    I can split out a documentation-only patch if it would help get that committed.

    In the meantime, patch v16 includes some fixups to comments etc in the test code that I missed myself.

    @vadmium vadmium added the tests Tests in the Lib/test dir label Jul 29, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 23, 2015

    New changeset 1fc049e5ec14 by Martin Panter in branch '3.4':
    Issue bpo-12067: Rewrite Comparisons section in the language reference
    https://hg.python.org/cpython/rev/1fc049e5ec14

    New changeset b6698c00265b by Martin Panter in branch '3.5':
    Issue bpo-12067: Merge comparisons doc from 3.4 into 3.5
    https://hg.python.org/cpython/rev/b6698c00265b

    New changeset 294b8a7957e9 by Martin Panter in branch 'default':
    Issue bpo-12067: Merge comparisons doc from 3.5
    https://hg.python.org/cpython/rev/294b8a7957e9

    @vadmium
    Copy link
    Member

    vadmium commented Sep 23, 2015

    I committed the changes to expressions.rst for 3.4+. That still leaves the changes to test_compare.py, and possibly changes for 2.7.

    Andy: In msg229721 you mentioned a potential 2.7 patch. Did you get anywhere with that? Even if it is only half finished, someone else may be able to keep working on it.

    @vadmium
    Copy link
    Member

    vadmium commented Jan 21, 2017

    Here is a port of the documentation to Python 2. Main differences:

    • Default rules for order comparisons are different
    • Not all kinds of objects inherit from object()
    • str(), unicode() compatibility
    • xrange() only seems to have default comparability
    • NAN, “binary sequences” and sets not listed

    @vadmium
    Copy link
    Member

    vadmium commented Jan 24, 2017

    Updated patch for 2.7, which I plan to commit soon. Corresponding Py 3 patch coming soon.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 29, 2017

    New changeset 8c9a86aa222e by Martin Panter in branch '3.5':
    Issue bpo-12067: Recommend that hash and equality be consistent
    https://hg.python.org/cpython/rev/8c9a86aa222e

    New changeset 9702c5f08df1 by Martin Panter in branch '3.6':
    Issues bpo-12067: Merge hash recommendation from 3.5
    https://hg.python.org/cpython/rev/9702c5f08df1

    New changeset 9dbb7bbc1449 by Martin Panter in branch 'default':
    Issues bpo-12067: Merge hash recommendation from 3.6
    https://hg.python.org/cpython/rev/9dbb7bbc1449

    New changeset 8a9904c5cb1d by Martin Panter in branch '2.7':
    Issue bpo-12067: Rewrite Comparisons section in the language reference
    https://hg.python.org/cpython/rev/8a9904c5cb1d

    @csabella
    Copy link
    Contributor

    csabella commented Jun 4, 2017

    It appears all the patches for this issue have been applied. Is the only open item the changes to test_compare?

    @vadmium
    Copy link
    Member

    vadmium commented Jun 5, 2017

    Yes I think I committed all the documentation. Someone needs to decide whether to use Andy’s tests as they are, or perhaps modify or drop some or all of them.

    @csabella
    Copy link
    Contributor

    I've created a PR for the changes to test_compare from v16 of the patch.

    @terryjreedy
    Copy link
    Member Author

    Martin: [tests need review]. Yep. Improved constant folding and elimination of duplication now causes many 'is not' tests to fail. Language tests should not test current implementation limitations.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @terryjreedy
    Copy link
    Member Author

    I revised #3199, cutting out duplicated instances and all tests involving identity, not just the 3 that fail now . The result is about half as big.

    terryjreedy added a commit that referenced this issue May 20, 2023
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 20, 2023
    (cherry picked from commit 68ee8b3)
    
    Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    terryjreedy added a commit that referenced this issue May 20, 2023
    gh-56276: Add tests to test_compare (GH-3199)
    (cherry picked from commit 68ee8b3)
    
    Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir easy tests Tests in the Lib/test dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    9 participants