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.

raising_format_tuple.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. '''
  2. Complain about multi-argument exception constructors where the first argument
  3. contains a percent sign, thus suggesting a % string formatting was intended
  4. instead. The same holds for a string containing {...} suggesting str.format()
  5. was intended.
  6. '''
  7. def bad_percent(arg):
  8. '''Raising a percent-formatted string and an argument'''
  9. raise KeyError('Bad key: %r', arg) # [raising-format-tuple]
  10. def good_percent(arg):
  11. '''Instead of passing multiple arguments, format the message'''
  12. raise KeyError('Bad key: %r' % arg)
  13. def bad_multiarg(name, value):
  14. '''Raising a formatted string and multiple additional arguments'''
  15. raise ValueError('%s measures %.2f', name, value) # [raising-format-tuple]
  16. def good_multiarg(name, value):
  17. '''The arguments have to be written as a tuple for formatting'''
  18. raise ValueError('%s measures %.2f' % (name, value))
  19. def bad_braces(arg):
  20. '''Curly braces as placeholders'''
  21. raise KeyError('Bad key: {:r}', arg) # [raising-format-tuple]
  22. def good_braces(arg):
  23. '''Call str.format() instead'''
  24. raise KeyError('Bad key: {:r}'.format(arg))
  25. def bad_multistring(arg):
  26. '''Multiple adjacent string literals'''
  27. raise AssertionError( # [raising-format-tuple]
  28. 'Long message about %s '
  29. "split over several adjacent literals", arg)
  30. def bad_triplequote(arg):
  31. '''String literals with triple quotes'''
  32. raise AssertionError( # [raising-format-tuple]
  33. '''Long message about %s
  34. split over several adjacent literals''', arg)
  35. def bad_unicode(arg):
  36. '''Unicode string literal'''
  37. raise ValueError(u'Bad %s', arg) # [raising-format-tuple]
  38. def raise_something_without_name(arg):
  39. '''Regression test for nodes without .node attribute'''
  40. import standard_exceptions # pylint: disable=import-error
  41. raise standard_exceptions.MyException(u'An %s', arg) # [raising-format-tuple]