K i[XddlZddlmZmZddlmZmZGddeZGddeZ y)N)messageschecker)TestCaseskipceZdZdZdZdZdZdZeddZ 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@ZAdAZBdBZCdCZDdDZEyE)FTestcD|jdtjy)Nbarflakesm UndefinedNameselfs h/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/pyflakes/test/test_undefined_names.pytest_undefinedzTest.test_undefineds E1??+c&|jdy)Nz for a in range(10) if a]r rs rtest_definedInListCompzTest.test_definedInListComp s 12rcD|jdtjy)Nz2 [a for a in range(10)] a r rs rtest_undefinedInListCompzTest.test_undefinedInListComps  OO  %rcb|jdtjtjy)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_undefinedExceptionNames&  __a..  0rc&|jdy)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 belowcD|jdtjy)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.s  OO %rcb|jdtjtjy)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?s&  __a.. 0rcD|jdtjy)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_undefinedExceptionNameObscuringLocalVariableFalsePositive1N     rc&|jdy)z7The exception name can be deleted in the except: block.z\ try: pass except Exception as exc: del exc Nrrs rtest_delExceptionInExceptzTest.test_delExceptionInExcept]   rcD|jdtjy)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_undefinedExceptionNameObscuringLocalVariableFalsePositive2fs     rcD|jdtjy)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_undefinedExceptionNameObscuringGlobalVariablexs   $$ &rcD|jdtjy)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_undefinedExceptionNameObscuringGlobalVariable2s   $$ &rcD|jdtjy)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_undefinedExceptionNameObscuringGlobalVariableFalsePositive1s     rcD|jdtjy)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_undefinedExceptionNameObscuringGlobalVariableFalsePositive2s     rc&|jdy)NzQ class a: def b(): fu fu = 1 rrs rtest_functionsNeedGlobalScopez"Test.test_functionsNeedGlobalScopes   rc&|jdy)Nz range(10)rrs r test_builtinszTest.test_builtinss K rc&|jdy)zm C{WindowsError} is sometimes a builtin name, so no warning is emitted for using it. WindowsErrorNrrs rtest_builtinWindowsErrorzTest.test_builtinWindowsError N#rc&|jdy)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 %&rc&|jdy)zh Use of the C{__file__} magic global should not emit an undefined name warning. __file__Nrrs rtest_magicGlobalsFilezTest.test_magicGlobalsFile Jrc&|jdy)zl Use of the C{__builtins__} magic global should not emit an undefined name warning. __builtins__Nrrs rtest_magicGlobalsBuiltinszTest.test_magicGlobalsBuiltinsr>rc&|jdy)zh Use of the C{__name__} magic global should not emit an undefined name warning. __name__Nrrs rtest_magicGlobalsNamezTest.test_magicGlobalsNamerErcj|jdtj|jddy)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_magicGlobalsPaths' J0 J)> ?rc|jdtj|jd|jdtjy)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!//2     __  rc|jdtj|jd|jdtjy)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 NAOO4     __  rcb|jdtjtjy)z)Can't find undefined names with import *.zfrom fu import *; barN)r r ImportStarUsedImportStarUsagers rtest_globalImportStarzTest.test_globalImportStars" +$$a&7&7 9rcH|jd|jdy)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_definedByGlobals(      rc&|jdy)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   rcD|jdtjy)zD A global statement in the global scope is ignored. zB global x def foo(): print(x) Nr rs rtest_globalInGlobalScopezTest.test_globalInGlobalScope6s  __  rcb|jdtjtjy)z@A global statement does not prevent other names being undefined.zQ def f1(): s def f2(): global m N)r r rUnusedIndirectAssignmentrs rtest_global_reset_name_onlyz Test.test_global_reset_name_only@s&  __a88  :rtodocD|jdtjy)z4An unused global statement does not define the name.zQ def f1(): m def f2(): global m Nr rs rtest_unused_globalzTest.test_unused_globalLs  __  rcD|jdtjy)zDel deletes bindings.za = 1; del a; aNr rs rtest_delz Test.test_delWs %q7rc&|jdy)z%Del a global binding from a function.zY a = 1 def f(): global a del a a Nrrs rtest_delGlobalzTest.test_delGlobal[s   rcD|jdtjy)zDel an undefined name.zdel aNr rs rtest_delUndefinedzTest.test_delUndefinedes GQ__-rc&|jdy)z8 Ignores conditional bindings deletion. zq context = None test = True if False: del(test) assert(test) Nrrs rtest_delConditionalzTest.test_delConditionalis   rc&|jdy)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   rc&|jdy)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_delWhilerrc&|jdy)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_delWhileTestUsagerrc&|jdy)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  rc&|jdy)z.Global names are available from nested scopes.zO a = 1 def b(): def c(): a Nrrs rtest_globalFromNestedScopezTest.test_globalFromNestedScoper*rcD|jdtjy)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_laterRedefinedGlobalFromNestedScopes      rcb|jdtjtjy)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 N)r r r/rars r)test_laterRedefinedGlobalFromNestedScope2z.Test.test_laterRedefinedGlobalFromNestedScope2s(    q99 ;rcD|jdtjy)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_intermediateClassScopeIgnoreds      rc|jdtjjd}|jrdnd}|j |j d|fy)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_doubleNestingReportsClosestNames^kk      (x +"&!1!1Aq ))C1B+CDrcD|jdtjy)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|jdtjtjtjtjtjy)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 s9   OO OO OOQ-- OO rc&|jdy)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  rcD|jdtjy)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  __ rc&|jdy)z+Star and double-star arg names are defined.z? def f(a, *b, **c): print(a, b, c) Nrrs rtest_definedAsStarArgszTest.test_definedAsStarArgs7s   rcj|jd|jd|jdy)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>s9         rcj|jd|jd|jdy)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_usedAsStarUnpackMs;         rc<|jdtjtj|jdtjtj|jdtjtjtjy)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    q// 1    q//1A1A CrcH|jd|jdy)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_keywordOnlyArgsos&     rcD|jdtjy)zTypo in kwonly name.zC def f(*, a, b=default_c): print(a, b) Nr rs rtest_keywordOnlyArgsUndefinedz"Test.test_keywordOnlyArgsUndefined|s  __ rcH|jd|jdy)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_annotationUndefineds&     rc&|jdy)NzR from abc import ABCMeta class A(metaclass=ABCMeta): pass rrs rtest_metaClassUndefinedzTest.test_metaClassUndefineds   rcH|jd|jdy)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_definedInGenExps 12 CDrc|jdtj|jdtjy)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_undefinedInGenExpNesteds2 COO % COO %rc|jd|jd|jdtj|jdtjy)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`       __    __  rcH|jd|jdy)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_definedInClasss(      rc&|jdy)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|jdtj|jdtj|jdtjy)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_undefinedInLoopsR  __   __   __ rc&|jdy)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   rc&|jdy)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_definedFromLambdaInGeneratorrrcD|jdtjy)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   __ rcD|jdtjy)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_undefinedFromLambdaInComprehensionrrc*d}|j|y)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_rbrergrirkrmrorqrsrurwryr{r}rrrrrrrrrrrrrrrrrrrrrrrrrrrrs,3% 0   AB%C% 0  $ AB&C&" AB&C&("( !$' $ @  9    : &\8 .           ; $E2  $       "C"     E %> *     rrceZdZdZdZy) NameTestsz6 Tests for some extra cases of name handling. ctjd}t|jdjd_|j ttj|y)zj A Name node with an unrecognized context results in a RuntimeError being raised. zx = 10rN) astparseobjectbodytargetsctx assertRaises RuntimeErrorrChecker)rtrees rtest_impossibleContextz NameTests.test_impossibleContext+sE yy"&,h ! Q# ,>rN)rJrQrT__doc__rrrrrr's ?rr) rpyflakesrr rpyflakes.test.harnessrrrrrrrrs) +0] 8] @ ? ?r