From 96b05a891e6db64d78d61636a5aaa172cd825d60 Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Sat, 21 Nov 2026 03:10:30 +0000 Subject: [PATCH] refactor: move regex_chans table to orm class --- .pyrefly/baseline.json | 245 ------------------ ...emove_extraneous_regex_chans_constraint.py | 82 ++++++ plugins/core/regex_chans.py | 47 ++-- tests/plugin_tests/test_regex_chans.py | 65 ++--- 4 files changed, 140 insertions(+), 299 deletions(-) create mode 100644 alembic/versions/d3c591b463b1_remove_extraneous_regex_chans_constraint.py diff --git a/.pyrefly/baseline.json b/.pyrefly/baseline.json index 51dd8d484..3393a8837 100644 --- a/.pyrefly/baseline.json +++ b/.pyrefly/baseline.json @@ -25606,13 +25606,6 @@ "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", "severity": "error" }, - { - "column": 16, - "path": "plugins/core/regex_chans.py", - "name": "implicit-any-parameter", - "concise_description": "`load_cache` is missing an annotation for parameter `db`", - "severity": "error" - }, { "column": 17, "path": "plugins/core/regex_chans.py", @@ -25620,62 +25613,6 @@ "concise_description": "Cannot infer type of empty container; it will be treated as containing `Any`", "severity": "error" }, - { - "column": 9, - "path": "plugins/core/regex_chans.py", - "name": "unknown-variable-type", - "concise_description": "The type of `row` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 19, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `execute` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 16, - "path": "plugins/core/regex_chans.py", - "name": "unknown-variable-type", - "concise_description": "The type of `conn` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 20, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `connection` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 16, - "path": "plugins/core/regex_chans.py", - "name": "unknown-variable-type", - "concise_description": "The type of `chan` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 20, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `channel` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 18, - "path": "plugins/core/regex_chans.py", - "name": "unknown-variable-type", - "concise_description": "The type of `status` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 22, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `status` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, { "column": 17, "path": "plugins/core/regex_chans.py", @@ -25683,13 +25620,6 @@ "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", "severity": "error" }, - { - "column": 23, - "path": "plugins/core/regex_chans.py", - "name": "unknown-argument-type", - "concise_description": "The type of this argument is unknown", - "severity": "error" - }, { "column": 9, "path": "plugins/core/regex_chans.py", @@ -25711,34 +25641,6 @@ "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", "severity": "error" }, - { - "column": 16, - "path": "plugins/core/regex_chans.py", - "name": "implicit-any-parameter", - "concise_description": "`set_status` is missing an annotation for parameter `db`", - "severity": "error" - }, - { - "column": 31, - "path": "plugins/core/regex_chans.py", - "name": "implicit-any-parameter", - "concise_description": "`set_status` is missing an annotation for parameter `chan`", - "severity": "error" - }, - { - "column": 8, - "path": "plugins/core/regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 12, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `execute` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, { "column": 20, "path": "plugins/core/regex_chans.py", @@ -25746,27 +25648,6 @@ "concise_description": "The type of this argument is unknown", "severity": "error" }, - { - "column": 12, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `execute` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, - { - "column": 34, - "path": "plugins/core/regex_chans.py", - "name": "unknown-argument-type", - "concise_description": "The type of this argument is unknown", - "severity": "error" - }, - { - "column": 8, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `commit` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, { "column": 16, "path": "plugins/core/regex_chans.py", @@ -25781,20 +25662,6 @@ "concise_description": "`delete_status` is missing an annotation for parameter `db`", "severity": "error" }, - { - "column": 34, - "path": "plugins/core/regex_chans.py", - "name": "implicit-any-parameter", - "concise_description": "`delete_status` is missing an annotation for parameter `chan`", - "severity": "error" - }, - { - "column": 8, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `execute` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, { "column": 16, "path": "plugins/core/regex_chans.py", @@ -25802,13 +25669,6 @@ "concise_description": "The type of this argument is unknown", "severity": "error" }, - { - "column": 8, - "path": "plugins/core/regex_chans.py", - "name": "unknown-attribute-access", - "concise_description": "The type of attribute `commit` is unknown; it is inferred as an implicit `Any`", - "severity": "error" - }, { "column": 16, "path": "plugins/core/regex_chans.py", @@ -73626,76 +73486,6 @@ "concise_description": "The type of attribute `bot` is unknown; it is inferred as an implicit `Any`", "severity": "error" }, - { - "column": 21, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 21, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `set_status` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 21, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 21, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `set_status` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 21, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 21, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `delete_status` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 21, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 25, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 25, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, - { - "column": 25, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, { "column": 5, "path": "tests/plugin_tests/test_regex_chans.py", @@ -73703,13 +73493,6 @@ "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", "severity": "error" }, - { - "column": 25, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, { "column": 43, "path": "tests/plugin_tests/test_regex_chans.py", @@ -73724,13 +73507,6 @@ "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", "severity": "error" }, - { - "column": 25, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, { "column": 43, "path": "tests/plugin_tests/test_regex_chans.py", @@ -73745,13 +73521,6 @@ "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", "severity": "error" }, - { - "column": 25, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, { "column": 43, "path": "tests/plugin_tests/test_regex_chans.py", @@ -73766,13 +73535,6 @@ "concise_description": "The type of this argument is partially unknown; it contains an implicit `Any`", "severity": "error" }, - { - "column": 25, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, { "column": 43, "path": "tests/plugin_tests/test_regex_chans.py", @@ -73941,13 +73703,6 @@ "concise_description": "Class member `TestResetRegex.get_initial_db_data` overrides a member in a parent class but is missing an `@override` decorator", "severity": "error" }, - { - "column": 17, - "path": "tests/plugin_tests/test_regex_chans.py", - "name": "partially-unknown-type", - "concise_description": "The type of attribute `load_cache` is partially unknown; it contains an implicit `Any`", - "severity": "error" - }, { "column": 25, "path": "tests/plugin_tests/test_remind.py", diff --git a/alembic/versions/d3c591b463b1_remove_extraneous_regex_chans_constraint.py b/alembic/versions/d3c591b463b1_remove_extraneous_regex_chans_constraint.py new file mode 100644 index 000000000..e80d47910 --- /dev/null +++ b/alembic/versions/d3c591b463b1_remove_extraneous_regex_chans_constraint.py @@ -0,0 +1,82 @@ +"""remove extraneous regex_chans constraint + +Revision ID: d3c591b463b1 +Revises: 524103ded692 +Create Date: 2026-11-21 04:43:39.485280 + +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import sqlalchemy as sa +from alembic import op + +if TYPE_CHECKING: + from collections.abc import Sequence + + +# revision identifiers, used by Alembic. +revision: str = "d3c591b463b1" +down_revision: str | Sequence[str] | None = "524103ded692" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + """Upgrade schema.""" + bind = op.get_bind() + inspect = sa.inspect(bind) + table = sa.table( + "regex_chans", + sa.column("connection", sa.String), + sa.column("channel", sa.String), + sa.column("status", sa.String), + ) + if inspect.has_table(table.name): + old_ignores = bind.execute(sa.select(table)).mappings().all() + + old_table = f"{table.name}_pre_{revision}" + op.rename_table(table.name, old_table) + t = op.create_table( + table.name, + sa.Column("connection", sa.String, primary_key=True), + sa.Column("channel", sa.String, primary_key=True), + sa.Column("status", sa.String), + ) + op.bulk_insert( + t, + [ + dict(row) + for row in old_ignores + if row["connection"] is not None and row["channel"] is not None + ], + ) + op.drop_table(old_table) + + +def downgrade() -> None: + """Downgrade schema.""" + bind = op.get_bind() + inspect = sa.inspect(bind) + table = sa.table( + "regex_chans", + sa.column("connection", sa.String), + sa.column("channel", sa.String), + sa.column("status", sa.String), + ) + if inspect.has_table(table.name): + old_ignores = bind.execute(sa.select(table)).mappings().all() + + old_table = f"{table.name}_pre_{revision}_downgrade" + op.rename_table(table.name, old_table) + t = op.create_table( + table.name, + sa.Column("connection", sa.String), + sa.Column("channel", sa.String), + sa.Column("status", sa.String), + sa.UniqueConstraint("connection", "channel"), + ) + op.bulk_insert(t, [dict(row) for row in old_ignores]) + op.drop_table(old_table) diff --git a/plugins/core/regex_chans.py b/plugins/core/regex_chans.py index 1e582ad54..47be5cea9 100644 --- a/plugins/core/regex_chans.py +++ b/plugins/core/regex_chans.py @@ -1,9 +1,10 @@ from __future__ import annotations import logging -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, final -from sqlalchemy import Column, String, Table, UniqueConstraint +from sqlalchemy import delete, insert, update +from sqlalchemy.orm import Mapped, Session, mapped_column from cloudbot import hook from cloudbot.client import Client @@ -17,14 +18,15 @@ from cloudbot.event_functions import EventMessage, EventNotice from cloudbot.plugin_hooks import Hook -table = Table( - "regex_chans", - database.metadata, - Column("connection", String), - Column("channel", String), - Column("status", String), - UniqueConstraint("connection", "channel"), -) + +@final +class RegexChans(database.Base): + __tablename__ = "regex_chans" + + connection: Mapped[str] = mapped_column(primary_key=True) + channel: Mapped[str] = mapped_column(primary_key=True) + status: Mapped[str | None] + ENABLED = "ENABLED" DISABLED = "DISABLED" @@ -38,9 +40,9 @@ @hook.on_start() -def load_cache(db) -> None: +def load_cache(db: Session) -> None: new_cache = {} - for row in db.execute(table.select()): + for row in db.query(RegexChans).all(): conn = row.connection chan = row.channel status = row.status @@ -51,8 +53,7 @@ def load_cache(db) -> None: else: # Unknown values just use the default (existing behavior) logger.warning( - "[regex_chans] Unknown status: %s, falling back to default", - tuple(row), + "[regex_chans] Unknown status: %s, falling back to default", row ) continue @@ -62,20 +63,20 @@ def load_cache(db) -> None: status_cache.update(new_cache) -def set_status(db, conn: str, chan, status: bool) -> None: +def set_status(db: Session, conn: str, chan: str, status: bool) -> None: status_value = ENABLED if status else DISABLED if (conn, chan) in status_cache: # if we have a set value, update db.execute( - table.update() + update(RegexChans) .values(status=status_value) - .where(table.c.connection == conn) - .where(table.c.channel == chan) + .where(RegexChans.connection == conn) + .where(RegexChans.channel == chan) ) else: # otherwise, insert db.execute( - table.insert().values( + insert(RegexChans).values( connection=conn, channel=chan, status=status_value ) ) @@ -84,11 +85,11 @@ def set_status(db, conn: str, chan, status: bool) -> None: load_cache(db) -def delete_status(db, conn: str, chan) -> None: +def delete_status(db: Session, conn: str, chan: str) -> None: db.execute( - table.delete() - .where(table.c.connection == conn) - .where(table.c.channel == chan) + delete(RegexChans) + .where(RegexChans.connection == conn) + .where(RegexChans.channel == chan) ) db.commit() diff --git a/tests/plugin_tests/test_regex_chans.py b/tests/plugin_tests/test_regex_chans.py index 583b476fe..640e94e99 100644 --- a/tests/plugin_tests/test_regex_chans.py +++ b/tests/plugin_tests/test_regex_chans.py @@ -13,6 +13,7 @@ from cloudbot.plugin_hooks import CommandHook, EventHook, RegexHook from plugins.core import regex_chans from tests.util import wrap_hook_response +from tests.util.mock_bot import CloudBotLogCaptureFixture, MockBot from tests.util.mock_conn import MockClient if TYPE_CHECKING: @@ -37,15 +38,15 @@ def clear_cache() -> Generator[None]: def test_status_new(mock_db: MockDB) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: - mock_db.load_data(regex_chans.table, []) + mock_db.load_data(regex_chans.RegexChans, []) regex_chans.load_cache(session) regex_chans.set_status(session, "net", "#chan", True) - assert mock_db.get_data(regex_chans.table) == [ + assert mock_db.get_data(regex_chans.RegexChans) == [ ("net", "#chan", "ENABLED") ] @@ -53,10 +54,10 @@ def test_status_new(mock_db: MockDB) -> None: def test_status_existing(mock_db: MockDB) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [{"connection": "net", "channel": "#chan", "status": "DISABLED"}], ) @@ -64,7 +65,7 @@ def test_status_existing(mock_db: MockDB) -> None: regex_chans.set_status(session, "net", "#chan", True) - assert mock_db.get_data(regex_chans.table) == [ + assert mock_db.get_data(regex_chans.RegexChans) == [ ("net", "#chan", "ENABLED") ] @@ -72,10 +73,10 @@ def test_status_existing(mock_db: MockDB) -> None: def test_delete_status(mock_db: MockDB) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [{"connection": "net", "channel": "#chan", "status": "DISABLED"}], ) @@ -83,16 +84,16 @@ def test_delete_status(mock_db: MockDB) -> None: regex_chans.delete_status(session, "net", "#chan") - assert mock_db.get_data(regex_chans.table) == [] + assert mock_db.get_data(regex_chans.RegexChans) == [] assert regex_chans.status_cache == {} def test_listregex(mock_db: MockDB, mock_bot: MockBot) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ {"connection": "net", "channel": "#chan", "status": "DISABLED"}, {"connection": "net", "channel": "#chan1", "status": "ENABLED"}, @@ -125,10 +126,10 @@ def test_listregex(mock_db: MockDB, mock_bot: MockBot) -> None: class TestRegexStatus: def test_current_chan(self, mock_db: MockDB, mock_bot: MockBot) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ { "connection": "net", @@ -167,10 +168,10 @@ def test_current_chan(self, mock_db: MockDB, mock_bot: MockBot) -> None: ) def test_other_chan(self, mock_db: MockDB, mock_bot: MockBot) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ { "connection": "net", @@ -211,10 +212,10 @@ def test_other_chan(self, mock_db: MockDB, mock_bot: MockBot) -> None: def test_other_chan_no_prefix( self, mock_db: MockDB, mock_bot: MockBot ) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ { "connection": "net", @@ -262,10 +263,10 @@ async def test_block_regex_hook( caplog_bot: CloudBotLogCaptureFixture, ) -> None: mock_bot = mock_bot_factory(db=mock_db) - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ { "connection": "net", @@ -340,10 +341,10 @@ async def test_allow_regex_hook( caplog_bot: CloudBotLogCaptureFixture, ) -> None: mock_bot = mock_bot_factory(db=mock_db) - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ { "connection": "net", @@ -418,10 +419,10 @@ async def test_no_block_other_hook( caplog_bot: CloudBotLogCaptureFixture, ) -> None: mock_bot = mock_bot_factory(db=mock_db) - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ { "connection": "net", @@ -486,10 +487,10 @@ async def test_allow_other_hook( caplog_bot: CloudBotLogCaptureFixture, ) -> None: mock_bot = mock_bot_factory(db=mock_db) - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) with mock_db.session() as session: mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [ { "connection": "net", @@ -568,8 +569,8 @@ async def test_set( self, mock_db: MockDB, mock_bot_factory: MockBotFactory ) -> None: mock_bot = mock_bot_factory(db=mock_db) - regex_chans.table.create(mock_db.engine) - mock_db.load_data(regex_chans.table, self.get_initial_db_data()) + mock_db.create_table(regex_chans.RegexChans) + mock_db.load_data(regex_chans.RegexChans, self.get_initial_db_data()) conn = MagicMock() conn.configure_mock(name="net", config={}) @@ -610,7 +611,8 @@ async def test_set( assert hook.mock_calls == [] assert ( - mock_db.get_data(regex_chans.table) == self.get_expected_db_data() + mock_db.get_data(regex_chans.RegexChans) + == self.get_expected_db_data() ) @@ -759,9 +761,9 @@ def get_initial_db_data(self) -> list[dict[str, str]]: def test_bad_value( mock_db: MockDB, caplog_bot: CloudBotLogCaptureFixture ) -> None: - regex_chans.table.create(mock_db.engine) + mock_db.create_table(regex_chans.RegexChans) mock_db.load_data( - regex_chans.table, + regex_chans.RegexChans, [{"connection": "net", "channel": "#chan", "status": "UNKNOWN"}], ) @@ -770,6 +772,7 @@ def test_bad_value( assert not regex_chans.status_cache assert caplog_bot.messages == [ - "[regex_chans] Unknown status: ('net', '#chan', 'UNKNOWN'), falling back " + "[regex_chans] Unknown status: RegexChans(connection='net', " + "channel='#chan', status='UNKNOWN'), falling back " "to default" ]