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

Allow additional types for SQL parameters in migrations.RunSQL() #1803

Merged
merged 3 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions django-stubs/db/migrations/operations/special.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class SeparateDatabaseAndState(Operation):

class RunSQL(Operation):
noop: Literal[""]
sql: str | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[str] | None]]
reverse_sql: str | None | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[str] | None]]
sql: str | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[Any] | None]]
reverse_sql: str | None | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[Any] | None]]
state_operations: Sequence[Operation]
hints: Mapping[str, Any]
def __init__(
self,
sql: str | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[str] | None]],
reverse_sql: str | None | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[str] | None]] = ...,
sql: str | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[Any] | None]],
Copy link
Member

Choose a reason for hiding this comment

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

We now don't need _ListOrTuple[str | tuple[str, dict[str, Any] part

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure about that ? I feel like every variant is needed here: sql il either:

  • str: -> RunSQL(sql="SOME SQL")
  • _ListOrTuple[str] -> RunSQL(sql=("SOME SQLS", "SOME SQLS")) / RunSQL(sql=["SOME SQLS", "SOME SQLS"])
  • _ListOrTuple[tuple[str, dict[str, Any]] -> RunSQL(sql=[("SOME SQLS %(VAL)s", {"VAL": "FOO"})]
  • _ListOrTuple[tuple[str, _ListOrTuple[Any]] ->RunSQL(sql=[("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])])
  • _ListOrTuple[tuple[str, None] -> RunSQL([("INSERT INTO musician (name) VALUES ('Reinhardt');", None)])

I don't really see how it can be simplified, am I missing something here ?

btw, I'v added another test case and a TypeAlias to reduce duplication

Copy link
Member

Choose a reason for hiding this comment

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

We have _ListOrTuple[Any], Any matches all possible inner types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is only part of the union for the second member of the tuple, not for the whole thing

str | 
_ListOrTuple[
    str |
    tuple[
        str, 
        dict[str, Any] | _ListOrTuple[Any] | None
    ]
]

reverse_sql: str | None | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[Any] | None]] = ...,
state_operations: Sequence[Operation] | None = ...,
hints: Mapping[str, Any] | None = ...,
elidable: bool = ...,
Expand Down
14 changes: 12 additions & 2 deletions tests/typecheck/db/migrations/test_operations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@

RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])], reverse_sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])])

RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[<nothing>, <nothing>]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type]
RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])]) # E: List item 0 has incompatible type "object"; expected "str" [list-item]
RunSQL("INSERT INTO musician (name) VALUES ('Reinhardt');")
RunSQL([("INSERT INTO musician (name) VALUES ('Reinhardt');", None)])
RunSQL([("INSERT INTO musician (name) VALUES (%s);", ["Reinhardt"])])

query = "UPDATE posts SET category = %s WHERE category = ANY(%s);"
RunSQL([(query, ['new category', ['retired category', 'another retired category']])])

RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])])

RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[<nothing>, <nothing>]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type]
RunSQL(sql=("SOME SQL", 1)) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, int]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type]
RunSQL(sql=("SOME SQL", None)) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, None]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type]