Skip to content

Commit 0c48a4e

Browse files
refactor: use from-imports for ast, configparser, and pytest
1 parent 509d47b commit 0c48a4e

5 files changed

Lines changed: 21 additions & 21 deletions

File tree

squawk_alembic/hook.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Pre-commit hook that generates DDL via alembic upgrade --sql and lints with squawk."""
22

33
import argparse
4-
import ast
5-
import configparser
64
import os
75
import re
86
import subprocess
97
import sys
108
import tempfile
9+
from ast import Assign, Constant, Name, Tuple, iter_child_nodes, parse
10+
from configparser import ConfigParser, NoOptionError, NoSectionError
1111
from pathlib import Path
1212

1313
_BRANCH_RE = re.compile(r"^[a-zA-Z0-9._/\-]+$")
@@ -19,12 +19,12 @@ def find_migrations_path():
1919
if not config_path.exists():
2020
return None
2121

22-
config = configparser.ConfigParser()
22+
config = ConfigParser()
2323
config.read(config_path)
2424

2525
try:
2626
script_location = config.get("alembic", "script_location")
27-
except (configparser.NoSectionError, configparser.NoOptionError):
27+
except (NoSectionError, NoOptionError):
2828
return None
2929

3030
script_location = script_location.removeprefix("./")
@@ -49,35 +49,35 @@ def extract_revision_info(filepath):
4949
"""Parse a migration file to extract revision and down_revision from module-level assignments."""
5050
with open(filepath) as f:
5151
try:
52-
tree = ast.parse(f.read())
52+
tree = parse(f.read())
5353
except SyntaxError:
5454
return None
5555

5656
revision = None
5757
down_revision = None
5858

59-
for node in ast.iter_child_nodes(tree):
60-
if not isinstance(node, ast.Assign):
59+
for node in iter_child_nodes(tree):
60+
if not isinstance(node, Assign):
6161
continue
62-
if len(node.targets) != 1 or not isinstance(node.targets[0], ast.Name):
62+
if len(node.targets) != 1 or not isinstance(node.targets[0], Name):
6363
continue
6464

6565
name = node.targets[0].id
6666
if name == "revision":
67-
if isinstance(node.value, ast.Constant) and isinstance(
67+
if isinstance(node.value, Constant) and isinstance(
6868
node.value.value, str
6969
):
7070
revision = node.value.value
7171
elif name == "down_revision":
72-
if isinstance(node.value, ast.Constant):
72+
if isinstance(node.value, Constant):
7373
if isinstance(node.value.value, str):
7474
down_revision = node.value.value
7575
elif node.value.value is None:
7676
down_revision = None
77-
elif isinstance(node.value, ast.Tuple):
77+
elif isinstance(node.value, Tuple):
7878
values = []
7979
for elt in node.value.elts:
80-
if isinstance(elt, ast.Constant) and isinstance(elt.value, str):
80+
if isinstance(elt, Constant) and isinstance(elt.value, str):
8181
values.append(elt.value)
8282
down_revision = tuple(values)
8383

tests/test_find_migrations_path.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Tests for alembic.ini auto-detection."""
22

3-
import pytest
3+
from pytest import fixture
44

55
from squawk_alembic.hook import find_migrations_path
66

77

8-
@pytest.fixture()
8+
@fixture()
99
def repo(tmp_path, monkeypatch):
1010
"""Set up a fake repo directory and chdir into it."""
1111
monkeypatch.chdir(tmp_path)

tests/test_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import textwrap
44
from unittest.mock import patch
55

6-
import pytest
6+
from pytest import fixture
77

88
from squawk_alembic.hook import main
99

1010

11-
@pytest.fixture()
11+
@fixture()
1212
def repo(tmp_path, monkeypatch):
1313
"""Set up a fake repo with alembic config and a versions directory."""
1414
monkeypatch.chdir(tmp_path)

tests/test_revision_info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import textwrap
44

5-
import pytest
5+
from pytest import fixture
66

77
from squawk_alembic.hook import extract_revision_info
88

99

10-
@pytest.fixture()
10+
@fixture()
1111
def migration_file(tmp_path):
1212
"""Write a migration file and return its path."""
1313

tests/test_squawk_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import shutil
44
import subprocess
55

6-
import pytest
6+
from pytest import fixture, mark
77

8-
pytestmark = pytest.mark.skipif(
8+
pytestmark = mark.skipif(
99
shutil.which("squawk") is None, reason="squawk not installed"
1010
)
1111

1212

1313
SQL = "ALTER TABLE foo ADD COLUMN bar text;\n"
1414

1515

16-
@pytest.fixture()
16+
@fixture()
1717
def sql_file(tmp_path):
1818
path = tmp_path / "migration.sql"
1919
path.write_text(SQL)

0 commit comments

Comments
 (0)