This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xmorel
Recipients xmorel
Date 2020-11-26.08:23:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1606379035.36.0.842330798084.issue42470@roundup.psfhosted.org>
In-reply-to
Content
In 3.9, using `random.sample` on sets triggers

    DeprecationWarning: Sampling from a set deprecated
since Python 3.9 and will be removed in a subsequent version.

*However* it also triggers on types which implement *both* Sequence and Set, despite Sequence on its own being fine.

The issue is that it first checks for Set and triggers a warning, and only then checks that the input is a sequence:

        if isinstance(population, _Set):
            _warn('Sampling from a set deprecated\n'
                  'since Python 3.9 and will be removed in a subsequent version.',
                  DeprecationWarning, 2)
            population = tuple(population)
        if not isinstance(population, _Sequence):
            raise TypeError("Population must be a sequence.  For dicts or sets, use sorted(d).")

the check should rather be:

        if not isinstance(population, _Sequence):
            if isinstance(population, _Set):
                _warn('Sampling from a set deprecated\n'
                      'since Python 3.9 and will be removed in a subsequent version.',
                      DeprecationWarning, 2)
                population = tuple(population)
            else:
                raise TypeError("Population must be a sequence.  For dicts or sets, use sorted(d).")

this also only incurs a single instance check for `_Sequence` types instead of two.
History
Date User Action Args
2020-11-26 08:23:55xmorelsetrecipients: + xmorel
2020-11-26 08:23:55xmorelsetmessageid: <1606379035.36.0.842330798084.issue42470@roundup.psfhosted.org>
2020-11-26 08:23:55xmorellinkissue42470 messages
2020-11-26 08:23:55xmorelcreate