Ejemplo n.º 1
0
def test_PD011_pass_values_call():
    """
    Test that using .values() attribute call does not result in an error.
    """
    statement = "result = {}.values()"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 2
0
def test_PD012_fail_read_table():
    """
    Test that using .read_table() method results in an error.
    """
    statement = "df = pd.read_table(input_file)"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD012(1, 5)]
    assert actual == expected
Ejemplo n.º 3
0
def test_PD001_pass():
    """
    Test that importing pandas the recommended way does not result in an error.
    """
    statement = "import pandas as pd"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 4
0
def test_PD010_fail_unstack():
    """
    Test that using .unstack() results in an error.
    """
    statement = "table = df.unstack(level=0)"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD010(1, 8)]
    assert actual == expected
Ejemplo n.º 5
0
def test_PD013_fail_stack():
    """
    Test that using .stack() results in an error.
    """
    statement = "table = df.stack(level=-1, dropna=True)"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD013(1, 8)]
    assert actual == expected
Ejemplo n.º 6
0
def test_PD002_fail():
    """
    Test that using inplace=True results in an error.
    """
    statement = """df.drop(['a'], axis=1, inplace=True)"""
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD002(1, 0)]
    assert actual == expected
Ejemplo n.º 7
0
def test_PD015_fail_merge_on_pandas_object():
    """
    Test that using .merge() on the pandas root object generates an error.
    """
    statement = "pd.merge(df1, df2)"
    tree = ast.parse(statement)
    expected = [PD015(1, 0)]
    actual = list(VetPlugin(tree).run())
    assert actual == expected
Ejemplo n.º 8
0
def test_PD012_pass_read_csv():
    """
    Test that using .read_csv() explicitly does not result in an error.
    """
    statement = "df = pd.read_csv(input_file)"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 9
0
def test_PD015_pass_merge_on_dataframe():
    """
    Test that using .merge() on the dataframe does not result in an error.
    """
    statement = "df1.merge(df2)"
    tree = ast.parse(statement)
    expected = []
    actual = list(VetPlugin(tree).run())
    assert actual == expected
Ejemplo n.º 10
0
def test_PD015_pass_merge_on_dataframe_with_multiple_args():
    """
    Test that using `df.merge(arg1, arg2)` does not produce an error.
    """
    statement = "df1.merge(df2, 'inner')"
    tree = ast.parse(statement)
    expected = []
    actual = list(VetPlugin(tree).run())
    assert actual == expected
Ejemplo n.º 11
0
def test_PD007_pass_loc():
    """
    Test that using .loc[] explicitly does not result in an error.
    """
    statement = "new_df = df.loc['d':, 'A':'C']"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 12
0
def test_PD007_fail():
    """
    Test that using .ix[] results in an error.
    """
    statement = "s = df.ix[[0, 2], 'A']"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD007(1, 4)]
    assert actual == expected
Ejemplo n.º 13
0
def test_PD001_fail_wrong_alias():
    """
    Test that importing pandas with an odd name results in an error.
    """
    statement = "import pandas as foo"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD001(1, 1)]
    assert actual == expected
Ejemplo n.º 14
0
def test_PD011_pass_node_Name():
    """
    Test that where 'values' is a Name does NOT raise an error
    """
    statement = "result = values"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 15
0
def test_PD001_fail_no_as():
    """
    Test that importing pandas without an 'as' clause results in an error.
    """
    statement = "import pandas"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD001(1, 1)]
    assert actual == expected
Ejemplo n.º 16
0
def test_PD011_fail_values():
    """
    Test that using .values data frame attribute results in an error.
    """
    statement = "result = df.values"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD011(1, 9)]
    assert actual == expected
Ejemplo n.º 17
0
def test_PD012_node_Name_pass():
    """
    Test that where 'read_table' is a Name does NOT raise an error
    """
    statement = "df = read_table"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 18
0
def test_PD002_pass():
    """
    Test that using inplace=False explicitly does not result in an error.
    """
    statement = """df.drop(['a'], axis=1, inplace=False)"""
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 19
0
def test_PD011_pass_array():
    """
    Test that using .array explicitly does not result in an error.
    """
    statement = "result = df.array"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 20
