Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type annotation for RunSQL #1090

Merged
merged 3 commits into from
Aug 9, 2022
Merged

Conversation

PIG208
Copy link
Contributor

@PIG208 PIG208 commented Aug 5, 2022

I have made things!

The sqls to be executed do not necessarily need to be a homogeneous list
or tuple containing only lists or tuples or strs. It can be a mix of
everything acceptable for the cursor. RunSQL iterates through the list or tuple
and executes them separately.

Also, the 2-item tuple for sql can have a dict as the second item
for parameters. This behavior is the same as using
cursor.execute for backends except for SQLite.

A test case is added along with this change.

Relevant implementation
https://github.com/django/django/blob/5f760025004bdb02f9844011033459c30347f215/django/db/migrations/operations/special.py#L119-L133

Related issues

PIG208 added 3 commits August 5, 2022 10:49
The sqls to be executed do not necessarily need to be a homogeneous list
or tuple containing only lists or tuples or strs. It can be a mix of
everything.

Signed-off-by: Zixuan James Li <[email protected]>
The 2-item tuple for `sql` can have a `dict` as the second item
for parameters. This behavior is the same as using
`cursor.execute` for backends except SQLite.

Relevant implementation:
https://github.com/django/django/blob/5f760025004bdb02f9844011033459c30347f215/django/db/migrations/operations/special.py#L119-L133

Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
@PIG208 PIG208 closed this Aug 8, 2022
@PIG208 PIG208 reopened this Aug 8, 2022
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.

Thank you!

@@ -21,15 +21,17 @@ class SeparateDatabaseAndState(Operation):

class RunSQL(Operation):
noop: Literal[""] = ...
sql: Union[str, _ListOrTuple[str], _ListOrTuple[Tuple[str, Optional[_ListOrTuple[str]]]]] = ...
reverse_sql: Optional[Union[str, _ListOrTuple[str], _ListOrTuple[Tuple[str, Optional[_ListOrTuple[str]]]]]] = ...
sql: Union[str, _ListOrTuple[Union[str, Tuple[str, Union[Dict[str, Any], Optional[_ListOrTuple[str]]]]]]] = ...
Copy link
Member

Choose a reason for hiding this comment

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

Oh my...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes... I know

@sobolevn sobolevn merged commit a144529 into typeddjango:master Aug 9, 2022
@PIG208 PIG208 deleted the runsql branch August 9, 2022 15:27
@intgr
Copy link
Collaborator

intgr commented Nov 8, 2022

This causes false positive when used with a list[str] variable:

sqls: list[str] = ["SOME SQLS", "SOME SQLS"]
RunSQL(sql=sqls, reverse_sql=[])

error: Argument "sql" to "RunSQL" has incompatible type "List[str]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], List[str], Tuple[str, ...], Tuple[], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], List[str], Tuple[str, ...], Tuple[], None]]], ...], Tuple[]]]" [arg-type]

I don't think there's a simple fix though. Ordinariy we would change List[] to Sequence[], but the RunSQL code is actually very specific that it only accepts lists and tuples.

@sobolevn
Copy link
Member

sobolevn commented Nov 8, 2022

Wild guess: maybe moving list[str] to the left? Like Union[str, list[str], ...]?
Just a guess though.

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.

3 participants