--- /dev/null
+.. change::
+ :tags: bug, typing
+ :tickets: 10264, 9284
+
+ Fixed regression introduced in 2.0.20 via :ticket:`9600` fix which
+ attempted to add more formal typing to
+ :paramref:`_schema.MetaData.naming_convention`. This change prevented basic
+ naming convention dictionaries from passing typing and has been adjusted so
+ that a plain dictionary of strings for keys as well as dictionaries that
+ use constraint types as keys or a mix of both, are again accepted.
+
+ As part of this change, lesser used forms of the naming convention
+ dictionary are also typed, including that it currently allows for
+ ``Constraint`` type objects as keys as well.
from typing import Sequence as _typing_Sequence
from typing import Set
from typing import Tuple
-from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
)
-_AllConstraints = Union[
- Index,
- UniqueConstraint,
- CheckConstraint,
- ForeignKeyConstraint,
- PrimaryKeyConstraint,
-]
-
-_NamingSchemaCallable = Callable[[_AllConstraints, Table], str]
+_NamingSchemaCallable = Callable[[Constraint, Table], str]
+_NamingSchemaDirective = Union[str, _NamingSchemaCallable]
class _NamingSchemaTD(TypedDict, total=False):
- fk: Union[str, _NamingSchemaCallable]
- pk: Union[str, _NamingSchemaCallable]
- ix: Union[str, _NamingSchemaCallable]
- ck: Union[str, _NamingSchemaCallable]
- uq: Union[str, _NamingSchemaCallable]
+ fk: _NamingSchemaDirective
+ pk: _NamingSchemaDirective
+ ix: _NamingSchemaDirective
+ ck: _NamingSchemaDirective
+ uq: _NamingSchemaDirective
_NamingSchemaParameter = Union[
+ # it seems like the TypedDict here is useful for pylance typeahead,
+ # and not much else
_NamingSchemaTD,
- Mapping[
- Union[Type[_AllConstraints], str], Union[str, _NamingSchemaCallable]
- ],
+ # there is no form that allows Union[Type[Any], str] to work in all
+ # cases, including breaking out Mapping[] entries for each combination
+ # even, therefore keys must be `Any` (see #10264)
+ Mapping[Any, _NamingSchemaDirective],
]
"CONSTRAINT %s PRIMARY KEY (x, y))" % expected_name,
)
+ def test_constraint_classes_for_keys(self):
+ u1 = self._fixture(
+ naming_convention={
+ UniqueConstraint: "uq_%(table_name)s_%(column_0_name)s"
+ }
+ )
+ uq = UniqueConstraint(u1.c.data)
+ eq_(uq.name, "uq_user_data")
+
def test_uq_name(self):
u1 = self._fixture(
naming_convention={"uq": "uq_%(table_name)s_%(column_0_name)s"}
from typing import Union
+from sqlalchemy import CheckConstraint
from sqlalchemy import Constraint
from sqlalchemy import Index
from sqlalchemy import MetaData
"foo": lambda c, t: t.name + str(c.name),
}
)
+
+NAMING_CONVENTIONS_ONLY_CALLABLE = {
+ "fk_guid": fk_guid,
+ "foo": lambda c, t: t.name + str(c.name),
+}
+
+MetaData(naming_convention=NAMING_CONVENTIONS_ONLY_CALLABLE)
+
+NAMING_CONVENTIONS_TYPES_FOR_KEYS_ONLY = {
+ CheckConstraint: "%(table_name)s_%(constraint_name)s_ck",
+ Index: "%(column_0_label)s_ix",
+}
+
+MetaData(naming_convention=NAMING_CONVENTIONS_TYPES_FOR_KEYS_ONLY)
+
+NAMING_CONVENTIONS_TYPES_AND_STR_FOR_KEYS = {
+ CheckConstraint: "%(table_name)s_%(constraint_name)s_ck",
+ Index: "%(column_0_label)s_ix",
+ "custom": "custom",
+ "fk": "fk_name",
+}
+
+MetaData(naming_convention=NAMING_CONVENTIONS_TYPES_AND_STR_FOR_KEYS)
+
+
+NAMING_CONVENTIONS_STR = {
+ "ix": "%(column_0_label)s_ix",
+ "uq": "%(table_name)s_%(column_0_name)s_uq",
+ "ck": "%(table_name)s_%(constraint_name)s_ck",
+ "fk": "%(table_name)s_%(column_0_name)s_%(referred_table_name)s_fk",
+ "pk": "%(table_name)s_pk",
+}
+
+MetaData(naming_convention=NAMING_CONVENTIONS_STR)