Python源码示例:foo.baz()
示例1
def test_I250_from_fail_2(flake8dir):
flake8dir.make_example_py(
"""
from foo import bar as bar, baz as baz
bar
baz
"""
)
result = flake8dir.run_flake8()
assert set(result.out_lines) == {
(
"./example.py:1:1: I250 Unnecessary import alias - rewrite as "
+ "'from foo import bar'."
),
(
"./example.py:1:1: I250 Unnecessary import alias - rewrite as "
+ "'from foo import baz'."
),
}
# I251
示例2
def test_withStatementUndefinedInExpression(self):
"""
An undefined name warning is emitted if a name in the I{test}
expression of a C{with} statement is undefined.
"""
self.flakes('''
from __future__ import with_statement
with bar as baz:
pass
''', m.UndefinedName)
self.flakes('''
from __future__ import with_statement
with bar as bar:
pass
''', m.UndefinedName)
示例3
def test_withStatementUndefinedInExpression(self):
"""
An undefined name warning is emitted if a name in the I{test}
expression of a C{with} statement is undefined.
"""
self.flakes('''
from __future__ import with_statement
with bar as baz:
pass
''', m.UndefinedName)
self.flakes('''
from __future__ import with_statement
with bar as bar:
pass
''', m.UndefinedName)
示例4
def test_module_attribute_as_usage(self):
python_node = self.get_ast_node(
"""
import foo.bar as baz
var = 'echo "TEST"'
baz.qux(var)
"""
)
linter = get_bad_module_attribute_use_implementation({'foo.bar': ['qux']})
linter.visit(python_node)
result = linter.get_results()
expected = [
dlint.linters.base.Flake8Result(
lineno=6,
col_offset=0,
message=linter._error_tmpl
)
]
assert result == expected
示例5
def test_module_attribute_import_from_as_usage(self):
python_node = self.get_ast_node(
"""
from foo.bar import baz as qux
var = 'echo "TEST"'
qux(var)
"""
)
linter = get_bad_module_attribute_use_implementation({'foo.bar': ['baz']})
linter.visit(python_node)
result = linter.get_results()
expected = [
dlint.linters.base.Flake8Result(
lineno=6,
col_offset=0,
message=linter._error_tmpl
)
]
assert result == expected
示例6
def test_module_attribute_missing_import_usage(self):
python_node = self.get_ast_node(
"""
import baz
from qux import quine
from . import xyz
var = 'echo "TEST"'
foo = None
foo.bar(var)
"""
)
linter = get_bad_module_attribute_use_implementation({'foo': ['bar']})
linter.visit(python_node)
result = linter.get_results()
expected = []
assert result == expected
示例7
def test_module_attribute_usage_nested(self):
python_node = self.get_ast_node(
"""
import foo
var = 'echo "TEST"'
foo.bar(var).baz()
"""
)
linter = get_bad_module_attribute_use_implementation({'foo': ['bar']})
linter.visit(python_node)
result = linter.get_results()
expected = [
dlint.linters.base.Flake8Result(
lineno=6,
col_offset=0,
message=linter._error_tmpl
)
]
assert result == expected
示例8
def test_kwargs_present_different_module_path(self):
python_node = self.get_ast_node(
"""
import foo
import boo
boo.bar.baz(kwarg="test")
"""
)
linter = get_bad_kwarg_use_implementation(
[
{
"module_path": "foo.bar.baz",
"kwarg_name": "kwarg",
"predicate": dlint.tree.kwarg_present,
},
]
)
linter.visit(python_node)
result = linter.get_results()
expected = []
assert result == expected
示例9
def test_kwargs_missing_module_path(self):
python_node = self.get_ast_node(
"""
import foo
foo.bar.baz(kwarg="test")
"""
)
linter = get_bad_kwarg_use_implementation(
[
{
"module_path": "foo.bar.baz",
"kwarg_name": "kwarg",
"predicate": dlint.tree.kwarg_not_present,
},
]
)
linter.visit(python_node)
result = linter.get_results()
expected = []
assert result == expected
示例10
def test_withStatementUndefinedInExpression(self):
"""
An undefined name warning is emitted if a name in the I{test}
expression of a C{with} statement is undefined.
"""
self.flakes('''
from __future__ import with_statement
with bar as baz:
pass
''', m.UndefinedName)
self.flakes('''
from __future__ import with_statement
with bar as bar:
pass
''', m.UndefinedName)
示例11
def test_it_does_not_crash_on_attribute_functions(flake8dir):
flake8dir.make_example_py(
"""
import foo
bar = foo.baz(x + 1 for x in range(10))
"""
)
result = flake8dir.run_flake8()
assert result.out_lines == []
# C408
示例12
def test_simple(self):
pkgname = 'foo'
dirname_0 = self.create_init(pkgname)
dirname_1 = self.create_init(pkgname)
self.create_submodule(dirname_0, pkgname, 'bar', 0)
self.create_submodule(dirname_1, pkgname, 'baz', 1)
import foo.bar
import foo.baz
# Ensure we read the expected values
self.assertEqual(foo.bar.value, 0)
self.assertEqual(foo.baz.value, 1)
# Ensure the path is set up correctly
self.assertEqual(sorted(foo.__path__),
sorted([os.path.join(dirname_0, pkgname),
os.path.join(dirname_1, pkgname)]))
# Cleanup
shutil.rmtree(dirname_0)
shutil.rmtree(dirname_1)
del sys.path[0]
del sys.path[0]
del sys.modules['foo']
del sys.modules['foo.bar']
del sys.modules['foo.baz']
# Another awful testing hack to be cleaned up once the test_runpy
# helpers are factored out to a common location
示例13
def test_mixed_namespace(self):
pkgname = 'foo'
dirname_0 = self.create_init(pkgname)
dirname_1 = self.create_init(pkgname)
self.create_submodule(dirname_0, pkgname, 'bar', 0)
# Turn this into a PEP 420 namespace package
os.unlink(os.path.join(dirname_0, pkgname, '__init__.py'))
self.create_submodule(dirname_1, pkgname, 'baz', 1)
import foo.bar
import foo.baz
# Ensure we read the expected values
self.assertEqual(foo.bar.value, 0)
self.assertEqual(foo.baz.value, 1)
# Ensure the path is set up correctly
self.assertEqual(sorted(foo.__path__),
sorted([os.path.join(dirname_0, pkgname),
os.path.join(dirname_1, pkgname)]))
# Cleanup
shutil.rmtree(dirname_0)
shutil.rmtree(dirname_1)
del sys.path[0]
del sys.path[0]
del sys.modules['foo']
del sys.modules['foo.bar']
del sys.modules['foo.baz']
# XXX: test .pkg files
示例14
def test_simple(self):
pkgname = 'foo'
dirname_0 = self.create_init(pkgname)
dirname_1 = self.create_init(pkgname)
self.create_submodule(dirname_0, pkgname, 'bar', 0)
self.create_submodule(dirname_1, pkgname, 'baz', 1)
import foo.bar
import foo.baz
# Ensure we read the expected values
self.assertEqual(foo.bar.value, 0)
self.assertEqual(foo.baz.value, 1)
# Ensure the path is set up correctly
self.assertEqual(sorted(foo.__path__),
sorted([os.path.join(dirname_0, pkgname),
os.path.join(dirname_1, pkgname)]))
# Cleanup
shutil.rmtree(dirname_0)
shutil.rmtree(dirname_1)
del sys.path[0]
del sys.path[0]
del sys.modules['foo']
del sys.modules['foo.bar']
del sys.modules['foo.baz']
# Another awful testing hack to be cleaned up once the test_runpy
# helpers are factored out to a common location
示例15
def test_mixed_namespace(self):
pkgname = 'foo'
dirname_0 = self.create_init(pkgname)
dirname_1 = self.create_init(pkgname)
self.create_submodule(dirname_0, pkgname, 'bar', 0)
# Turn this into a PEP 420 namespace package
os.unlink(os.path.join(dirname_0, pkgname, '__init__.py'))
self.create_submodule(dirname_1, pkgname, 'baz', 1)
import foo.bar
import foo.baz
# Ensure we read the expected values
self.assertEqual(foo.bar.value, 0)
self.assertEqual(foo.baz.value, 1)
# Ensure the path is set up correctly
self.assertEqual(sorted(foo.__path__),
sorted([os.path.join(dirname_0, pkgname),
os.path.join(dirname_1, pkgname)]))
# Cleanup
shutil.rmtree(dirname_0)
shutil.rmtree(dirname_1)
del sys.path[0]
del sys.path[0]
del sys.modules['foo']
del sys.modules['foo.bar']
del sys.modules['foo.baz']
# XXX: test .pkg files
示例16
def test_attrAugmentedAssignment(self):
"""
Augmented assignment of attributes is supported.
We don't care about attr refs.
"""
self.flakes('''
foo = None
foo.bar += foo.baz
''')
示例17
def test_doubleClosedOver(self):
"""
Don't warn when the assignment is used in an inner function, even if
that inner function itself is in an inner function.
"""
self.flakes('''
def barMaker():
foo = 5
def bar():
def baz():
return foo
return bar
''')
示例18
def test_ifexp(self):
"""
Test C{foo if bar else baz} statements.
"""
self.flakes("a = 'moo' if True else 'oink'")
self.flakes("a = foo if True else 'oink'", m.UndefinedName)
self.flakes("a = 'moo' if True else bar", m.UndefinedName)
示例19
def test_withStatementTupleNames(self):
"""
No warnings are emitted for using any of the tuple of names defined by
a C{with} statement within the suite or afterwards.
"""
self.flakes('''
from __future__ import with_statement
with open('foo') as (bar, baz):
bar, baz
bar, baz
''')
示例20
def test_withStatementListNames(self):
"""
No warnings are emitted for using any of the list of names defined by a
C{with} statement within the suite or afterwards.
"""
self.flakes('''
from __future__ import with_statement
with open('foo') as [bar, baz]:
bar, baz
bar, baz
''')
示例21
def test_withStatementTupleNamesRedefined(self):
"""
A redefined name warning is emitted if a name bound by an import is
rebound by one of the names defined by the tuple-unpacking form of a
C{with} statement.
"""
self.flakes('''
from __future__ import with_statement
import bar
with open('foo') as (bar, baz):
pass
''', m.RedefinedWhileUnused)
示例22
def test_withStatementUndefinedInside(self):
"""
An undefined name warning is emitted if a name is used inside the
body of a C{with} statement without first being bound.
"""
self.flakes('''
from __future__ import with_statement
with open('foo') as bar:
baz
''', m.UndefinedName)
示例23
def test_withStatementNameDefinedInBody(self):
"""
A name defined in the body of a C{with} statement can be used after
the body ends without warning.
"""
self.flakes('''
from __future__ import with_statement
with open('foo') as bar:
baz = 10
baz
''')
示例24
def test_augmentedAssignmentImportedFunctionCall(self):
"""
Consider a function that is called on the right part of an
augassign operation to be used.
"""
self.flakes('''
from foo import bar
baz = 0
baz += bar()
''')
示例25
def test_attrAugmentedAssignment(self):
"""
Augmented assignment of attributes is supported.
We don't care about attr refs.
"""
self.flakes('''
foo = None
foo.bar += foo.baz
''')
示例26
def test_doubleClosedOver(self):
"""
Don't warn when the assignment is used in an inner function, even if
that inner function itself is in an inner function.
"""
self.flakes('''
def barMaker():
foo = 5
def bar():
def baz():
return foo
return bar
''')
示例27
def test_ifexp(self):
"""
Test C{foo if bar else baz} statements.
"""
self.flakes("a = 'moo' if True else 'oink'")
self.flakes("a = foo if True else 'oink'", m.UndefinedName)
self.flakes("a = 'moo' if True else bar", m.UndefinedName)
示例28
def test_withStatementTupleNames(self):
"""
No warnings are emitted for using any of the tuple of names defined by
a C{with} statement within the suite or afterwards.
"""
self.flakes('''
from __future__ import with_statement
with open('foo') as (bar, baz):
bar, baz
bar, baz
''')
示例29
def test_withStatementListNames(self):
"""
No warnings are emitted for using any of the list of names defined by a
C{with} statement within the suite or afterwards.
"""
self.flakes('''
from __future__ import with_statement
with open('foo') as [bar, baz]:
bar, baz
bar, baz
''')
示例30
def test_withStatementTupleNamesRedefined(self):
"""
A redefined name warning is emitted if a name bound by an import is
rebound by one of the names defined by the tuple-unpacking form of a
C{with} statement.
"""
self.flakes('''
from __future__ import with_statement
import bar
with open('foo') as (bar, baz):
pass
''', m.RedefinedWhileUnused)