Skip to content
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

sqlalchemy.utils.langhelpers.TypingOnly too rigidly prevents special dunders from appearing #11334

Closed
edgarrmondragon opened this issue Apr 30, 2024 · 5 comments
Labels
blocker issue that must be resolved asap as it is preventing things from working bug Something isn't working
Milestone

Comments

@edgarrmondragon
Copy link
Contributor

edgarrmondragon commented Apr 30, 2024

Describe the bug

An exception is raised on Python 3.13:

AssertionError: Class <class 'sqlalchemy.sql.elements.SQLCoreOperations'> directly inherits TypingOnly but has additional attributes {'__static_attributes__'}.

Let me know if this is too premature, specially since greenlet (a current dependency, but I think it'll be dropped for 2.1?) does not currently build on Python 3.13. Otherwise I can submit a quick PR to fix this.

Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected

No response

SQLAlchemy Version in Use

main

DBAPI (i.e. the database driver)

pysqlite

Database Vendor and Major Version

SQLite

Python Version

3.13

Operating system

Linux

To Reproduce

from sqlalchemy.orm import mapped_column

Error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    from sqlalchemy.orm import mapped_column
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/__init__.py", line 13, in <module>
    from .engine import AdaptedConnection as AdaptedConnection
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/engine/__init__.py", line 18, in <module>
    from . import events as events
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/engine/events.py", line 19, in <module>
    from .base import Connection
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 30, in <module>
    from .interfaces import BindTyping
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/engine/interfaces.py", line 38, in <module>
    from ..sql.compiler import Compiled as Compiled
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/sql/__init__.py", line 14, in <module>
    from .compiler import COLLECT_CARTESIAN_PRODUCTS as COLLECT_CARTESIAN_PRODUCTS
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/sql/compiler.py", line 62, in <module>
    from . import crud
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/sql/crud.py", line 34, in <module>
    from . import dml
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/sql/dml.py", line 34, in <module>
    from . import util as sql_util
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/sql/util.py", line 46, in <module>
    from .ddl import sort_tables as sort_tables  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/sql/ddl.py", line 30, in <module>
    from .elements import ClauseElement
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/sql/elements.py", line 806, in <module>
    class SQLCoreOperations(Generic[_T_co], ColumnOperators, TypingOnly):
    ...<372 lines>...
            def __rfloordiv__(self, other: Any) -> ColumnElement[Any]: ...
  File "/Users/me/.pyenv/versions/3.13.0a6/lib/python3.13/typing.py", line 1085, in _generic_init_subclass
    super(Generic, cls).__init_subclass__(*args, **kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Mylib/.venv/lib/python3.13/site-packages/sqlalchemy/util/langhelpers.py", line 1981, in __init_subclass__
    raise AssertionError(
    ...<2 lines>...
    )
AssertionError: Class <class 'sqlalchemy.sql.elements.SQLCoreOperations'> directly inherits TypingOnly but has additional attributes {'__static_attributes__'}.

Additional context

@edgarrmondragon edgarrmondragon added the requires triage New issue that requires categorization label Apr 30, 2024
@edgarrmondragon edgarrmondragon changed the title Missing Python 3.13 built-in attribute in sqlalchemy.utils.langhelpers.TypingOnly Missing Python 3.13 built-in attribute __static_attributes__ in sqlalchemy.utils.langhelpers.TypingOnly Apr 30, 2024
@zzzeek zzzeek added bug Something isn't working blocker issue that must be resolved asap as it is preventing things from working and removed requires triage New issue that requires categorization labels Apr 30, 2024
@zzzeek zzzeek added this to the 2.0.x milestone Apr 30, 2024
@zzzeek zzzeek changed the title Missing Python 3.13 built-in attribute __static_attributes__ in sqlalchemy.utils.langhelpers.TypingOnly sqlalchemy.utils.langhelpers.TypingOnly too rigidly prevents special dunders from appearing Apr 30, 2024
@zzzeek
Copy link
Member

zzzeek commented Apr 30, 2024

I think TypingOnly should be changed as follows:

diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py
index fe3bd1684..87b3e486c 100644
--- a/lib/sqlalchemy/util/langhelpers.py
+++ b/lib/sqlalchemy/util/langhelpers.py
@@ -1967,13 +1967,7 @@ class TypingOnly:
     def __init_subclass__(cls) -> None:
         if TypingOnly in cls.__bases__:
             remaining = set(cls.__dict__).difference(
-                {
-                    "__module__",
-                    "__doc__",
-                    "__slots__",
-                    "__orig_bases__",
-                    "__annotations__",
-                }
+                {name for name in cls.__dict__ if re.match("^__.+__$", name)}
             )
             if remaining:
                 raise AssertionError(

I see you have a PR, let's do it like that. I dont think we should be hardcoding all the dunders here.

@edgarrmondragon
Copy link
Contributor Author

Gotcha, that's better!

@sqla-tester
Copy link
Collaborator

Edgar Ramírez-Mondragón has proposed a fix for this issue in the main branch:

Ignore all dunders when checking attributes in sqlalchemy.util.langhelpers.TypingOnly https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5287

1 similar comment
@sqla-tester
Copy link
Collaborator

Edgar Ramírez-Mondragón has proposed a fix for this issue in the main branch:

Ignore all dunders when checking attributes in sqlalchemy.util.langhelpers.TypingOnly https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5287

@sqla-tester
Copy link
Collaborator

Edgar Ramírez-Mondragón has proposed a fix for this issue in the rel_2_0 branch:

Ignore all dunders when checking attributes in sqlalchemy.util.langhelpers.TypingOnly https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5273

sqlalchemy-bot pushed a commit that referenced this issue May 1, 2024
…elpers.TypingOnly`

Fixed an internal class that was testing for unexpected attributes to work
correctly under upcoming Python 3.13.   Pull request courtesy Edgar
Ramírez-Mondragón.

Fixes: #11334
Closes: #11335
Pull-request: #11335
Pull-request-sha: babd703

Change-Id: Ia2e7392c9403e25266c7d30b987b577f49d008c0
(cherry picked from commit eb118e2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker issue that must be resolved asap as it is preventing things from working bug Something isn't working
Projects
None yet
3 participants