Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

brain_fstrings.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) 2017 Claudiu Popa <pcmanticore@gmail.com>
  2. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  3. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  4. import sys
  5. import astroid
  6. def _clone_node_with_lineno(node, parent, lineno):
  7. cls = node.__class__
  8. other_fields = node._other_fields
  9. _astroid_fields = node._astroid_fields
  10. init_params = {
  11. 'lineno': lineno,
  12. 'col_offset': node.col_offset,
  13. 'parent': parent
  14. }
  15. postinit_params = {
  16. param: getattr(node, param)
  17. for param in _astroid_fields
  18. }
  19. if other_fields:
  20. init_params.update({
  21. param: getattr(node, param)
  22. for param in other_fields
  23. })
  24. new_node = cls(**init_params)
  25. if hasattr(node, 'postinit') and _astroid_fields:
  26. new_node.postinit(**postinit_params)
  27. return new_node
  28. def _transform_formatted_value(node):
  29. if node.value and node.value.lineno == 1:
  30. if node.lineno != node.value.lineno:
  31. new_node = astroid.FormattedValue(
  32. lineno=node.lineno,
  33. col_offset=node.col_offset,
  34. parent=node.parent
  35. )
  36. new_value = _clone_node_with_lineno(
  37. node=node.value,
  38. lineno=node.lineno,
  39. parent=new_node
  40. )
  41. new_node.postinit(value=new_value,
  42. format_spec=node.format_spec)
  43. return new_node
  44. if sys.version_info[:2] >= (3, 6):
  45. # TODO: this fix tries to *patch* http://bugs.python.org/issue29051
  46. # The problem is that FormattedValue.value, which is a Name node,
  47. # has wrong line numbers, usually 1. This creates problems for pylint,
  48. # which expects correct line numbers for things such as message control.
  49. astroid.MANAGER.register_transform(
  50. astroid.FormattedValue,
  51. _transform_formatted_value)