blender.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # This module contains the logic for "blending" of errors.
  2. # Since prospector runs multiple tools with overlapping functionality, this
  3. # module exists to merge together equivalent warnings from different tools for
  4. # the same line. For example, both pyflakes and pylint will generate an
  5. # "Unused Import" warning on the same line. This is obviously redundant, so we
  6. # remove duplicates.
  7. from collections import defaultdict
  8. import pkg_resources
  9. import yaml
  10. __all__ = (
  11. "blend",
  12. "BLEND_COMBOS",
  13. )
  14. def blend_line(messages, blend_combos=None):
  15. """
  16. Given a list of messages on the same line, blend them together so that we
  17. end up with one message per actual problem. Note that we can still return
  18. more than one message here if there are two or more different errors for
  19. the line.
  20. """
  21. blend_combos = blend_combos or BLEND_COMBOS
  22. blend_lists = [[] for _ in range(len(blend_combos))]
  23. blended = []
  24. # first we split messages into each of the possible blendable categories
  25. # so that we have a list of lists of messages which can be blended together
  26. for message in messages:
  27. key = (message.source, message.code)
  28. found = False
  29. for blend_combo_idx, blend_combo in enumerate(blend_combos):
  30. if key in blend_combo:
  31. found = True
  32. blend_lists[blend_combo_idx].append(message)
  33. # note: we use 'found=True' here rather than a simple break/for-else
  34. # because this allows the same message to be put into more than one
  35. # 'bucket'. This means that the same message from pycodestyle can 'subsume'
  36. # two from pylint, for example.
  37. if not found:
  38. # if we get here, then this is not a message which can be blended,
  39. # so by definition is already blended
  40. blended.append(message)
  41. # we should now have a list of messages which all represent the same
  42. # problem on the same line, so we will sort them according to the priority
  43. # in BLEND and pick the first one
  44. for blend_combo_idx, blend_list in enumerate(blend_lists):
  45. if len(blend_list) == 0:
  46. continue
  47. # pylint:disable=cell-var-from-loop
  48. blend_list.sort(
  49. key=lambda msg: blend_combos[blend_combo_idx].index(
  50. (msg.source, msg.code),
  51. ),
  52. )
  53. if blend_list[0] not in blended:
  54. # We may have already added this message if it represents
  55. # several messages in other tools which are not being run -
  56. # for example, pylint missing-docstring is blended with pydocstyle
  57. # D100, D101 and D102, but should not appear 3 times!
  58. blended.append(blend_list[0])
  59. # Some messages from a tool point out an error that in another tool is handled by two
  60. # different errors or more. For example, pylint emits the same warning (multiple-statements)
  61. # for "two statements on a line" separated by a colon and a semi-colon, while pycodestyle has E701
  62. # and E702 for those cases respectively. In this case, the pylint error will not be 'blended' as
  63. # it will appear in two blend_lists. Therefore we mark anything not taken from the blend list
  64. # as "consumed" and then filter later, to avoid such cases.
  65. for now_used in blend_list[1:]:
  66. now_used.used = True
  67. return [m for m in blended if not getattr(m, "used", False)]
  68. def blend(messages, blend_combos=None):
  69. blend_combos = blend_combos or BLEND_COMBOS
  70. # group messages by file and then line number
  71. msgs_grouped = defaultdict(lambda: defaultdict(list))
  72. for message in messages:
  73. msgs_grouped[message.location.path][message.location.line].append(
  74. message,
  75. )
  76. # now blend together all messages on the same line
  77. out = []
  78. for by_line in msgs_grouped.values():
  79. for messages_on_line in by_line.values():
  80. out += blend_line(messages_on_line, blend_combos)
  81. return out
  82. def get_default_blend_combinations():
  83. combos = yaml.safe_load(pkg_resources.resource_string(__name__, "blender_combinations.yaml"))
  84. combos = combos.get("combinations", [])
  85. defaults = []
  86. for combo in combos:
  87. toblend = []
  88. for msg in combo:
  89. toblend += msg.items()
  90. defaults.append(tuple(toblend))
  91. return tuple(defaults)
  92. BLEND_COMBOS = get_default_blend_combinations()