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: Instance of 'sessionmaker' has no 'begin' member #7090

Closed
neilmunday opened this issue Jun 29, 2022 · 6 comments
Closed

SQLAlchemy: Instance of 'sessionmaker' has no 'begin' member #7090

neilmunday opened this issue Jun 29, 2022 · 6 comments
Labels
Duplicate 🐫 Duplicate of an already existing issue False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)

Comments

@neilmunday
Copy link

Bug description

When using SQL Alchemy's sessionmaker.begin functionailty (ref: https://docs.sqlalchemy.org/en/14/orm/session_api.html#sqlalchemy.orm.sessionmaker.begin), pylint outputs the following errors:

E1101: Instance of 'sessionmaker' has no 'begin' member (no-member)

Example:

from sqlalchemy.orm import sessionmaker

Session = sessionmaker(some_engine)

with Session.begin() as session:
    session.add(some_object)

Configuration

No response

Command used

pylint bin/populate-db.py

Pylint output

************* Module populate-db
bin/populate-db.py:120:9: E1101: Instance of 'sessionmaker' has no 'begin' member (no-member)
bin/populate-db.py:156:9: E1101: Instance of 'sessionmaker' has no 'begin' member (no-member)
bin/populate-db.py:210:9: E1101: Instance of 'sessionmaker' has no 'begin' member (no-member)

Expected behavior

pylint should not output errors for sqlalchemy.orm.sessionmaker.begin.

Pylint version

pylint 2.14.3
astroid 2.11.6
Python 3.9.12 (main, Mar 25 2022, 00:00:00)
[GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]

OS / Environment

Fedora 34, BASH shell.

Additional dependencies

No response

@neilmunday neilmunday added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 29, 2022
@jacobtylerwalls
Copy link
Member

We have several open issues for SQLAlchemy, which points to the need for a third party project for this inspired by pylint-django etc.

Here, we just don't know that sessionmaker() is a class factory and returns instances of other classes. Poor pylint just knows it to be:
<Instance of sqlalchemy.orm.session.sessionmaker at 0x4576244016>, which definitely doesn't have begin.

As a workaround, you can invoke pylint with --ignored-classes='sqlalchemy.orm.session.sessionmaker'.

@jacobtylerwalls jacobtylerwalls added Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 29, 2022
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library labels Jun 30, 2022
@neilmunday
Copy link
Author

@jacobtylerwalls thank you for the explanation and for the info on how to workaround the issue.

@CarliJoy
Copy link
Contributor

Same is true for

from sqlalchemy import func

print(func.count(1))

Leads to

testing.py:3:6: E1102: func.count is not callable (not-callable)

So here again astroid/pylint isn't able to determine the correct stuff.

@scribblemaniac
Copy link

Poor pylint just knows it to be:
<Instance of sqlalchemy.orm.session.sessionmaker at 0x4576244016>, which definitely doesn't have begin.

@jacobtylerwalls Either I'm missing something, or that's exactly what this is: https://github.com/sqlalchemy/sqlalchemy/blob/ca813b33f5447e592eace3117f3521a007f30b60/lib/sqlalchemy/orm/session.py#L5057

@jenstroeger
Copy link

jenstroeger commented Feb 19, 2024

@CarliJoy for “normal” SQL functions — have you tried

>>> import sqlalchemy.sql.functions
>>> sqlalchemy.sql.functions.count
<class 'sqlalchemy.sql.functions.count'>

instead of the func function generator?

@neilmunday @jacobtylerwalls oddly enough, this one lints just fine:

with Session() as session:
    with session.begin():
        ...

For more details on the difference between Session.begin() and Session().begin() see the sessionmaker() docs.

@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Feb 19, 2024

Thanks for the workaround!

Based on the helpful link from @scribblemaniac, I think @overload is to blame. So this is a duplicate of pylint-dev/astroid#1015 / #5712.

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2024
@jacobtylerwalls jacobtylerwalls added the Duplicate 🐫 Duplicate of an already existing issue label Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)
Projects
None yet
Development

No branches or pull requests

6 participants