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:
JavaScript
x
13
13
1
from sqlalchemy.orm import declarative_base
2
Base = declarative_base()
3
4
class Source(Base):
5
__tablename__ = "server"
6
id = Column("id", Integer, primary_key=True)
7
name = Column("server", String(255))
8
host = Column("host", String(255))
9
port = Column("port", Integer)
10
username = Column("login", String(255))
11
password = Column("password", String(255))
12
database = Column("db", String(255))
13
Code with warning:
JavaScript
1
7
1
from sqlalchemy import select
2
from sqlalchemy.orm import Session
3
4
with Session(engine, future=True) as session:
5
stmt = select(Source).where(Source.name == source__name)
6
source = session.execute(stmt).scalar()
7
Warning itself:
JavaScript
1
3
1
/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)
2
elif self.bind:
3
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
JavaScript
1
45
45
1
import toml
2
from sqlalchemy import Column, Integer, String, select
3
from sqlalchemy.engine import create_engine, URL
4
from sqlalchemy.orm import declarative_base, Session
5
6
Base = declarative_base()
7
8
with open("config.toml") as stream:
9
config = toml.load(stream)
10
config = config["Database"]
11
12
engine_url = URL.create(
13
drivername="mysql+pymysql",
14
host=config["host"],
15
port=config["port"],
16
username=config["username"],
17
password=config["password"],
18
database=config["database"],
19
query={"charset": "utf8"},
20
)
21
22
engine = create_engine(
23
engine_url,
24
pool_recycle=3600,
25
pool_pre_ping=True,
26
encoding="utf-8",
27
future=True,
28
)
29
30
31
class Source(Base):
32
__tablename__ = "server"
33
id = Column("id", Integer, primary_key=True)
34
name = Column("server", String(255))
35
host = Column("host", String(255))
36
port = Column("port", Integer)
37
username = Column("login", String(255))
38
password = Column("password", String(255))
39
database = Column("db", String(255))
40
41
42
with Session(engine, future=True) as session:
43
stmt = select(Source).where(Source.name == "test")
44
source = session.execute(stmt).scalar()
45
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.
JavaScript
1
12
12
1
>>> stmt.bind
2
Traceback (most recent call last):
3
File "/pycharm/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
4
exec(exp, global_vars, local_vars)
5
File "<input>", line 1, in <module>
6
File "<string>", line 2, in bind
7
File "/virtualenvs/repeater-dIy575E2-py3.8/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 368, in warned
8
_warn_with_version(message, version, wtype, stacklevel=3)
9
File "/virtualenvs/repeater-dIy575E2-py3.8/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 41, in _warn_with_version
10
warnings.warn(warn, stacklevel=stacklevel + 1)
11
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)
12