Skip to content

BUG: Type error supplying SQLAlchemy NVARCHAR length in to_sql() #61660

Open
@philipnye

Description

@philipnye

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import os
import uuid

import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.types import NVARCHAR
from sqlalchemy.dialects.mssql import UNIQUEIDENTIFIER
import urllib.parse

connection = create_engine(
    f"mssql+pyodbc:///?odbc_connect={
        urllib.parse.quote_plus(
            'DRIVER=' + os.environ['ODBC_DRIVER'] + ';'
            'SERVER=' + os.environ['ODBC_SERVER'] + ';'
            'DATABASE=' + os.environ['ODBC_DATABASE'] + ';'
            'UID=' + os.environ['AZURE_CLIENT_ID'] + ';'
            'PWD=' + os.environ['AZURE_CLIENT_SECRET'] + ';'
            'Authentication=' + os.environ['ODBC_AUTHENTICATION']
        )
    }"
)

df = pd.DataFrame(
    columns=['id', 'name'],
    data=[
        [str(uuid.uuid4()), 'hello'],
        [str(uuid.uuid4()), 'world'],
        [str(uuid.uuid4()), 'foo'],
        [str(uuid.uuid4()), 'bar'],
        [str(uuid.uuid4()), 'baz'],
    ]
)

df.to_sql(
    'test',
    con=connection,
    dtype={
        'id': UNIQUEIDENTIFIER,
        'name': NVARCHAR(length=1024),
    },
    index=False,
)

Issue Description

This raises Argument of type "dict[str, type[UNIQUEIDENTIFIER[_UUID_RETURN@UNIQUEIDENTIFIER]] | NVARCHAR]" cannot be assigned to parameter "dtype" of type "DtypeArg | None" in function "to_sql". when using a type checker (Pylance/Pyright).

Expected Behavior

No type error is raised

Installed Versions

INSTALLED VERSIONS

commit : 2cc3762
python : 3.13.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.26100
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 2.3.0
numpy : 2.1.3
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 25.0
Cython : None
sphinx : None
IPython : 8.30.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : None
matplotlib : 3.10.0
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 18.1.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : 2.0.36
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team memberTypingtype annotations, mypy/pyright type checking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions