Skip to content

refactor(dbapi): add type annotations to pass mypy (part of #692)#800

Open
rikettsie wants to merge 7 commits into
ClickHouse:mainfrom
rikettsie:refactor/dbapi-type-annotations
Open

refactor(dbapi): add type annotations to pass mypy (part of #692)#800
rikettsie wants to merge 7 commits into
ClickHouse:mainfrom
rikettsie:refactor/dbapi-type-annotations

Conversation

@rikettsie

@rikettsie rikettsie commented Jun 13, 2026

Copy link
Copy Markdown

Summary

Annotate the dbapi layer (initialization, connection, cursor) so it passes mypy cleanly and can be removed from the ignore_errors baseline in pyproject.toml. This is part of issue #692.

Some non-obvious choices I made:

  • On Cursor, I declared self.names and self.types as Sequence (not list) since they can be both tuple (from query_result.column_names) and list (from the comprehensions a couple of lines below).

  • I kept check_valid() the void guard it was, but enforced the use of cast(Sequence, self.data) in callers (e.g. fetchall, ...) to inform mypy the value is never None after that point. That's a zero-cost type hint, another option could be using an assert.

  • I annotaed username and password in connect(), with str (not str | None) since their defaults were "". By contrast, None carries no distinct meaning here, unlike host, database, and interface which can legitimately be absent.

  • In connection.py, I annotated the return type of command() as Any because its type is unknown until we annotate the driver layer in a follow-up PRs.
    In all the other points where I used Any, the data type is genuinely polymorphic (in my understanding of the possibilities).

Checklist

  • A human-readable description of the changes was provided to include in CHANGELOG

@CLAassistant

CLAassistant commented Jun 13, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the effort in #692 by adding/adjusting type annotations in the clickhouse_connect.dbapi layer so it passes mypy cleanly, and then removing the DB-API modules from the mypy ignore_errors baseline in pyproject.toml.

Changes:

  • Remove clickhouse_connect.dbapi* modules from the mypy ignore baseline.
  • Add/adjust type annotations for DB-API connect(), Connection, and Cursor methods/properties.
  • Minor refactors in Cursor around bulk insert handling (e.g., op_columns normalization) to satisfy typing.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
pyproject.toml Removes DB-API modules from the mypy ignore baseline so they must type-check cleanly.
clickhouse_connect/dbapi/cursor.py Adds method/property annotations and refines some insert-path typing logic.
clickhouse_connect/dbapi/connection.py Annotates DB-API connection wrapper and normalizes args passed to create_client.
clickhouse_connect/dbapi/init.py Annotates the DB-API connect() entrypoint and kwargs typing.

Comment thread clickhouse_connect/dbapi/connection.py
Comment thread clickhouse_connect/dbapi/connection.py Outdated
Comment thread clickhouse_connect/dbapi/cursor.py
Comment thread clickhouse_connect/dbapi/cursor.py
Comment thread clickhouse_connect/dbapi/cursor.py Outdated
- Port annotation 'int | None = 0' inconsistency;
- Use explicit 'is None' check instead of 'or' to avoid silently treating an empty string as __default__;
- Change _summary type from 'dict[str, str]' to 'dict[str, Any]';
- Ensure _try_bulk_insert returns resetting _rowcount and _ix.
@rikettsie rikettsie requested a review from Copilot June 14, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread clickhouse_connect/dbapi/connection.py
Comment thread clickhouse_connect/dbapi/__init__.py
Comment thread clickhouse_connect/dbapi/cursor.py
@rikettsie rikettsie requested a review from Copilot June 14, 2026 16:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread clickhouse_connect/dbapi/cursor.py
Comment thread clickhouse_connect/dbapi/connection.py
Comment thread clickhouse_connect/dbapi/__init__.py
@rikettsie

Copy link
Copy Markdown
Author

@mshustov, I addressed all Copilot feedback. A couple of comments were genuine fixes. The others were false alarms as I explained in the resolution threads, specifically around "__default__" and port=0. These are documented sentinels in create_client and _parse_connection_params, not values introduced by this PR.

@rikettsie

rikettsie commented Jun 15, 2026

Copy link
Copy Markdown
Author

@joe-clickhouse, @peter-leonov-ch, @mshustov: the CI failure is a network timeout pulling the ClickHouse Docker image from Docker Hub, not related to the changes. Could you re-run it?

@joe-clickhouse

Copy link
Copy Markdown
Contributor

@rikettsie awesome work so far! The annotations look good and I agree with your reasoning on the port and database sentinels. I took a look and also confirmed both Copilot's objections there are false alarms.

Two requests before merge though. The bulk-insert path now updates rowcount, _ix, and summary, and adds a Sequence guard. Those are good behavior improvements but they go beyond annotations. So please add unit tests for them. E.g. rowcount equals written_rows after a bulk executemany, the summary is appended, and a generator passed as parameters falls through to the row path without raising. It looks like the existing bulk-insert tests use a bare Mock client so none of this is covered yet. Second, I'd prefer the changelog entry highlight the the rowcount fix rather than the annotations right now. I think a note about the annotations work should get a changelog when py.typed is restored at the end of the series. Thanks agai!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants