-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
Type of "to_sql" is partially unknown #353
Comments
PRs welcome to improve missing types. |
https://github.com/pandas-dev/pandas-stubs/blob/main/pandas-stubs/io/sql.pyi#L103-L118 I assume it has to do with this part right? Maybe it's just this two lines for it to work?
|
use this version |
I did do that. I'm on Windows, so not sure if that makes a difference. What got installed was 1.4.41. Can you do a |
#!/usr/bin/env python3.10
from rich import print
import pandas as pd
from sqlalchemy import func, Column, DateTime, ForeignKey, Integer, String, create_engine
from sqlalchemy.future import select
from sqlalchemy.orm import declarative_base, relationship, selectinload
from sqlalchemy.dialects.postgresql import JSONB
engine = create_engine(
"postgresql+psycopg://gert:p@localhost/gert", echo=False, future=True
)
Base = declarative_base()
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
data = Column(String, doc="A data", comment="A data")
create_date = Column(DateTime, server_default=func.now())
bs = relationship("B")
__mapper_args__ = {"eager_defaults": True}
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(Integer, ForeignKey("a.id"))
data = Column(JSONB, nullable=True, doc="P data", comment="P data")
with engine.begin() as conn:
stmt = select(A).options(selectinload(A.bs))
df = pd.read_sql(stmt, conn)
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(df)
df.to_sql('test', conn, if_exists='replace', index = False) |
OK, I now know what is happening here. The message about being "partially unknown" is because So you have to do a |
Never mind. It has something to do with this declaration in the stubs: method: Literal["multi"]
| Callable[[SQLTable, Any, list[str], Iterable], int | None]
| None = ..., If PR welcome to change: pandas-stubs/pandas-stubs/core/generic.pyi Line 168 in cb56bfa
to use |
We could slowly work towards enabling pyright's reportUnknownParameterType/reportUnknownArgumentType. I think they would cover that. |
Is it supposed to be |
Not entirely clear from the documentation. Maybe |
Here's what appears to be the code: https://github.com/pandas-dev/pandas/blob/e0608cb42e98f165fd99b595b2c9d0bfeb208b8f/pandas/io/sql.py#L948
|
But this code makes it seem like it could be a |
I believe that code accepts
In any case I would need to put together an example to prove this, in the interim it certainly doesn't hurt much to have a slightly broader type |
Based on https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#io-sql-method, and that it uses |
Here is a code example: import pandas as pd
import sqlite3
def insert_method(pd_table, conn, keys, data_iter):
print("pd_table:", type(pd_table))
print("conn:", type(conn))
print("keys:", type(keys))
print("data_iter:", type(data_iter))
data = list(data_iter)
print("data_iter:", type(data[0]))
with sqlite3.connect(":memory:") as con:
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
df.to_sql("table", con=con, method=insert_method)
This confirms my thinking above that ultimately a
Probably not something an insertion method function should do (mutating values), but nevertheless... |
Thanks for the test. I misread the code. We should just use |
osx vscode strict mode
pip3.10 install -U 'psycopg[c]'
pip3.10 install -U --pre SQLAlchemy
pip3.10 install -U pandas
pip3.10 install -U pandas-stubs
Type of "to_sql" is partially unknown
The text was updated successfully, but these errors were encountered: