-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
DeprecationWarning triggers for sequences which happen to be sets as well #86636
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
Comments
In 3.9, using
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 |
+0 Do you want to submit a PR for this? Some thoughts:
|
Sure. Do you think the code I proposed would be suitable?
Yes, that was my basis for it as it seemed sensible, but you're right that it's a bit of a behavioural change as you note:
Aye, and also I guess the "sequence" implementation of the input collection might be less efficient than one-shot converting to a set and sampling from the set.
Chances are you're right, but it's what got me to stumble upon it ($dayjob makes significant use of a "unique list / ordered set" smart-ish collection, that collection was actually registered against Set and Sequence specifically because Python 3's random.sample typechecks those, we registered against both as the collection technically implements both interfaces so that seemed like a good idea at the time). |
Yes. It will need tests and a news entry as well. |
I was preparing to open the PR but now I'm doubting: should I open the PR against master and miss islington will backport it, or should I open the PR against 3.9? |
Open it against master. |
Tried patterning the PR after the one which originally added the warning. Wasn't too sure how the news item was supposed to be generated, and grepping the repository didn't reveal any clear script doing that, so I made up a date and copied an existing random bit (which I expect is just a deduplicator in case multiple NEWS items are created at the same instant?) |
Given that this is rare and that it is a behavior change, I'm only applying this to 3.10. If you think it is essential for 3.9, feel free to reopen and we'll discuss it with the release manager. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: