From: Mike Bayer Date: Mon, 30 Aug 2021 18:30:37 +0000 (-0400) Subject: restore default values for keyword arguments in stubs X-Git-Tag: rel_1_7_1~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff8fd160ad007cd00353a138457275474a122ff2;p=thirdparty%2Fsqlalchemy%2Falembic.git restore default values for keyword arguments in stubs 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 --- diff --git a/alembic/context.pyi b/alembic/context.pyi index 8a33d520..d789a224 100644 --- a/alembic/context.pyi +++ b/alembic/context.pyi @@ -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 diff --git a/alembic/op.pyi b/alembic/op.pyi index 76912dc8..f5aca925 100644 --- a/alembic/op.pyi +++ b/alembic/op.pyi @@ -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 index 00000000..a24d5d5c --- /dev/null +++ b/docs/build/unreleased/895.rst @@ -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. + diff --git a/tools/write_pyi.py b/tools/write_pyi.py index 054d27f6..61c3052f 100644 --- a/tools/write_pyi.py +++ b/tools/write_pyi.py @@ -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( """\