-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Improve idmaker name selection in case of duplicate ids in parametrize #1474
Changes from 2 commits
b8c15a0
412042d
c66aedf
3ffce6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import types | ||
import sys | ||
import math | ||
import collections | ||
|
||
import py | ||
import pytest | ||
|
@@ -1152,9 +1153,14 @@ def _idvalset(idx, valset, argnames, idfn, ids): | |
def idmaker(argnames, argvalues, idfn=None, ids=None): | ||
ids = [_idvalset(valindex, valset, argnames, idfn, ids) | ||
for valindex, valset in enumerate(argvalues)] | ||
if len(set(ids)) < len(ids): | ||
# user may have provided a bad idfn which means the ids are not unique | ||
ids = [str(i) + testid for i, testid in enumerate(ids)] | ||
duplicates = [testid for testid in ids if ids.count(testid) > 1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A faster way to check for duplicates: has_duplicates = len(ids) != len(set(ids))
if has_duplicates: While usually it won't matter, for a large list of test ids the difference might be significant: $python -m timeit -s "ids = list(range(1000))" "[testid for testid in ids if ids.count(testid) > 1]"
100 loops, best of 3: 14.2 msec per loop
$python -m timeit -s "ids = list(range(1000))" "len(ids) != len(set(ids))"
10000 loops, best of 3: 22.2 usec per loop If I increase the number of ids to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first i used:
which is more efficient but Counter is 2.7+. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I figured. 😉 Thanks! 😄 |
||
if duplicates: | ||
# The ids are not unique | ||
counters = collections.defaultdict(lambda: 0) | ||
for index, testid in enumerate(ids): | ||
if testid in duplicates: | ||
ids[index] = testid + str(counters[testid]) | ||
counters[testid] += 1 | ||
return ids | ||
|
||
def showfixtures(config): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include a "thanks palaviv" and mention #1474. You might want to do the same for the item just above this one. 😉