]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
restore default values for keyword arguments in stubs
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 30 Aug 2021 18:30:37 +0000 (14:30 -0400)
committermike bayer <mike_mp@zzzcomputing.com>
Mon, 30 Aug 2021 20:14:31 +0000 (20:14 +0000)
Fixed issue in generated .pyi files where default values for ``Optional``
arguments were missing, thereby causing mypy to consider them as required.

Change-Id: I08c4cd7bd04c6763fdc9c5b42abbdcca8fd90e0e
Fixes: #895
alembic/context.pyi
alembic/op.pyi
docs/build/unreleased/895.rst [new file with mode: 0644]
tools/write_pyi.py

index 8a33d520afc3564b05cb94ccfcb71a45103df57c..d789a22447cd5e886d6cbef0682df620286061b9 100644 (file)
@@ -63,32 +63,32 @@ def begin_transaction() -> Union["_ProxyTransaction", ContextManager]:
     """
 
 def configure(
-    connection: Optional["Connection"],
-    url: Optional[str],
-    dialect_name: Optional[str],
-    dialect_opts: Optional[dict],
-    transactional_ddl: Optional[bool],
-    transaction_per_migration: bool,
-    output_buffer: Optional[TextIO],
-    starting_rev: Optional[str],
-    tag: Optional[str],
-    template_args: Optional[dict],
-    render_as_batch: bool,
-    target_metadata: Optional["MetaData"],
-    include_name: Optional[Callable],
-    include_object: Optional[Callable],
-    include_schemas: bool,
-    process_revision_directives: Optional[Callable],
-    compare_type: bool,
-    compare_server_default: bool,
-    render_item: Optional[Callable],
-    literal_binds: bool,
-    upgrade_token: str,
-    downgrade_token: str,
-    alembic_module_prefix: str,
-    sqlalchemy_module_prefix: str,
-    user_module_prefix: Optional[str],
-    on_version_apply: Optional[Callable],
+    connection: Optional["Connection"] = None,
+    url: Optional[str] = None,
+    dialect_name: Optional[str] = None,
+    dialect_opts: Optional[dict] = None,
+    transactional_ddl: Optional[bool] = None,
+    transaction_per_migration: bool = False,
+    output_buffer: Optional[TextIO] = None,
+    starting_rev: Optional[str] = None,
+    tag: Optional[str] = None,
+    template_args: Optional[dict] = None,
+    render_as_batch: bool = False,
+    target_metadata: Optional["MetaData"] = None,
+    include_name: Optional[Callable] = None,
+    include_object: Optional[Callable] = None,
+    include_schemas: bool = False,
+    process_revision_directives: Optional[Callable] = None,
+    compare_type: bool = False,
+    compare_server_default: bool = False,
+    render_item: Optional[Callable] = None,
+    literal_binds: bool = False,
+    upgrade_token: str = "upgrades",
+    downgrade_token: str = "downgrades",
+    alembic_module_prefix: str = "op.",
+    sqlalchemy_module_prefix: str = "sa.",
+    user_module_prefix: Optional[str] = None,
+    on_version_apply: Optional[Callable] = None,
     **kw
 ) -> None:
     """Configure a :class:`.MigrationContext` within this
