| 1 | # pylint: disable=undefined-variable, pointless-statement, missing-docstring, line-too-long, expression-not-assigned |
| 2 | # For name-reference see https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries |
| 3 | |
| 4 | # List comprehensions |
| 5 | [x for x in iterable] # [unnecessary-comprehension] |
| 6 | [y for x in iterable] # expression != target_list |
| 7 | [x for x in iterable] # [unnecessary-comprehension] use list(iterable) |
| 8 | [x for x,y,z in iterable] # expression != target_list |
| 9 | [(x, y) for x, y in iterable] # [unnecessary-comprehension] |
| 10 | [(x,y,z) for x,y,z in iterable] # [unnecessary-comprehension] |
| 11 | [(x,y,z) for (x,y,z) in iterable] # [unnecessary-comprehension] |
| 12 | [x for x, *y in iterable] # expression != target_list |
| 13 | [x for x in iterable if condition] # exclude comp_if |
| 14 | [y for x in iterable for y in x] # exclude nested comprehensions |
| 15 | [2 * x for x in iterable] # exclude useful comprehensions |
| 16 | [(x, y, 1) for x, y in iterable] # exclude useful comprehensions |
| 17 | # Test case for issue #4499 |
| 18 | a_dict = {} |
| 19 | b_dict = [(k, v) for k, v in a_dict.items()] # [unnecessary-comprehension] |
| 20 | |
| 21 | # Set comprehensions |
| 22 | {x for x in iterable} # [unnecessary-comprehension] |
| 23 | {y for x in iterable} # expression != target_list |
| 24 | {x for x,y,z in iterable} # expression != target_list |
| 25 | {(x,y,z) for x,y,z in iterable} # [unnecessary-comprehension] |
| 26 | b_dict = {(x,y,z) for (x, y, z) in iterable} # [unnecessary-comprehension] |
| 27 | {(x,y,z) for x in iterable} # expression != target_list |
| 28 | {(x,y,(a,b,c)) for x in iterable} # expression != target_list |
| 29 | {x for x, *y in iterable} # expression != target_list |
| 30 | {x for x in iterable if condition} # exclude comp_if |
| 31 | {y for x in iterable for y in x} # exclude nested comprehensions |
| 32 | |
| 33 | # Dictionary comprehensions |
| 34 | {x: y for x, y in iterable} # [unnecessary-comprehension] |
| 35 | {y: x for x, y in iterable} # key value wrong order |
| 36 | {x: y for (x, y) in iterable} # [unnecessary-comprehension] |
| 37 | {x: y for x,y,z in iterable} # expression != target_list |
| 38 | {x: y for x, y in iterable if condition} # exclude comp_if |
| 39 | {y: z for x in iterable for y, z in x} # exclude nested comprehensions |
| 40 | {x: 1 for x in iterable} # expression != target_list |
| 41 | {2 * x: 3 + x for x in iterable} # exclude useful comprehensions |
| 42 | |
| 43 | # Some additional tests on helptext -- when object is already a list/set/dict |
| 44 | my_list = [] |
| 45 | my_dict = {} |
| 46 | my_set = set() |
| 47 | |
| 48 | [elem for elem in my_list] # [unnecessary-comprehension] |
| 49 | ITEMS = {k: v for k, v in my_dict.items()} # [unnecessary-comprehension] |
| 50 | {k: my_dict[k] for k in my_dict} # [consider-using-dict-items] |
| 51 | {elem for elem in my_set} # [unnecessary-comprehension] |
| 52 | |
| 53 | # Iterating a dict yields its keys, so suggest ``dict(d.keys())`` rather than |
| 54 | # ``dict(d)``, which would just copy the dict (#8256). |
| 55 | DICT_WITH_TUPLE_KEYS = {(1, 2): 3} |
| 56 | {a: b for a, b in DICT_WITH_TUPLE_KEYS} # [unnecessary-comprehension] |