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.

undefined_variable.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # pylint: disable=missing-docstring, multiple-statements
  2. # pylint: disable=too-few-public-methods, no-init, no-self-use, old-style-class,bare-except,broad-except
  3. from __future__ import print_function
  4. DEFINED = 1
  5. if DEFINED != 1:
  6. if DEFINED in (unknown, DEFINED): # [undefined-variable]
  7. DEFINED += 1
  8. def in_method(var):
  9. """method doc"""
  10. var = nomoreknown # [undefined-variable]
  11. assert var
  12. DEFINED = {DEFINED:__revision__} # [undefined-variable]
  13. # +1:[undefined-variable]
  14. DEFINED[__revision__] = OTHER = 'move this is astroid test'
  15. OTHER += '$'
  16. def bad_default(var, default=unknown2): # [undefined-variable]
  17. """function with defaut arg's value set to an unexistant name"""
  18. print(var, default)
  19. print(xxxx) # [undefined-variable]
  20. augvar += 1 # [undefined-variable]
  21. del vardel # [undefined-variable]
  22. LMBD = lambda x, y=doesnotexist: x+y # [undefined-variable]
  23. LMBD2 = lambda x, y: x+z # [undefined-variable]
  24. try:
  25. POUET # don't catch me
  26. except NameError:
  27. POUET = 'something'
  28. try:
  29. POUETT # [used-before-assignment]
  30. except Exception: # pylint:disable = broad-except
  31. POUETT = 'something'
  32. try:
  33. POUETTT # don't catch me
  34. except: # pylint:disable = bare-except
  35. POUETTT = 'something'
  36. print(POUET, POUETT, POUETTT)
  37. try:
  38. PLOUF # [used-before-assignment]
  39. except ValueError:
  40. PLOUF = 'something'
  41. print(PLOUF)
  42. def if_branch_test(something):
  43. """hop"""
  44. if something == 0:
  45. if xxx == 1: # [used-before-assignment]
  46. pass
  47. else:
  48. print(xxx)
  49. xxx = 3
  50. def decorator(arg):
  51. """Decorator with one argument."""
  52. return lambda: list(arg)
  53. @decorator(arg=[i * 2 for i in range(15)])
  54. def func1():
  55. """A function with a decorator that contains a listcomp."""
  56. @decorator(arg=(i * 2 for i in range(15)))
  57. def func2():
  58. """A function with a decorator that contains a genexpr."""
  59. @decorator(lambda x: x > 0)
  60. def main():
  61. """A function with a decorator that contains a lambda."""
  62. # Test shared scope.
  63. def test_arguments(arg=TestClass): # [used-before-assignment]
  64. """ TestClass isn't defined yet. """
  65. return arg
  66. class TestClass(Ancestor): # [used-before-assignment]
  67. """ contains another class, which uses an undefined ancestor. """
  68. class MissingAncestor(Ancestor1): # [used-before-assignment]
  69. """ no op """
  70. def test1(self):
  71. """ It should trigger here, because the two classes
  72. have the same scope.
  73. """
  74. class UsingBeforeDefinition(Empty): # [used-before-assignment]
  75. """ uses Empty before definition """
  76. class Empty(object):
  77. """ no op """
  78. return UsingBeforeDefinition
  79. def test(self):
  80. """ Ancestor isn't defined yet, but we don't care. """
  81. class MissingAncestor1(Ancestor):
  82. """ no op """
  83. return MissingAncestor1
  84. class Self(object):
  85. """ Detect when using the same name inside the class scope. """
  86. obj = Self # [undefined-variable]
  87. class Self1(object):
  88. """ No error should be raised here. """
  89. def test(self):
  90. """ empty """
  91. return Self1
  92. class Ancestor(object):
  93. """ No op """
  94. class Ancestor1(object):
  95. """ No op """
  96. NANA = BAT # [undefined-variable]
  97. del BAT
  98. class KeywordArgument(object):
  99. """Test keyword arguments."""
  100. enable = True
  101. def test(self, is_enabled=enable):
  102. """do nothing."""
  103. def test1(self, is_enabled=enabled): # [used-before-assignment]
  104. """enabled is undefined at this point, but it is used before assignment."""
  105. def test2(self, is_disabled=disabled): # [undefined-variable]
  106. """disabled is undefined"""
  107. enabled = True
  108. func = lambda arg=arg: arg * arg # [undefined-variable]
  109. arg2 = 0
  110. func2 = lambda arg2=arg2: arg2 * arg2
  111. # Don't emit if the code is protected by NameError
  112. try:
  113. unicode_1
  114. except NameError:
  115. pass
  116. try:
  117. unicode_2 # [undefined-variable]
  118. except Exception:
  119. pass
  120. try:
  121. unicode_3 # [undefined-variable]
  122. except:
  123. pass
  124. try:
  125. unicode_4 # [undefined-variable]
  126. except ValueError:
  127. pass
  128. # See https://bitbucket.org/logilab/pylint/issue/111/
  129. try: raise IOError(1, "a")
  130. except IOError as err: print(err)
  131. def test_conditional_comprehension():
  132. methods = ['a', 'b', '_c', '_d']
  133. my_methods = sum(1 for method in methods
  134. if not method.startswith('_'))
  135. return my_methods
  136. class MyError(object):
  137. pass
  138. class MyClass(object):
  139. class MyError(MyError):
  140. pass
  141. def dec(inp):
  142. def inner(func):
  143. print(inp)
  144. return func
  145. return inner