From: Jordan Macdonald Date: Wed, 16 Aug 2023 15:00:59 +0000 (-0400) Subject: Fix type hint of `key` on `Visitable.__class_getitem__()` X-Git-Tag: rel_2_0_21~32^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df126fe0d1799367572af6410b76850eebcd1100;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix type hint of `key` on `Visitable.__class_getitem__()` Fixed the type annotation for ``__class_getitem__()`` as applied to the ``Visitable`` class at the base of expression constructs to accept ``Any`` for a key, rather than ``str``, which helps with some IDEs such as PyCharm when attempting to write typing annotations for SQL constructs which include generic selectors. Pull request courtesy Jordan Macdonald. Fixes: #9878 Closes: #9881 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/9881 Pull-request-sha: 5aefaaf1065d1f946137a8b2f5be0767856cf9b0 Change-Id: I956e6d41142c50d870a142c3e99567a7ed2f7114 --- diff --git a/doc/build/changelog/unreleased_20/9878.rst b/doc/build/changelog/unreleased_20/9878.rst new file mode 100644 index 0000000000..0b6901ec32 --- /dev/null +++ b/doc/build/changelog/unreleased_20/9878.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: bug, typing + :tickets: 9878 + + Fixed the type annotation for ``__class_getitem__()`` as applied to the + ``Visitable`` class at the base of expression constructs to accept ``Any`` + for a key, rather than ``str``, which helps with some IDEs such as PyCharm + when attempting to write typing annotations for SQL constructs which + include generic selectors. Pull request courtesy Jordan Macdonald. + diff --git a/lib/sqlalchemy/orm/decl_api.py b/lib/sqlalchemy/orm/decl_api.py index e6b67b326c..1b378f3c8d 100644 --- a/lib/sqlalchemy/orm/decl_api.py +++ b/lib/sqlalchemy/orm/decl_api.py @@ -1556,7 +1556,7 @@ class registry: if hasattr(cls, "__class_getitem__"): - def __class_getitem__(cls: Type[_T], key: str) -> Type[_T]: + def __class_getitem__(cls: Type[_T], key: Any) -> Type[_T]: # allow generic classes in py3.9+ return cls diff --git a/lib/sqlalchemy/sql/visitors.py b/lib/sqlalchemy/sql/visitors.py index 69dc6a827c..8020181a29 100644 --- a/lib/sqlalchemy/sql/visitors.py +++ b/lib/sqlalchemy/sql/visitors.py @@ -146,7 +146,7 @@ class Visitable: cls._original_compiler_dispatch ) = _compiler_dispatch - def __class_getitem__(cls, key: str) -> Any: + def __class_getitem__(cls, key: Any) -> Any: # allow generic classes in py3.9+ return cls