From 78833af4e650d37e6257cfbb541e4db56e2a285f Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Sun, 27 Nov 2022 18:11:34 +0100 Subject: update for mypy 1.0 dev As I need dmypy to work without facing [1], I am running the latest build of mypy which seems so far to finally not have that issue. update constructs that latest mypy is being more picky about, including better typing for the _NONE_NAME symbol used in constraints (porting those elements from the Enum patch at I15ac3daee770408b5795746f47c1bbd931b7d26d) [1] https://github.com/python/mypy/issues/12744 Change-Id: Ib3f56787fa65ea9bb2e6a0bccc4d99f54c516dad --- lib/sqlalchemy/sql/schema.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'lib/sqlalchemy/sql/schema.py') diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index f1caf79be..2d04b28a8 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -58,6 +58,7 @@ from . import ddl from . import roles from . import type_api from . import visitors +from .base import _NoneName from .base import DedupeColumnCollection from .base import DialectKWArgs from .base import Executable @@ -111,6 +112,8 @@ _TAB = TypeVar("_TAB", bound="Table") _CreateDropBind = Union["Engine", "Connection", "MockConnection"] +_ConstraintNameArgument = Optional[Union[str, _NoneName]] + class SchemaConst(Enum): @@ -1927,11 +1930,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): self._proxies = _proxies else: # otherwise, add DDL-related events - if isinstance(self.type, SchemaEventTarget): - self.type._set_parent_with_dispatch(self) - for impl in self.type._variant_mapping.values(): - if isinstance(impl, SchemaEventTarget): - impl._set_parent_with_dispatch(self) + self._set_type(self.type) if default is not None: if not isinstance(default, (ColumnDefault, Sequence)): @@ -2023,6 +2022,14 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): identity: Optional[Identity] + def _set_type(self, type_: TypeEngine[Any]) -> None: + self.type = type_ + if isinstance(self.type, SchemaEventTarget): + self.type._set_parent_with_dispatch(self) + for impl in self.type._variant_mapping.values(): + if isinstance(impl, SchemaEventTarget): + impl._set_parent_with_dispatch(self) + @util.memoized_property def _gen_static_annotations_cache_key(self) -> bool: # type: ignore """special attribute used by cache key gen, if true, we will @@ -2480,7 +2487,7 @@ class ForeignKey(DialectKWArgs, SchemaItem): column: _DDLColumnArgument, _constraint: Optional[ForeignKeyConstraint] = None, use_alter: bool = False, - name: Optional[str] = None, + name: _ConstraintNameArgument = None, onupdate: Optional[str] = None, ondelete: Optional[str] = None, deferrable: Optional[bool] = None, @@ -3744,7 +3751,7 @@ class Constraint(DialectKWArgs, HasConditionalDDL, SchemaItem): def __init__( self, - name: Optional[str] = None, + name: _ConstraintNameArgument = None, deferrable: Optional[bool] = None, initially: Optional[str] = None, info: Optional[_InfoType] = None, @@ -3987,7 +3994,7 @@ class ColumnCollectionConstraint(ColumnCollectionMixin, Constraint): def __init__( self, *columns: _DDLColumnArgument, - name: Optional[str] = None, + name: _ConstraintNameArgument = None, deferrable: Optional[bool] = None, initially: Optional[str] = None, info: Optional[_InfoType] = None, @@ -4123,7 +4130,7 @@ class CheckConstraint(ColumnCollectionConstraint): def __init__( self, sqltext: _TextCoercedExpressionArgument[Any], - name: Optional[str] = None, + name: _ConstraintNameArgument = None, deferrable: Optional[bool] = None, initially: Optional[str] = None, table: Optional[Table] = None, @@ -4238,7 +4245,7 @@ class ForeignKeyConstraint(ColumnCollectionConstraint): self, columns: _typing_Sequence[_DDLColumnArgument], refcolumns: _typing_Sequence[_DDLColumnArgument], - name: Optional[str] = None, + name: _ConstraintNameArgument = None, onupdate: Optional[str] = None, ondelete: Optional[str] = None, deferrable: Optional[bool] = None, -- cgit v1.2.1