Skip to content

Commit 364938d

Browse files
committed
Add type checking to alembic/
1 parent cdcb678 commit 364938d

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ repos:
3838
entry: poetry run mypy --install-types --non-interactive
3939
language: system
4040
types: ['python']
41-
exclude: ^alembic/
4241
- id: pylint
4342
name: Pylint
4443
entry: poetry run pylint --rcfile=.python-lint

alembic/env.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from logging.config import fileConfig
2+
from typing import Any, Literal, Optional
23

34
from sqlalchemy import engine_from_config, pool
5+
from sqlalchemy.sql.schema import SchemaItem
46

57
from alembic import context
68
from rctab.crud.models import metadata
@@ -13,6 +15,7 @@
1315
config.set_main_option("sqlalchemy.url", str(settings.postgres_dsn))
1416
# Interpret the config file for Python logging.
1517
# This line sets up loggers basically.
18+
assert config.config_file_name is not None
1619
fileConfig(config.config_file_name)
1720

1821
# add your model's MetaData object here
@@ -26,16 +29,29 @@
2629
# my_important_option = config.get_main_option("my_important_option")
2730
# ... etc.
2831

32+
SchemaType = Literal[
33+
"schema", "table", "column", "index", "unique_constraint", "foreign_key_constraint"
34+
]
2935

30-
def include_object(object, name, type_, reflected, compare_to):
36+
37+
def include_object(
38+
object: SchemaItem,
39+
name: Optional[str],
40+
type_: SchemaType,
41+
reflected: bool,
42+
compare_to: Optional[SchemaItem],
43+
) -> bool:
3144
"""
3245
Exclude views from Alembic's consideration.
3346
"""
3447

35-
return not object.info.get("is_view", False)
48+
if object.info:
49+
if object.info.get("is_view", False):
50+
return False
51+
return True
3652

3753

38-
def run_migrations_offline():
54+
def run_migrations_offline() -> None:
3955
"""Run migrations in 'offline' mode.
4056
4157
This configures the context with just a URL
@@ -61,7 +77,7 @@ def run_migrations_offline():
6177
context.run_migrations()
6278

6379

64-
def run_migrations_online():
80+
def run_migrations_online() -> None:
6581
"""Run migrations in 'online' mode.
6682
6783
In this scenario we need to create an Engine

alembic/versions/b65796c99771_squash.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FinanceHistorySql:
6767
DROP_FUNCTION = "DROP FUNCTION {schema}.finance_changed();"
6868

6969

70-
def upgrade():
70+
def upgrade() -> None:
7171
op.execute(text("create schema accounting"))
7272
op.create_table(
7373
"user_cache",
@@ -362,9 +362,7 @@ def upgrade():
362362
sa.Column("subscription_id", postgresql.UUID(), nullable=False),
363363
sa.Column("display_name", sa.String(), nullable=False),
364364
sa.Column("state", sa.String(), nullable=False),
365-
sa.Column(
366-
"role_assignments", postgresql.JSONB(astext_type=sa.Text()), nullable=True
367-
),
365+
sa.Column("role_assignments", postgresql.JSONB(), nullable=True),
368366
sa.Column(
369367
"time_created",
370368
sa.DateTime(timezone=True),
@@ -385,7 +383,7 @@ def upgrade():
385383
sa.Column("id", sa.String(), nullable=False),
386384
sa.Column("name", sa.String(), nullable=True),
387385
sa.Column("type", sa.String(), nullable=True),
388-
sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
386+
sa.Column("tags", postgresql.JSONB(), nullable=True),
389387
sa.Column("billing_account_id", sa.String(), nullable=True),
390388
sa.Column("billing_account_name", sa.String(), nullable=True),
391389
sa.Column("billing_period_start_date", sa.Date(), nullable=True),
@@ -488,7 +486,7 @@ def upgrade():
488486
)
489487

490488

491-
def downgrade():
489+
def downgrade() -> None:
492490
op.drop_table("cost_recovery", schema="accounting")
493491
op.execute(
494492
"DROP MATERIALIZED VIEW {schema}.usage_view;".format(schema="accounting")

0 commit comments

Comments
 (0)