interfaces.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (c) 2009-2010, 2012-2013 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2013-2014 Google, Inc.
  3. # Copyright (c) 2014 Michal Nowikowski <godfryd@gmail.com>
  4. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  5. # Copyright (c) 2015-2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
  6. # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org>
  7. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  8. # Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com>
  9. # Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
  10. # Copyright (c) 2020-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  11. # Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
  12. # Copyright (c) 2020 Anthony Sottile <asottile@umich.edu>
  13. # Copyright (c) 2021 Nick Drozd <nicholasdrozd@gmail.com>
  14. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  15. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  16. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  17. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  18. """Interfaces for Pylint objects"""
  19. from collections import namedtuple
  20. from typing import TYPE_CHECKING, Tuple
  21. from astroid import nodes
  22. if TYPE_CHECKING:
  23. from pylint.reporters.ureports.nodes import Section
  24. Confidence = namedtuple("Confidence", ["name", "description"])
  25. # Warning Certainties
  26. HIGH = Confidence("HIGH", "No false positive possible.")
  27. INFERENCE = Confidence("INFERENCE", "Warning based on inference result.")
  28. INFERENCE_FAILURE = Confidence(
  29. "INFERENCE_FAILURE", "Warning based on inference with failures."
  30. )
  31. UNDEFINED = Confidence("UNDEFINED", "Warning without any associated confidence level.")
  32. CONFIDENCE_LEVELS = [HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED]
  33. class Interface:
  34. """Base class for interfaces."""
  35. @classmethod
  36. def is_implemented_by(cls, instance):
  37. return implements(instance, cls)
  38. def implements(obj: "Interface", interface: Tuple[type, type]) -> bool:
  39. """Return whether the given object (maybe an instance or class) implements
  40. the interface.
  41. """
  42. kimplements = getattr(obj, "__implements__", ())
  43. if not isinstance(kimplements, (list, tuple)):
  44. kimplements = (kimplements,)
  45. return any(issubclass(i, interface) for i in kimplements)
  46. class IChecker(Interface):
  47. """This is a base interface, not designed to be used elsewhere than for
  48. sub interfaces definition.
  49. """
  50. def open(self):
  51. """called before visiting project (i.e set of modules)"""
  52. def close(self):
  53. """called after visiting project (i.e set of modules)"""
  54. class IRawChecker(IChecker):
  55. """interface for checker which need to parse the raw file"""
  56. def process_module(self, node: nodes.Module) -> None:
  57. """process a module
  58. the module's content is accessible via astroid.stream
  59. """
  60. class ITokenChecker(IChecker):
  61. """Interface for checkers that need access to the token list."""
  62. def process_tokens(self, tokens):
  63. """Process a module.
  64. tokens is a list of all source code tokens in the file.
  65. """
  66. class IAstroidChecker(IChecker):
  67. """interface for checker which prefers receive events according to
  68. statement type
  69. """
  70. class IReporter(Interface):
  71. """reporter collect messages and display results encapsulated in a layout"""
  72. def handle_message(self, msg) -> None:
  73. """Handle the given message object."""
  74. def display_reports(self, layout: "Section") -> None:
  75. """display results encapsulated in the layout tree"""
  76. __all__ = ("IRawChecker", "IAstroidChecker", "ITokenChecker", "IReporter", "IChecker")