Skip to content

Improve database backend typing#3190

Open
dekkers wants to merge 1 commit intotypeddjango:masterfrom
dekkers:improve-database-backend-typing
Open

Improve database backend typing#3190
dekkers wants to merge 1 commit intotypeddjango:masterfrom
dekkers:improve-database-backend-typing

Conversation

@dekkers
Copy link

@dekkers dekkers commented Mar 15, 2026

A few improvements to the typing in the database backend code:

  • Changed Any to better types in some places.
  • Removed | None from arguments where the method uses the argument without checking whether it is None.

settings_dict: dict[str, Any]
alias: str
queries_log: Any
queries_log: deque
Copy link
Member

Choose a reason for hiding this comment

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

Can you please specify the type arg?

Copy link
Author

Choose a reason for hiding this comment

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

I have added the type arguments.

def get_primary_key_column(self, cursor: CursorWrapper | None, table_name: str) -> str | None: ...
def get_primary_key_columns(self, cursor: CursorWrapper | None, table_name: str) -> list[str] | None: ...
def get_constraints(self, cursor: CursorWrapper | None, table_name: str) -> Any: ...
def get_sequences(self, cursor: CursorWrapper, table_name: str, table_fields: Iterable = ()) -> list[dict]: ...
Copy link
Member

Choose a reason for hiding this comment

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

Same here

def get_relations(self, cursor: CursorWrapper, table_name: str) -> dict[str, tuple[str, str]]: ...
def get_primary_key_column(self, cursor: CursorWrapper, table_name: str) -> str | None: ...
def get_primary_key_columns(self, cursor: CursorWrapper, table_name: str) -> list[str] | None: ...
def get_constraints(self, cursor: CursorWrapper, table_name: str) -> dict[str, dict]: ...
Copy link
Member

Choose a reason for hiding this comment

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

and here

@dekkers dekkers force-pushed the improve-database-backend-typing branch from b8845f0 to 31b6e7a Compare March 15, 2026 20:53
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

def table_names(self, cursor: CursorWrapper | None = None, include_views: bool = False) -> list[str]: ...
def get_table_list(self, cursor: CursorWrapper | None) -> Any: ...
def get_table_description(self, cursor: CursorWrapper | None, table_name: str) -> Any: ...
def get_table_list(self, cursor: CursorWrapper) -> list: ...
Copy link
Member

Choose a reason for hiding this comment

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

we miss type arg here

Copy link
Author

Choose a reason for hiding this comment

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

I added type args for this one and the one below.

def get_constraints(self, cursor: CursorWrapper | None, table_name: str) -> Any: ...
def get_sequences(
self, cursor: CursorWrapper, table_name: str, table_fields: Iterable[Field] = ()
) -> list[dict]: ...
Copy link
Member

Choose a reason for hiding this comment

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

and here for dict

@github-actions

This comment has been minimized.

@dekkers dekkers force-pushed the improve-database-backend-typing branch from 31b6e7a to 0a68003 Compare March 22, 2026 14:24
@dekkers
Copy link
Author

dekkers commented Mar 22, 2026

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

zulip (https://github.com/zulip/zulip)
+ zerver/lib/migrate.py:120: error: Incompatible types in assignment (expression has type "dict[Any, Any]", variable has type "_ConstraintDict")  [assignment]
+ zerver/lib/migrate.py:121: error: TypedDict "_ConstraintDict" has no key "name"  [typeddict-item]
+ zerver/lib/migrate.py:122: error: TypedDict "_ConstraintDict" has no key "column"  [typeddict-item]
+ zerver/lib/migrate.py:122: note: Did you mean "columns"?

This is an issue in the zulip code, both for loops use the same infodict name for the variable name but the types are different.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

zulip (https://github.com/zulip/zulip)
+ zerver/lib/migrate.py:120: error: Incompatible types in assignment (expression has type "_SequenceDict", variable has type "_ConstraintDict")  [assignment]
+ zerver/lib/migrate.py:121: error: TypedDict "_ConstraintDict" has no key "name"  [typeddict-item]
+ zerver/lib/migrate.py:122: error: TypedDict "_ConstraintDict" has no key "column"  [typeddict-item]
+ zerver/lib/migrate.py:122: note: Did you mean "columns"?

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants