joined_str_check.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (c) 2022 Rocky Bernstein
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. def joined_str_invalid(
  15. self, lhs: str, n: int, rule, tree, tokens: list, first: int, last: int
  16. ) -> bool:
  17. # In Python 3.8, there is a new "=" specifier.
  18. # See https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging
  19. # We detect this here inside joined_str by looking for an
  20. # expr->LOAD_STR which has an "=" added at the end
  21. # and is equal without the "=" to expr->formated_value2->LOAD_CONST
  22. # converted to a string.
  23. expr1 = tree[0]
  24. if expr1 != "expr":
  25. return False
  26. load_str = expr1[0]
  27. if load_str != "LOAD_STR":
  28. return False
  29. format_value_equal = load_str.attr
  30. if format_value_equal[-1] != "=":
  31. return False
  32. expr2 = tree[1]
  33. if expr2 != "expr":
  34. return False
  35. formatted_value = expr2[0]
  36. if not formatted_value.kind.startswith("formatted_value"):
  37. return False
  38. expr2a = formatted_value[0]
  39. if expr2a != "expr":
  40. return False
  41. load_const = expr2a[0]
  42. if load_const == "LOAD_CONST":
  43. format_value2 = load_const.attr
  44. return str(format_value2) == format_value_equal[:-1]
  45. return True