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

Truth value of sets not properly documented #74986

Closed
peterthomassen mannequin opened this issue Jun 29, 2017 · 9 comments
Closed

Truth value of sets not properly documented #74986

peterthomassen mannequin opened this issue Jun 29, 2017 · 9 comments
Assignees
Labels
3.7 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@peterthomassen
Copy link
Mannequin

peterthomassen mannequin commented Jun 29, 2017

BPO 30803
Nosy @rhettinger, @terryjreedy, @peterthomassen
PRs
  • bpo-30803: clarify truth value testing documentation #2508
  • [3.6] bpo-30803: clarify truth value testing documentation (GH-2508) #2946
  • 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 = 'https://github.com/terryjreedy'
    closed_at = <Date 2017-07-29.22:56:58.707>
    created_at = <Date 2017-06-29.13:36:18.082>
    labels = ['type-feature', '3.7', 'docs']
    title = 'Truth value of sets not properly documented'
    updated_at = <Date 2017-07-29.22:56:58.707>
    user = 'https://github.com/peterthomassen'

    bugs.python.org fields:

    activity = <Date 2017-07-29.22:56:58.707>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = True
    closed_date = <Date 2017-07-29.22:56:58.707>
    closer = 'terry.reedy'
    components = ['Documentation']
    creation = <Date 2017-06-29.13:36:18.082>
    creator = 'thomassen'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30803
    keywords = []
    message_count = 9.0
    messages = ['297268', '297312', '297391', '297448', '298099', '298327', '298335', '299483', '299488']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'terry.reedy', 'docs@python', 'thomassen']
    pr_nums = ['2508', '2946']
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue30803'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    @peterthomassen
    Copy link
    Mannequin Author

    peterthomassen mannequin commented Jun 29, 2017

    The truth value of sets is not properly documented, in particular regarding whether an empty set is considered false or not.

    Ignoring primitive (such as numerals) as well as user-defined types, https://docs.python.org/3/library/stdtypes.html#truth says:

    The following values are considered false:

    • [...]
    • any empty sequence, for example, '', (), [].
    • any empty mapping, for example, {}.
    • [...]

    All other values are considered true

    According to https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range, a set is not a sequence (it is unordered, its elements do not have indices, etc.):

    There are three basic sequence types: lists, tuples, and range objects.

    And, according to https://docs.python.org/3/library/stdtypes.html#mapping-types-dict,

    There is currently only one standard mapping type, the dictionary.

    So, as per the documentation, the set type is not a type that can ever be False. However, when I try, bool(set()) evaluates to False.

    When I asked this on Stack Overflow, someone checked in the CPython code and judged that this is most likely a mere documentation issue: https://stackoverflow.com/a/44813565/6867099

    @peterthomassen peterthomassen mannequin added 3.7 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement labels Jun 29, 2017
    @rhettinger
    Copy link
    Contributor

    This case was supposed to be covered by the last bullet point, "instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False.". The word "user-defined" should be dropped.

    Also, the whole section can be simplified to something like:

    """
    By default, objects are considered true unless they define either a __bool__ method that returns False or __len__ method that returns zero.

    Practically, this means that empty containers are false (such as [], (), {}, '', etc) and that numbers equal to zero are false (such as 0, 0.0, 0.0j, False, Decimal(0), Fractions(0, 1), etc). Also, *None* is a false value.

    """

    @rhettinger rhettinger assigned rhettinger and unassigned docspython Jun 29, 2017
    @peterthomassen
    Copy link
    Mannequin Author

    peterthomassen mannequin commented Jun 30, 2017

    I submitted a PR on github, and signed the CLA before doing so. (I double-checked my bpo username in the CLA, and my github username in the bpo profile.) Still, the bot says I need to sign the CLA. I'm not sure what to do?

    @terryjreedy
    Copy link
    Member

    1. You have to go to your profile page
      https://bugs.python.org/user26480
      and add your GitHub name in the GitHub Name box.
    2. A committer has to change the labels to trigger the robot to recheck. I did that but it did not work because of 1.

    As I said in my review, I strongly prefer leaving the bulleted list and making a minimal addition of 'set or' and 'set(), '. I would not merge the current patch.

    @terryjreedy
    Copy link
    Member

    Responding here to Peter's PR comment. Peter opened this issue with the claim that the doc failed a specific test case-- document the truth value of set(). Since mappings are (or can be viewed as) a specialized type of set, I always considered that the empty mapping line implicitly covered sets. But I acknowledge that this is not clear for everyone. The simplest fix to make the test pass would be: "any unordered collection, for example, set(), {}". This should also cover frozenset and a possible frozendict.

    Raymond noted that 'user-defined' in the last bullet point is wrong (it implies that built-in functions are different) and should be deleted. He then combined the corrected rule for false with the default rule in the next sentence to produce a succinct statement of the actual rule. (In CPython, 'default' is literally true. Class 'object' has neither __bool__ nor __len__; ditto for all subclasses that do not add one.)

    With a minor change, I like this statement and agree that it should be moved above the examples. But I think the bullet points should be reduced to just 3, rewritten, and single spaced, but not smashed into running text. I suggest replacing everything between the first sentence (ending with 'below.') and the last two (beginning with 'Operations') with:

    "By default, an object is considered true unless its class defines either a __bool__ method that returns False or __len__ method that returns zero, when called with the object. Here are most of the built-in objects considered false.

    • constants defined to be false: None and False.
    • numeric 0 of any type: 0, 0.0, Decimal(0), Fractions(0, 1)
    • empty sequences and collections: '', (), [], {}, set(), range(0)
      "

    Before writing the above, I checked that an instance attribute __bool__ = lambda: False is not consulted by bool().

    @peterthomassen
    Copy link
    Mannequin Author

    peterthomassen mannequin commented Jul 14, 2017

    I like your most recent suggestion, and updated the PR after fixing a typo ('Fractions') and making it more complete (complex numbers).

    Let me know if anything else is needed.

    (A mapping is not a specialized set, at least as far as typing is concerned: isinstance({}, set) is false. Semantically, they may be related, but the question here is whether the types are actually related.)

    @terryjreedy
    Copy link
    Member

    Raymond, if you either unassign yourself or approve the current PR, I will merge and backport to 3.6 (but not 3.5).

    @rhettinger rhettinger assigned terryjreedy and unassigned rhettinger Jul 21, 2017
    @terryjreedy
    Copy link
    Member

    New changeset caa1280 by Terry Jan Reedy (Peter Thomassen) in branch 'master':
    bpo-30803: clarify truth value testing documentation (bpo-2508)
    caa1280

    @terryjreedy
    Copy link
    Member

    New changeset 4c7b368 by Terry Jan Reedy in branch '3.6':
    [3.6] bpo-30803: clarify truth value testing documentation (GH-2508) (bpo-2946)
    4c7b368

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants