brain_qt.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2015-2016, 2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
  2. # Copyright (c) 2016 Ceridwen <ceridwenv@gmail.com>
  3. # Copyright (c) 2017 Roy Wright <roy@wright.org>
  4. # Copyright (c) 2018 Ashley Whetter <ashley@awhetter.co.uk>
  5. # Copyright (c) 2019 Antoine Boellinger <aboellinger@hotmail.com>
  6. # Copyright (c) 2020-2021 hippo91 <guillaume.peillex@gmail.com>
  7. # Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  8. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  9. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  10. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  11. """Astroid hooks for the PyQT library."""
  12. from astroid import nodes, parse
  13. from astroid.brain.helpers import register_module_extender
  14. from astroid.builder import AstroidBuilder
  15. from astroid.manager import AstroidManager
  16. def _looks_like_signal(node, signal_name="pyqtSignal"):
  17. if "__class__" in node.instance_attrs:
  18. try:
  19. cls = node.instance_attrs["__class__"][0]
  20. return cls.name == signal_name
  21. except AttributeError:
  22. # return False if the cls does not have a name attribute
  23. pass
  24. return False
  25. def transform_pyqt_signal(node):
  26. module = parse(
  27. """
  28. class pyqtSignal(object):
  29. def connect(self, slot, type=None, no_receiver_check=False):
  30. pass
  31. def disconnect(self, slot):
  32. pass
  33. def emit(self, *args):
  34. pass
  35. """
  36. )
  37. signal_cls = module["pyqtSignal"]
  38. node.instance_attrs["emit"] = signal_cls["emit"]
  39. node.instance_attrs["disconnect"] = signal_cls["disconnect"]
  40. node.instance_attrs["connect"] = signal_cls["connect"]
  41. def transform_pyside_signal(node):
  42. module = parse(
  43. """
  44. class NotPySideSignal(object):
  45. def connect(self, receiver, type=None):
  46. pass
  47. def disconnect(self, receiver):
  48. pass
  49. def emit(self, *args):
  50. pass
  51. """
  52. )
  53. signal_cls = module["NotPySideSignal"]
  54. node.instance_attrs["connect"] = signal_cls["connect"]
  55. node.instance_attrs["disconnect"] = signal_cls["disconnect"]
  56. node.instance_attrs["emit"] = signal_cls["emit"]
  57. def pyqt4_qtcore_transform():
  58. return AstroidBuilder(AstroidManager()).string_build(
  59. """
  60. def SIGNAL(signal_name): pass
  61. class QObject(object):
  62. def emit(self, signal): pass
  63. """
  64. )
  65. register_module_extender(AstroidManager(), "PyQt4.QtCore", pyqt4_qtcore_transform)
  66. AstroidManager().register_transform(
  67. nodes.FunctionDef, transform_pyqt_signal, _looks_like_signal
  68. )
  69. AstroidManager().register_transform(
  70. nodes.ClassDef,
  71. transform_pyside_signal,
  72. lambda node: node.qname() in {"PySide.QtCore.Signal", "PySide2.QtCore.Signal"},
  73. )