-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Metadata naming_convention
can't be of type dict[str, str]
in v2.0.20
#10264
Comments
Hi, That seems a mypy bug, since the type allows for a |
@CaselIT This is not a mypy bug: |
strange that pyright likes the current type, while mypy does not. Usually pyright is the correct one on complex types |
actually it seems that it's also erroring |
I'm not sure how to proceed. The typing for mapping seem too constrained to allow the actual type of this attribute. _AllConstraints = Union[
Index,
UniqueConstraint,
CheckConstraint,
ForeignKeyConstraint,
PrimaryKeyConstraint,
]
_NamingSchemaCallable = Callable[[_AllConstraints, Table], str]
_NamingSchemaValue = Union[str, _NamingSchemaCallable]
class _NamingSchemaTD(TypedDict, total=False):
fk: _NamingSchemaValue
pk: _NamingSchemaValue
ix: _NamingSchemaValue
ck: _NamingSchemaValue
uq: _NamingSchemaValue
_NamingSchemaParameter = Union[
_NamingSchemaTD,
Mapping[str, _NamingSchemaValue],
Mapping[Type[_AllConstraints], _NamingSchemaValue],
Mapping[Union[Type[_AllConstraints], str], _NamingSchemaValue],
] but it's still reporting errors on from typing import Union
from sqlalchemy import Constraint
from sqlalchemy import Index
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import UniqueConstraint
MetaData(
naming_convention={
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s",
}
)
MetaData(
naming_convention={
Index: "ix_%(table_name)s",
"uq": "uq_%(table_name)s_%(column_0_N_name)s",
}
)
def fk_guid(constraint: Union[Constraint, Index], table: Table) -> str:
return "foo"
MetaData(
naming_convention={
"fk_guid": fk_guid,
"ix": "ix_%(column_0_label)s",
"fk": "fk_%(fk_guid)s",
"foo": lambda c, t: t.name + str(c.name),
}
)
naming_convention = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s",
}
MetaData(naming_convention=naming_convention)
naming_convention2 = {
Index: "ix_%(column_0_label)s",
UniqueConstraint: "uq_%(table_name)s_%(column_0_name)s",
}
MetaData(naming_convention=naming_convention2)
naming_convention3 = {
Index: "ix_%(column_0_label)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
}
MetaData(naming_convention=naming_convention3) mypy
while pyright reports
suggestions that are not |
seems so. Not sure if this is a full revert or not but we have to go with diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py
index 008ae2c005..8d97254490 100644
--- a/lib/sqlalchemy/sql/schema.py
+++ b/lib/sqlalchemy/sql/schema.py
@@ -5297,21 +5297,7 @@ _AllConstraints = Union[
_NamingSchemaCallable = Callable[[_AllConstraints, Table], str]
-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]
-
-
-_NamingSchemaParameter = Union[
- _NamingSchemaTD,
- Mapping[
- Union[Type[_AllConstraints], str], Union[str, _NamingSchemaCallable]
- ],
-]
-
+_NamingSchemaParameter = Mapping[Any, Union[str, _NamingSchemaCallable]]
DEFAULT_NAMING_CONVENTION: _NamingSchemaParameter = util.immutabledict(
{"ix": "ix_%(column_0_label)s"} # type: ignore[arg-type]
|
that's a shame. as always python typing are quite lacking... |
My workaround to silence the mypy error (until a proper fix is released): from sqlalchemy.sql.schema import _NamingSchemaTD
# use the convention that you have in your project
convention = _NamingSchemaTD(
ix="ix_%(table_name)s_%(column_0_N_name)s",
uq="uq_%(table_name)s_%(column_0_N_name)s",
ck="ck_%(table_name)s_%(constraint_name)s",
fk="fk_%(table_name)s_%(column_0_N_name)s_%(referred_table_name)s",
pk="pk_%(table_name)s",
)
metadata = sqlalchemy.MetaData(naming_convention=convention) |
Defining it inline also works correctly import sqlalchemy as sa
metadata = sa.MetaData(
naming_convention=dict(
ix="ix_%(table_name)s_%(column_0_N_name)s",
uq="uq_%(table_name)s_%(column_0_N_name)s",
ck="ck_%(table_name)s_%(constraint_name)s",
fk="fk_%(table_name)s_%(column_0_N_name)s_%(referred_table_name)s",
pk="pk_%(table_name)s",
)
) |
we didnt even note this in the changelog for 2.0.20 |
Mike Bayer has proposed a fix for this issue in the main branch: repair typing for the ordinary case of naming_convention w/ str https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4882 |
Ensure stubs packages are not installed
sqlalchemy-stubs
andsqlalchemy2-stubs
are not compatible with v2)Verify if the api is typed
Describe the typing issue
In SQLAlchemy 2.0.20, the type of ORM Metadata's naming_convention was updated from
Optional[dict[str, str]]
toMypy now will not accept a dict[str, str] as seen in doc examples, with the error below.
To Reproduce
Error
Versions
Additional context
I validated the offending code in 2.0.19, with the same mypy configuration and did not encounter an issue.
I tried modifying the types in the source code, and it is introduced by the
Type[_AllConstraints]
. Removing that portion of the type resolves the issue. I did not investigate further, and this might be a mypy bug.If relevant, here is the mypy configuration from my pyproject.toml:
The text was updated successfully, but these errors were encountered: