From: Federico Caselli Date: Sun, 27 Nov 2022 17:11:34 +0000 (+0100) Subject: update for mypy 1.0 dev X-Git-Tag: rel_2_0_0b4~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78833af4e650d37e6257cfbb541e4db56e2a285f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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 --- diff --git a/lib/sqlalchemy/orm/instrumentation.py b/lib/sqlalchemy/orm/instrumentation.py index dfe09fcbd3..85fd957929 100644 --- a/lib/sqlalchemy/orm/instrumentation.py +++ b/lib/sqlalchemy/orm/instrumentation.py @@ -621,7 +621,7 @@ class InstrumentationFactory(EventTarget): if factory is None: factory = ClassManager - manager = factory(class_) + manager = ClassManager(class_) else: assert manager is not None diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index 58407a74d4..c0fc11d233 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -1365,7 +1365,7 @@ def _inspect_mc( return mapper -GenericAlias = type(List[_T]) +GenericAlias = type(List[Any]) @inspection._inspects(GenericAlias) diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index c818911696..fc80334e87 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -103,6 +103,14 @@ class _NoArg(Enum): NO_ARG = _NoArg.NO_ARG + +class _NoneName(Enum): + NONE_NAME = 0 + """indicate a 'deferred' name that was ultimately the value None.""" + + +_NONE_NAME = _NoneName.NONE_NAME + _T = TypeVar("_T", bound=Any) _Fn = TypeVar("_Fn", bound=Callable[..., Any]) diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 9e4422fbd0..50cf9b477c 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -67,6 +67,7 @@ from . import util as sql_util from ._typing import is_column_element from ._typing import is_dml from .base import _from_objects +from .base import _NONE_NAME from .base import Executable from .base import NO_ARG from .elements import ClauseElement @@ -6440,7 +6441,7 @@ class IdentifierPreparer: def format_constraint(self, constraint, _alembic_quote=True): naming = util.preloaded.sql_naming - if constraint.name is elements._NONE_NAME: + if constraint.name is _NONE_NAME: name = naming._constraint_name_for_table( constraint, constraint.table ) diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 6a9bd74caa..eff8c9bc11 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -5112,9 +5112,6 @@ class conv(_truncated_label): __slots__ = () -_NONE_NAME = util.symbol("NONE_NAME") -"""indicate a 'deferred' name that was ultimately the value None.""" - # for backwards compatibility in case # someone is re-implementing the # _truncated_identifier() sequence in a custom diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 2498bfb377..a3fed1d568 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -136,6 +136,7 @@ from .selectable import ( from .selectable import Lateral as Lateral from .selectable import ReturnsRows as ReturnsRows from .selectable import ScalarSelect as ScalarSelect +from .selectable import ScalarValues as ScalarValues from .selectable import Select as Select from .selectable import Selectable as Selectable from .selectable import SelectBase as SelectBase diff --git a/lib/sqlalchemy/sql/naming.py b/lib/sqlalchemy/sql/naming.py index eaa5d8dd3b..0fe1490018 100644 --- a/lib/sqlalchemy/sql/naming.py +++ b/lib/sqlalchemy/sql/naming.py @@ -16,7 +16,7 @@ from __future__ import annotations import re from . import events # noqa -from .elements import _NONE_NAME +from .base import _NONE_NAME from .elements import conv as conv from .schema import CheckConstraint from .schema import Column diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index f1caf79be8..2d04b28a87 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, diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 624b7d16ef..a0f56d8391 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -37,10 +37,10 @@ from . import elements from . import operators from . import roles from . import type_api +from .base import _NONE_NAME from .base import NO_ARG from .base import SchemaEventTarget from .cache_key import HasCacheKey -from .elements import _NONE_NAME from .elements import quoted_name from .elements import Slice from .elements import TypeCoerce as type_coerce # noqa diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index af8c15f98f..e50aa236a9 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -44,7 +44,7 @@ from sqlalchemy.schema import DefaultClause from sqlalchemy.schema import DropIndex from sqlalchemy.sql import naming from sqlalchemy.sql import operators -from sqlalchemy.sql.elements import _NONE_NAME +from sqlalchemy.sql.base import _NONE_NAME from sqlalchemy.sql.elements import literal_column from sqlalchemy.sql.schema import RETAIN_SCHEMA from sqlalchemy.testing import assert_raises