0
def test_PD004_fail():
    """
    Test that using .notnull() results in an error.
    """
    statement = "notnulls = pd.notnull(val)"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD004(1, 11)]
    assert actual == expected
Ejemplo n.º 21
0
def test_PD004_pass():
    """
    Test that using .notna() explicitly does not result in an error.
    """
    statement = "notnas = pd.notna(val)"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 22
0
def test_PD009_pass():
    """
    Test that using .iloc[] explicitly does not result in an error.
    """
    statement = "index = df.iloc[:, 1:3]"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 23
0
def test_PD009_fail():
    """
    Test that using .iat[] results in an error.
    """
    statement = "index = df.iat[:, 1:3]"
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = [PD009(1, 8)]
    assert actual == expected
Ejemplo n.º 24
0
def test_PD006_fail_comparison_method():
    """
    Test that using comparison method results in an error.
    """
    comparison_methods = ['gt', 'lt', 'ge', 'le', 'eq', 'ne']
    for op in comparison_methods:
        statement = 'C = A.{0}(B)'.format(op)
        tree = ast.parse(statement)
        actual = list(VetPlugin(tree).run())
        expected = [PD006(1, 4)]
        assert actual == expected
Ejemplo n.º 25
0
def test_PD006_fail_comparison_method():
    """
    Test that using comparison method results in an error.
    """
    comparison_methods = ["gt", "lt", "ge", "le", "eq", "ne"]
    for op in comparison_methods:
        statement = "C = A.{0}(B)".format(op)
        tree = ast.parse(statement)
        actual = list(VetPlugin(tree).run())
        expected = [PD006(1, 4)]
        assert actual == expected
Ejemplo n.º 26
0
def test_PD006_pass_comparison_operator():
    """
    Test that explicit use of binary comparison operator does not
    result in an error.
    """
    comparison_operators = ['>', '<', '>=', '<=', '==', '!=']
    for op in comparison_operators:
        statement = 'C = A {0} B'.format(op)
        tree = ast.parse(statement)
        actual = list(VetPlugin(tree).run())
        expected = []
        assert actual == expected
Ejemplo n.º 27
0
def test_PD005_pass_arithmetic_operator():
    """
    Test that explicit use of binary arithmetic operator does not
    result in an error.
    """
    arithmetic_operators = ["+", "-", "*", "/", "**", "//", "%"]
    for op in arithmetic_operators:
        statement = "C = A {0} B".format(op)
        tree = ast.parse(statement)
        actual = list(VetPlugin(tree).run())
        expected = []
        assert actual == expected
Ejemplo n.º 28
0
def test_PD006_pass_comparison_operator():
    """
    Test that explicit use of binary comparison operator does not
    result in an error.
    """
    comparison_operators = [">", "<", ">=", "<=", "==", "!="]
    for op in comparison_operators:
        statement = "C = A {0} B".format(op)
        tree = ast.parse(statement)
        actual = list(VetPlugin(tree).run())
        expected = []
        assert actual == expected
Ejemplo n.º 29
0
def test_PD013_pass():
    """
    Test that using .melt() explicitly does not result in an error.
    """
    statement = """table = df.melt(
        id_vars='airline',
        value_vars=['ATL', 'DEN', 'DFW'],
        value_name='airline delay'
        )
    """
    tree = ast.parse(statement)
    actual = list(VetPlugin(tree).run())
    expected = []
    assert actual == expected
Ejemplo n.º 30
0
def check_code(source, filepath):
    """Lint Notebook Code Cells."""
    # Execute the linter
    try:
        tree = ast.parse(source)
        result = list(VetPlugin(tree).run())
        if result:
            print(f"{Fore.YELLOW}"
                  f"Found Notebook Error(s) in file {filepath}:"
                  f"{Fore.RED}"
                  f"\n{result[0]}\n"
                  f"{Style.RESET_ALL}")

    except SyntaxError as e:
        print(e)
        print("Source: ", f"\n```python\n{source}\n```")