@@ -524,7 +524,7 @@ def configure(
 
     """
 
-def execute(sql, execution_options):
+def execute(sql, execution_options=None):
     """Execute the given SQL using the current change context.
 
     The behavior of :meth:`.execute` is the same
@@ -629,7 +629,7 @@ def get_tag_argument() -> Optional[str]:
 
     """
 
-def get_x_argument(as_dictionary: bool):
+def get_x_argument(as_dictionary: bool = False):
     """Return the value(s) passed for the ``-x`` argument, if any.
 
     The ``-x`` argument is an open ended flag that allows any user-defined
index 76912dc872cfe42d7c4af8218c491bd6d9151fc5..f5aca925375ade91ed203ad1ddf2b8f08b2dc29d 100644 (file)
@@ -25,6 +25,7 @@ if TYPE_CHECKING:
     from sqlalchemy.sql.schema import Identity
     from sqlalchemy.sql.schema import Table
     from sqlalchemy.sql.type_api import TypeEngine
+    from sqlalchemy.util import immutabledict
 
     from .operations.ops import MigrateOperation
     from .util.sqla_compat import _literal_bindparam
@@ -32,7 +33,7 @@ if TYPE_CHECKING:
 ### end imports ###
 
 def add_column(
-    table_name: str, column: "Column", schema: Optional[str]
+    table_name: str, column: "Column", schema: Optional[str] = None
 ) -> Optional["Table"]:
     """Issue an "add column" instruction using the current
     migration context.
@@ -86,16 +87,18 @@ def add_column(
 def alter_column(
     table_name: str,
     column_name: str,
-    nullable: Optional[bool],
-    comment: Union[str, bool, None],
-    server_default: Any,
-    new_column_name: Optional[str],
-    type_: Union["TypeEngine", Type["TypeEngine"], None],
-    existing_type: Union["TypeEngine", Type["TypeEngine"], None],
-    existing_server_default: Union[str, bool, "Identity", "Computed", None],
-    existing_nullable: Optional[bool],
-    existing_comment: Optional[str],
-    schema: Optional[str],
+    nullable: Optional[bool] = None,
+    comment: Union[str, bool, None] = False,
+    server_default: Any = False,
+    new_column_name: Optional[str] = None,
+    type_: Union["TypeEngine", Type["TypeEngine"], None] = None,
+    existing_type: Union["TypeEngine", Type["TypeEngine"], None] = None,
+    existing_server_default: Union[
+        str, bool, "Identity", "Computed", None
+    ] = False,
+    existing_nullable: Optional[bool] = None,
+    existing_comment: Optional[str] = None,
+    schema: Optional[str] = None,
     **kw
 ) -> Optional["Table"]:
     """Issue an "alter column" instruction using the
@@ -189,15 +192,15 @@ def alter_column(
 
 def batch_alter_table(
     table_name,
-    schema,
-    recreate,
-    partial_reordering,
-    copy_from,
-    table_args,
-    table_kwargs,
-    reflect_args,
-    reflect_kwargs,
-    naming_convention,
+    schema=None,
+    recreate="auto",
+    partial_reordering=None,
+    copy_from=None,
+    table_args=(),
+    table_kwargs=immutabledict({}),
+    reflect_args=(),
+    reflect_kwargs=immutabledict({}),
+    naming_convention=None,
 ):
     """Invoke a series of per-table migrations in batch.
 
@@ -337,7 +340,9 @@ def batch_alter_table(
     """
 
 def bulk_insert(
-    table: Union["Table", "TableClause"], rows: List[dict], multiinsert: bool
+    table: Union["Table", "TableClause"],
+    rows: List[dict],
+    multiinsert: bool = True,
 ) -> None:
     """Issue a "bulk insert" operation using the current
     migration context.
@@ -418,7 +423,7 @@ def create_check_constraint(
     constraint_name: Optional[str],
     table_name: str,
     condition: "BinaryExpression",
-    schema: Optional[str],
+    schema: Optional[str] = None,
     **kw
 ) -> Optional["Table"]:
     """Issue a "create check constraint" instruction using the
@@ -508,13 +513,13 @@ def create_foreign_key(
     referent_table: str,
     local_cols: List[str],
     remote_cols: List[str],
-    onupdate: Optional[str],
-    ondelete: Optional[str],
-    deferrable: Optional[bool],
-    initially: Optional[str],
-    match: Optional[str],
-    source_schema: Optional[str],
-    referent_schema: Optional[str],
+    onupdate: Optional[str] = None,
+    ondelete: Optional[str] = None,
+    deferrable: Optional[bool] = None,
+    initially: Optional[str] = None,
+    match: Optional[str] = None,
+    source_schema: Optional[str] = None,
+    referent_schema: Optional[str] = None,
     **dialect_kw
 ) -> Optional["Table"]:
     """Issue a "create foreign key" instruction using the
@@ -566,8 +571,8 @@ def create_index(
     index_name: str,
     table_name: str,
     columns: Sequence[Union[str, "TextClause", "Function"]],
-    schema: Optional[str],
-    unique: bool,
+    schema: Optional[str] = None,
+    unique: bool = False,
     **kw
 ) -> Optional["Table"]:
     """Issue a "create index" instruction using the current
@@ -616,7 +621,7 @@ def create_primary_key(
     constraint_name: str,
     table_name: str,
     columns: List[str],
-    schema: Optional[str],
+    schema: Optional[str] = None,
 ) -> Optional["Table"]:
     """Issue a "create primary key" instruction using the current
     migration context.
@@ -736,8 +741,8 @@ def create_table(table_name: str, *columns, **kw) -> Optional["Table"]:
 def create_table_comment(
     table_name: str,
     comment: Optional[str],
-    existing_comment: None,
-    schema: Optional[str],
+    existing_comment: None = None,
+    schema: Optional[str] = None,
 ) -> Optional["Table"]:
     """Emit a COMMENT ON operation to set the comment for a table.
 
@@ -763,7 +768,7 @@ def create_unique_constraint(
     constraint_name: Optional[str],
     table_name: str,
     columns: Sequence[str],
-    schema: Optional[str],
+    schema: Optional[str] = None,
     **kw
 ) -> Any:
     """Issue a "create unique constraint" instruction using the
@@ -805,7 +810,7 @@ def create_unique_constraint(
     """
 
 def drop_column(
-    table_name: str, column_name: str, schema: Optional[str], **kw
+    table_name: str, column_name: str, schema: Optional[str] = None, **kw
 ) -> Optional["Table"]:
     """Issue a "drop column" instruction using the current
     migration context.
@@ -847,8 +852,8 @@ def drop_column(
 def drop_constraint(
     constraint_name: str,
     table_name: str,
-    type_: Optional[str],
-    schema: Optional[str],
+    type_: Optional[str] = None,
+    schema: Optional[str] = None,
 ) -> Optional["Table"]:
     """Drop a constraint of the given name, typically via DROP CONSTRAINT.
 
@@ -864,7 +869,10 @@ def drop_constraint(
     """
 
 def drop_index(
-    index_name: str, table_name: Optional[str], schema: Optional[str], **kw
+    index_name: str,
+    table_name: Optional[str] = None,
+    schema: Optional[str] = None,
+    **kw
 ) -> Optional["Table"]:
     """Issue a "drop index" instruction using the current
     migration context.
@@ -888,7 +896,9 @@ def drop_index(
 
     """
 
-def drop_table(table_name: str, schema: Optional[str], **kw: Any) -> None:
+def drop_table(
+    table_name: str, schema: Optional[str] = None, **kw: Any
+) -> None:
     """Issue a "drop table" instruction using the current
     migration context.
 
@@ -908,7 +918,9 @@ def drop_table(table_name: str, schema: Optional[str], **kw: Any) -> None:
     """
 
 def drop_table_comment(
-    table_name: str, existing_comment: Optional[str], schema: Optional[str]
+    table_name: str,
+    existing_comment: Optional[str] = None,
+    schema: Optional[str] = None,
 ) -> Optional["Table"]:
     """Issue a "drop table comment" operation to
     remove an existing comment set on a table.
@@ -928,7 +940,7 @@ def drop_table_comment(
     """
 
 def execute(
-    sqltext: Union[str, "TextClause", "Update"], execution_options: None
+    sqltext: Union[str, "TextClause", "Update"], execution_options: None = None
 ) -> Optional["Table"]:
     """Execute the given SQL using the current migration context.
 
@@ -1078,7 +1090,7 @@ def implementation_for(op_cls: Any) -> Callable:
     """
 
 def inline_literal(
-    value: Union[str, int], type_: None
+    value: Union[str, int], type_: None = None
 ) -> "_literal_bindparam":
     """Produce an 'inline literal' expression, suitable for
     using in an INSERT, UPDATE, or DELETE statement.
@@ -1128,7 +1140,9 @@ def invoke(operation: "MigrateOperation") -> Any:
 
     """
 
-def register_operation(name: str, sourcename: Optional[str]) -> Callable:
+def register_operation(
+    name: str, sourcename: Optional[str] = None
+) -> Callable:
     """Register a new operation for this class.
 
     This method is normally used to add new operations
@@ -1146,7 +1160,7 @@ def register_operation(name: str, sourcename: Optional[str]) -> Callable:
     """
 
 def rename_table(
-    old_table_name: str, new_table_name: str, schema: Optional[str]
+    old_table_name: str, new_table_name: str, schema: Optional[str] = None
 ) -> Optional["Table"]:
     """Emit an ALTER TABLE to rename a table.
 
diff --git a/docs/build/unreleased/895.rst b/docs/build/unreleased/895.rst
new file mode 100644 (file)
index 0000000..a24d5d5
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, pep484
+    :tickets: 895
+
+    Fixed issue in generated .pyi files where default values for ``Optional``
+    arguments were missing, thereby causing mypy to consider them as required.
+
index 054d27f6118514bde9a32de5d376f88ac0938e69..61c3052fd682e3658d73272023fd7662ec4b65dc 100644 (file)
@@ -121,9 +121,7 @@ def _generate_stub_for_meth(cls, name, printer):
         retval = re.sub("NoneType", "None", retval)
         return retval
 
-    argspec = inspect_formatargspec(
-        *spec, formatvalue=lambda value: "", formatannotation=_formatannotation
-    )
+    argspec = inspect_formatargspec(*spec, formatannotation=_formatannotation)
 
     func_text = textwrap.dedent(
         """\