Skip to content
Advertisement

Bound metadata RemovedIn20Warning in debug mode

I use SQLAlchemy 1.4.0beta1 and enabled future flag for both the engine and the Session. Normally I don’t receive warnings. But in debug mode I receive warnings on 2.0 style select statements.

My models.py:

from sqlalchemy.orm import declarative_base
Base = declarative_base()

class Source(Base):
    __tablename__ = "server"
    id = Column("id", Integer, primary_key=True)
    name = Column("server", String(255))
    host = Column("host", String(255))
    port = Column("port", Integer)
    username = Column("login", String(255))
    password = Column("password", String(255))
    database = Column("db", String(255))

Code with warning:

from sqlalchemy import select
from sqlalchemy.orm import Session

with Session(engine, future=True) as session:
    stmt = select(Source).where(Source.name == source__name)
    source = session.execute(stmt).scalar()

Warning itself:

/virtualenv/lib/python3.8/site-packages/sqlalchemy/sql/elements.py:489: RemovedIn20Warning: The Executable.bind attribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: http://sqlalche.me/e/b8d9)
  elif self.bind:

Why is there any warning if I don’t bind any MetaData anywhere? And I also cannot reach breakpoint at the mentioned line of file when I receive this warning in the debug mode.

Complete example

import toml
from sqlalchemy import Column, Integer, String, select
from sqlalchemy.engine import create_engine, URL
from sqlalchemy.orm import declarative_base, Session

Base = declarative_base()

with open("config.toml") as stream:
    config = toml.load(stream)
config = config["Database"]

engine_url = URL.create(
    drivername="mysql+pymysql",
    host=config["host"],
    port=config["port"],
    username=config["username"],
    password=config["password"],
    database=config["database"],
    query={"charset": "utf8"},
)

engine = create_engine(
    engine_url,
    pool_recycle=3600,
    pool_pre_ping=True,
    encoding="utf-8",
    future=True,
)


class Source(Base):
    __tablename__ = "server"
    id = Column("id", Integer, primary_key=True)
    name = Column("server", String(255))
    host = Column("host", String(255))
    port = Column("port", Integer)
    username = Column("login", String(255))
    password = Column("password", String(255))
    database = Column("db", String(255))


with Session(engine, future=True) as session:
    stmt = select(Source).where(Source.name == "test")
    source = session.execute(stmt).scalar()

I put breakpoint at line with stmt and warning appears after going at line with source.

Advertisement

Answer

PyCharm evaluates all attributes of local objects. Deprecated bind happens to be one of them.

>>> stmt.bind
Traceback (most recent call last):
  File "/pycharm/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "<string>", line 2, in bind
  File "/virtualenvs/repeater-dIy575E2-py3.8/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 368, in warned
    _warn_with_version(message, version, wtype, stacklevel=3)
  File "/virtualenvs/repeater-dIy575E2-py3.8/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 41, in _warn_with_version
    warnings.warn(warn, stacklevel=stacklevel + 1)
sqlalchemy.exc.RemovedIn20Warning: The Executable.bind attribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: http://sqlalche.me/e/b8d9)
Advertisement