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.

redefined.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Checks variable types aren't redefined within a method or a function"""
  2. # pylint: disable=too-few-public-methods,missing-docstring,unused-variable,invalid-name
  3. _OK = True
  4. class MyClass(object):
  5. class Klass(object):
  6. def __init__(self):
  7. self.var2 = 'var'
  8. def __init__(self):
  9. self.var = True
  10. self.var1 = 2
  11. self.var2 = 1.
  12. self.var1 = 2. # [redefined-variable-type]
  13. self.a_str = "hello"
  14. a_str = False
  15. (a_str, b_str) = (1, 2) # no support for inference on tuple assignment
  16. a_str = 2.0 if self.var else 1.0 # no support for inference on ifexpr
  17. def _getter(self):
  18. return self.a_str
  19. def _setter(self, val):
  20. self.a_str = val
  21. var2 = property(_getter, _setter)
  22. def some_method(self):
  23. def func():
  24. var = 1
  25. test = 'bar'
  26. var = 'baz' # [redefined-variable-type]
  27. self.var = 1 # the rule checks for redefinitions in the scope of a function or method
  28. test = 'foo'
  29. myint = 2
  30. myint = False # [redefined-variable-type]
  31. _OK = "This is OK" # [redefined-variable-type]
  32. if _OK:
  33. SOME_FLOAT = 1.
  34. def dummy_function():
  35. return 2
  36. def other_function():
  37. instance = MyClass()
  38. instance = True # [redefined-variable-type]
  39. SOME_FLOAT = dummy_function() # [redefined-variable-type]
  40. A_GLOB = None
  41. A_GLOB = [1, 2, 3]
  42. def func2(x):
  43. if x:
  44. var = 'foo'
  45. else:
  46. var = True
  47. if x:
  48. var2 = 'foo'
  49. elif not x:
  50. var2 = 2
  51. else:
  52. pass
  53. if x:
  54. var3 = 'foo'
  55. var3 = 2 # [redefined-variable-type]
  56. else:
  57. pass
  58. var = 2 # [redefined-variable-type]
  59. if x:
  60. pass
  61. elif not x:
  62. var4 = True
  63. elif _OK:
  64. pass
  65. else:
  66. var4 = 2.
  67. var4 = 'baz' # [redefined-variable-type]