bg \fddlZddlmZmZddlmZmZGddeZGddeZ dS)N)messageschecker)TestCaseskipc"eZdZdZdZdZdZdZeddZ dZ d Z d Z d Z edd Zedd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ edd Z!d!Z"d"Z#d#Z$d$Z%d%Z&d&Z'd'Z(d(Z)d)Z*d*Z+d+Z,d,Z-d-Z.d.Z/d/Z0d0Z1d1Z2d2Z3d3Z4d4Z5d5Z6d6Z7d7Z8d8Z9d9Z:d:Z;d;Zd>Z?d?Z@d@ZAdAZBdBZCdCZDdDZEdES)FTestcF|dtjdS)Nbarflakesm UndefinedNameselfs s/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib/python3.11/site-packages/pyflakes/test/test_undefined_names.pytest_undefinedzTest.test_undefineds E1?+++++c0|ddS)Nz for a in range(10) if a]r rs rtest_definedInListCompzTest.test_definedInListComp s 122222rcF|dtjdS)Nz2 [a for a in range(10)] a r rs rtest_undefinedInListCompzTest.test_undefinedInListComps.  O  % % % % %rc\|dtjtjdS)zxException names can't be used after the except: block. The exc variable is unused inside the exception handler.zx try: raise ValueError('ve') except ValueError as exc: pass exc Nr r rUnusedVariablers rtest_undefinedExceptionNamez Test.test_undefinedExceptionNames5  _a.  0 0 0 0 0rc0|ddS)zLocals declared in except: blocks can be used after the block. This shows the example in test_undefinedExceptionName is different.zy try: raise ValueError('ve') except ValueError as exc: e = exc e Nrrs r test_namesDeclaredInExceptBlocksz%Test.test_namesDeclaredInExceptBlocks!)       rz5error reporting disabled due to false positives belowcF|dtjdS)zException names obscure locals, can't be used after. Last line will raise UnboundLocalError on Python 3 after exiting the except: block. Note next two examples for false positives to watch out for.z exc = 'Original value' try: raise ValueError('ve') except ValueError as exc: pass exc Nr rs r1test_undefinedExceptionNameObscuringLocalVariablez6Test.test_undefinedExceptionNameObscuringLocalVariable.s0  O % % % % %rc\|dtjtjdS)zException names are unbound after the `except:` block. Last line will raise UnboundLocalError. The exc variable is unused inside the exception handler. z try: raise ValueError('ve') except ValueError as exc: pass print(exc) exc = 'Original value' Nrrs r2test_undefinedExceptionNameObscuringLocalVariable2z7Test.test_undefinedExceptionNameObscuringLocalVariable2?s5  _a. 0 0 0 0 0rcF|dtjdS)zException names obscure locals, can't be used after. Unless. Last line will never raise UnboundLocalError because it's only entered if no exception was raised.z exc = 'Original value' try: raise ValueError('ve') except ValueError as exc: print('exception logged') raise exc Nr r rrs r?test_undefinedExceptionNameObscuringLocalVariableFalsePositive1zDTest.test_undefinedExceptionNameObscuringLocalVariableFalsePositive1N1        rc0|ddS)z7The exception name can be deleted in the except: block.z\ try: pass except Exception as exc: del exc Nrrs rtest_delExceptionInExceptzTest.test_delExceptionInExcept]'       rcF|dtjdS)zException names obscure locals, can't be used after. Unless. Last line will never raise UnboundLocalError because `error` is only falsy if the `except:` block has not been entered.z exc = 'Original value' error = None try: raise ValueError('ve') except ValueError as exc: error = 'exception logged' if error: print(error) else: exc Nr%rs r?test_undefinedExceptionNameObscuringLocalVariableFalsePositive2zDTest.test_undefinedExceptionNameObscuringLocalVariableFalsePositive2fs1        rcF|dtjdS)zException names obscure globals, can't be used after. Last line will raise UnboundLocalError because the existence of that exception name creates a local scope placeholder for it, obscuring any globals, etc.z exc = 'Original value' def func(): try: pass # nothing is raised except ValueError as exc: pass # block never entered, exc stays unbound exc Nr r UndefinedLocalrs r2test_undefinedExceptionNameObscuringGlobalVariablez7Test.test_undefinedExceptionNameObscuringGlobalVariablexs1  $ & & & & &rcF|dtjdS)aException names obscure globals, can't be used after. Last line will raise NameError on Python 3 because the name is locally unbound after the `except:` block, even if it's nonlocal. We should issue an error in this case because code only working correctly if an exception isn't raised, is invalid. Unless it's explicitly silenced, see false positives below.a exc = 'Original value' def func(): global exc try: raise ValueError('ve') except ValueError as exc: pass # block never entered, exc stays unbound exc Nr.rs r3test_undefinedExceptionNameObscuringGlobalVariable2z8Test.test_undefinedExceptionNameObscuringGlobalVariable2s1  $ & & & & &rcF|dtjdS)zException names obscure globals, can't be used after. Unless. Last line will never raise NameError because it's only entered if no exception was raised.a exc = 'Original value' def func(): global exc try: raise ValueError('ve') except ValueError as exc: print('exception logged') raise exc Nr%rs r@test_undefinedExceptionNameObscuringGlobalVariableFalsePositive1zETest.test_undefinedExceptionNameObscuringGlobalVariableFalsePositive1s1        rcF|dtjdS)zException names obscure globals, can't be used after. Unless. Last line will never raise NameError because `error` is only falsy if the `except:` block has not been entered.aN exc = 'Original value' def func(): global exc error = None try: raise ValueError('ve') except ValueError as exc: error = 'exception logged' if error: print(error) else: exc Nr%rs r@test_undefinedExceptionNameObscuringGlobalVariableFalsePositive2zETest.test_undefinedExceptionNameObscuringGlobalVariableFalsePositive2s1        rc0|ddS)NzQ class a: def b(): fu fu = 1 rrs rtest_functionsNeedGlobalScopez"Test.test_functionsNeedGlobalScopes'       rc0|ddS)Nz range(10)rrs r test_builtinszTest.test_builtinss K     rc0|ddS)zm C{WindowsError} is sometimes a builtin name, so no warning is emitted for using it. WindowsErrorNrrs rtest_builtinWindowsErrorzTest.test_builtinWindowsError N#####rc0|ddS)z Use of the C{__annotations__} in module scope should not emit an undefined name warning when version is greater than or equal to 3.6. __annotations__Nrrs rtest_moduleAnnotationszTest.test_moduleAnnotationss %&&&&&rc0|ddS)zh Use of the C{__file__} magic global should not emit an undefined name warning. __file__Nrrs rtest_magicGlobalsFilezTest.test_magicGlobalsFile Jrc0|ddS)zl Use of the C{__builtins__} magic global should not emit an undefined name warning. __builtins__Nrrs rtest_magicGlobalsBuiltinszTest.test_magicGlobalsBuiltinsr>rc0|ddS)zh Use of the C{__name__} magic global should not emit an undefined name warning. __name__Nrrs rtest_magicGlobalsNamezTest.test_magicGlobalsNamerErct|dtj|dddS)z Use of the C{__path__} magic global should not emit an undefined name warning, if you refer to it from a file called __init__.py. __path__zpackage/__init__.py)filenameNr rs rtest_magicGlobalsPathzTest.test_magicGlobalsPaths7 J000 J)> ?????rc|dtj|d|dtjdS)z Use of the C{__module__} magic builtin should not emit an undefined name warning if used in class scope. __module__z3 class Foo: __module__ zR class Foo: def bar(self): __module__ Nr rs rtest_magicModuleInClassScopez!Test.test_magicModuleInClassScopesc L!/222       _      rc|dtj|d|dtjdS)z Use of the C{__qualname__} magic builtin should not emit an undefined name warning if used in class scope. __qualname__z5 class Foo: __qualname__ zT class Foo: def bar(self): __qualname__ Nr rs rtest_magicQualnameInClassScopez#Test.test_magicQualnameInClassScope sc NAO444       _      rc\|dtjtjdS)z)Can't find undefined names with import *.zfrom fu import *; barN)r r ImportStarUsedImportStarUsagers rtest_globalImportStarzTest.test_globalImportStars0 +$a&7 9 9 9 9 9rcZ|d|ddS)zd "global" can make an otherwise undefined name in another function defined. z@ def a(): global fu; fu = 1 def b(): fu zC def c(): bar def b(): global bar; bar = 1 Nrrs rtest_definedByGlobalzTest.test_definedByGlobalsF            rc0|ddS)z5 "global" can accept multiple names. zS def a(): global fu, bar; fu = 1; bar = 2 def b(): fu; bar Nrrs r!test_definedByGlobalMultipleNamesz&Test.test_definedByGlobalMultipleNames-s)       rcF|dtjdS)zD A global statement in the global scope is ignored. zB global x def foo(): print(x) Nr rs rtest_globalInGlobalScopezTest.test_globalInGlobalScope6s0  _      rcF|dtjdS)z@A global statement does not prevent other names being undefined.zQ def f1(): s def f2(): global m Nr rs rtest_global_reset_name_onlyz Test.test_global_reset_name_only@s0  _      rtodocF|dtjdS)z4An unused global statement does not define the name.zQ def f1(): m def f2(): global m Nr rs rtest_unused_globalzTest.test_unused_globalLs0  _      rcF|dtjdS)zDel deletes bindings.za = 1; del a; aNr rs rtest_delz Test.test_delWs %q77777rc0|ddS)z%Del a global binding from a function.zY a = 1 def f(): global a del a a Nrrs rtest_delGlobalzTest.test_delGlobal[s'       rcF|dtjdS)zDel an undefined name.zdel aNr rs rtest_delUndefinedzTest.test_delUndefinedes GQ_-----rc0|ddS)z8 Ignores conditional bindings deletion. zq context = None test = True if False: del(test) assert(test) Nrrs rtest_delConditionalzTest.test_delConditionalis)       rc0|ddS)zh Ignored conditional bindings deletion even if they are nested in other blocks. z context = None test = True if False: with context(): del(test) assert(test) Nrrs rtest_delConditionalNestedzTest.test_delConditionalNestedus)       rc0|ddS)zb Ignore bindings deletion if called inside the body of a while statement. z~ def test(): foo = 'bar' while False: del foo assert(foo) Nrrs r test_delWhilezTest.test_delWhilerrc0|ddS)z Ignore bindings deletion if called inside the body of a while statement and name is used inside while's test part. z def _worker(): o = True while o is not True: del o o = False Nrrs rtest_delWhileTestUsagezTest.test_delWhileTestUsagerrc0|ddS)zx Ignore bindings deletions if node is part of while's test, even when del is in a nested block. z context = None def _worker(): o = True while o is not True: while True: with context(): del o o = False Nrrs rtest_delWhileNestedzTest.test_delWhileNesteds)  rc0|ddS)z.Global names are available from nested scopes.zO a = 1 def b(): def c(): a Nrrs rtest_globalFromNestedScopezTest.test_globalFromNestedScoper*rcF|dtjdS)z~ Test that referencing a local name that shadows a global, before it is defined, generates a warning. z_ a = 1 def fun(): a a = 2 return a Nr.rs r(test_laterRedefinedGlobalFromNestedScopez-Test.test_laterRedefinedGlobalFromNestedScopes1         rcF|dtjdS)z Test that referencing a local name in a nested scope that shadows a global declared in an enclosing scope, before it is defined, generates a warning. z a = 1 def fun(): global a def fun2(): a a = 2 return a Nr.rs r)test_laterRedefinedGlobalFromNestedScope2z.Test.test_laterRedefinedGlobalFromNestedScope2s1        rcF|dtjdS)a If a name defined in an enclosing scope is shadowed by a local variable and the name is used locally before it is bound, an unbound local warning is emitted, even if there is a class scope between the enclosing scope and the local scope. z def f(): x = 1 class g: def h(self): a = x x = None print(x, a) print(x) Nr.rs r"test_intermediateClassScopeIgnoredz'Test.test_intermediateClassScopeIgnoreds1        rc|dtjjd}|jrdnd}||jd|fdS)a Test that referencing a local name in a nested scope that shadows a variable declared in two different outer scopes before it is defined in the innermost scope generates an UnboundLocal warning which refers to the nearest shadowed name. a def a(): x = 1 def b(): x = 2 # line 5 def c(): x x = 3 return x return x return x rxN)r r r/r withDoctest assertEqual message_args)rexcexpected_line_nums r$test_doubleNestingReportsClosestNamez)Test.test_doubleNestingReportsClosestNamesikk      ( +"&!18AAq )C1B+CDDDDDrcF|dtjdS)z Test that referencing a local name in a nested scope that shadows a global, before it is defined, generates a warning. z def fun(): a = 1 def fun2(): a a = 1 return a return a Nr.rs r)test_laterRedefinedGlobalFromNestedScope3z.Test.test_laterRedefinedGlobalFromNestedScope3r'rc|dtjtjtjtjtjdS)Nz def f(seq): a = 0 seq[a] += 1 seq[b] /= 2 c[0] *= 2 a -= 3 d += 4 e[any] = 5 rrs r!test_undefinedAugmentedAssignmentz&Test.test_undefinedAugmentedAssignment sB   O O OQ- O     rc0|ddS)z*Nested classes can access enclosing scope.z def f(foo): class C: bar = foo def f(self): return foo return C() f(123).f() Nrrs rtest_nestedClasszTest.test_nestedClasss'  rcF|dtjdS)z=Free variables in nested classes must bind at class creation.z def f(): class C: bar = foo foo = 456 return foo f() Nr rs rtest_badNestedClasszTest.test_badNestedClass,s.  _     rc0|ddS)z+Star and double-star arg names are defined.z? def f(a, *b, **c): print(a, b, c) Nrrs rtest_definedAsStarArgszTest.test_definedAsStarArgs7s'       rc|d|d|ddS)z!Star names in unpack are defined.z7 a, *b = range(10) print(a, b) z7 *a, b = range(10) print(a, b) z= a, *b, c = range(10) print(a, b, c) Nrrs rtest_definedAsStarUnpackzTest.test_definedAsStarUnpack>sa                 rc|d|d|ddS)zS Star names in unpack are used if RHS is not a tuple/list literal. z8 def f(): a, *b = range(10) z: def f(): (*a, b) = range(10) z= def f(): [a, *b, c] = range(10) Nrrs rtest_usedAsStarUnpackzTest.test_usedAsStarUnpackMsc                 rc|dtjtj|dtjtj|dtjtjtjdS)zQ Star names in unpack are unused if RHS is a tuple/list literal. zC def f(): a, *b = any, all, 4, 2, 'un' zL def f(): (*a, b) = [bool, int, float, complex] zD def f(): [a, *b, c] = 9, 8, 7, 6, 5, 4 Nr%rs rtest_unusedAsStarUnpackzTest.test_unusedAsStarUnpack^s   q/ 1 1 1   q/ 1 1 1   q/1A C C C C CrcZ|d|ddS)z#Keyword-only arg names are defined.z> def f(*, a, b=None): print(a, b) z\ import default_b def f(*, a, b=default_b): print(a, b) Nrrs rtest_keywordOnlyArgszTest.test_keywordOnlyArgsosD           rcF|dtjdS)zTypo in kwonly name.zC def f(*, a, b=default_c): print(a, b) Nr rs rtest_keywordOnlyArgsUndefinedz"Test.test_keywordOnlyArgsUndefined|s.  _     rcZ|d|ddS)zUndefined annotations.z from abc import note1, note2, note3, note4, note5 def func(a: note1, *args: note2, b: note3=12, **kw: note4) -> note5: pass zk def func(): d = e = 42 def func(a: {1, d}) -> (lambda c: e): pass Nrrs rtest_annotationUndefinedzTest.test_annotationUndefinedsD           rc0|ddS)NzR from abc import ABCMeta class A(metaclass=ABCMeta): pass rrs rtest_metaClassUndefinedzTest.test_metaClassUndefineds'       rcZ|d|ddS)zc Using the loop variable of a generator expression results in no warnings. z(a for a in [1, 2, 3] if a)z-(b for b in (a for a in [1, 2, 3] if a) if b)Nrrs rtest_definedInGenExpzTest.test_definedInGenExps0 1222 CDDDDDrc|dtj|dtjdS)z} The loop variables of generator expressions nested together are not defined in the other generator. z-(b for b in (a for a in [1, 2, 3] if b) if b)z-(b for b in (a for a in [1, 2, 3] if a) if a)Nr rs rtest_undefinedInGenExpNestedz!Test.test_undefinedInGenExpNestedsL CO % % % CO % % % % %rc|d|d|dtj|dtjdS)zr Some compatibility code checks explicitly for NameError. It should not trigger warnings. zc try: socket_map except NameError: socket_map = {} z try: _memoryview.contiguous except (NameError, AttributeError): raise RuntimeError("Python >= 3.3 is required") zY try: socket_map except: socket_map = {} zc try: socket_map except Exception: socket_map = {} Nr rs rtest_undefinedWithErrorHandlerz#Test.test_undefinedWithErrorHandlers           _      _      rcZ|d|ddS)zT Defined name for generator expressions and dict/set comprehension. z class A: T = range(10) Z = (x for x in T) L = [x for x in T] B = dict((i, str(i)) for i in T) zu class A: T = range(10) X = {x for x in T} Y = {x:x for x in T} Nrrs rtest_definedInClasszTest.test_definedInClasssF            rc0|ddS)z9Defined name for nested generator expressions in a class.za class A: T = range(10) Z = (x for x in (a for a in T)) Nrrs rtest_definedInClassNestedzTest.test_definedInClassNestedr*rc|dtj|dtj|dtjdS)zP The loop variable is defined after the expression is computed. z9 for i in range(i): print(i) z( [42 for i in range(i)] z( (42 for i in range(i)) Nr rs rtest_undefinedInLoopzTest.test_undefinedInLoopst  _     _     _     rc0|ddS)zi Defined name referenced from a lambda function within a dict/set comprehension. z4 {lambda: id(x) for x in range(10)} Nrrs r/test_definedFromLambdaInDictionaryComprehensionz4Test.test_definedFromLambdaInDictionaryComprehension)       rc0|ddS)zg Defined name referenced from a lambda function within a generator expression. z7 any(lambda: id(x) for x in range(10)) Nrrs r!test_definedFromLambdaInGeneratorz&Test.test_definedFromLambdaInGeneratorrrcF|dtjdS)zk Undefined name referenced from a lambda function within a dict/set comprehension. z4 {lambda: id(y) for x in range(10)} Nr rs r1test_undefinedFromLambdaInDictionaryComprehensionz6Test.test_undefinedFromLambdaInDictionaryComprehension .  _     rcF|dtjdS)zi Undefined name referenced from a lambda function within a generator expression. z7 any(lambda: id(y) for x in range(10)) Nr rs r'test_undefinedFromLambdaInComprehensionz,Test.test_undefinedFromLambdaInComprehensionrrc4d}||dS)Nz class Test(object): def __init__(self): print(__class__.__name__) self.x = 1 t = Test() r)rcodes rtest_dunderClasszTest.test_dunderClasss#  DrN)FrJrQrTrrrrrrr!r#r&r)r,r0r2r4r6r8r:r=rArDrHrKrOrRrUrYr[r]r_rardrfrhrjrlrnrprrrtrvrxrzr|rrrrrrrrrrrrrrrrrrrrrrrrrrrrs],,,333%%% 0 0 0     T ABB%%CB% 0 0 0      $ T ABB&&CB&" T ABB&&CB&("(   !!!$$$'''   $$$   @@@  999           T&\\\888   ...                          $EEE2      $                 "CCC"           EEE % % %>   *                 rrceZdZdZdZdS) NameTestsz6 Tests for some extra cases of name handling. ctjd}tjd}t |jdjd_|ttj ||dS)zj A Name node with an unrecognized context results in a RuntimeError being raised. zx = 10r) file_tokensN) astparser make_tokensobjectbodytargetsctx assertRaises RuntimeErrorChecker)rtreers rtest_impossibleContextz NameTests.test_impossibleContext+sb y"")(33 &,hh ! Q# ,;WWWWWrN)rJrQrT__doc__rrrrrr's2 X X X X Xrr) rpyflakesrr rpyflakes.test.harnessrrrrrrrrs ++++++++00000000] ] ] ] ] 8] ] ] @ X X X X X X X X X Xr