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

BUG: AttributeError: 'function' object has no attribute 'currentframe' #48733

Closed
3 tasks done
huka81 opened this issue Sep 23, 2022 · 8 comments · Fixed by #48736
Closed
3 tasks done

BUG: AttributeError: 'function' object has no attribute 'currentframe' #48733

huka81 opened this issue Sep 23, 2022 · 8 comments · Fixed by #48736
Labels
IO SQL to_sql, read_sql, read_sql_query Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@huka81
Copy link

huka81 commented Sep 23, 2022

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 pandas.io.sql
import sqlalchemy

dbEngine=sqlalchemy.create_engine(f'sqlite:///dummy.db')

db = pandas.io.sql.SQLDatabase(engine=dbEngine)
db.check_case_sensitive("TABLE1", "") # passing
print("check 1 for existing table passed")
db.check_case_sensitive("TABLE2", "") # failing
print("check 2 for non-existing table passed")

Issue Description

Fails on invoking SQLDatabase.check_case_sensitive with table name, which does not exists in database with error:

Traceback (most recent call last):
File "C:\Users\artur\PycharmProjects\Ex_Files_Python_EssT\Exercise Files\Chap02\pandas_debug.py", line 15, in
db.check_case_sensitive("TABLE2", "")
File "C:\Users\artur\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pandas\io\sql.py", line 1664, in check_case_sensitive
stacklevel=find_stack_level(inspect.currentframe()),
AttributeError: 'function' object has no attribute 'currentframe'

Expected Behavior

Same code was passing till version 1.4.4

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Polish_Poland.1250

pandas : 1.5.0
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : 1.4.41
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@huka81 huka81 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 23, 2022
@phofl
Copy link
Member

phofl commented Sep 23, 2022

Hi, thanks for your report. This is not considered public, so there are not guarantees that the class is stable from one release to another

cc @MarcoGorelli

commit e94faa23e24c0abf9db74d79cfebe06676577867
Author: Marco Edward Gorelli <marcogorelli@protonmail.com>
Date:   Wed Aug 17 23:00:08 2022 +0100

    WARN,TST check stacklevel for all warnings  (#47998)

@phofl phofl added IO SQL to_sql, read_sql, read_sql_query and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 23, 2022
@MarcoGorelli
Copy link
Member

Thanks for the report - this is indeed a bug as inspect there is the one imported from sqlachemy, rather than the builtin Python one

I think something like

--- a/pandas/io/sql.py
+++ b/pandas/io/sql.py
@@ -1645,10 +1645,10 @@ class SQLDatabase(PandasSQL):
         if not name.isdigit() and not name.islower():
             # check for potentially case sensitivity issues (GH7815)
             # Only check when name is not a number and name is not lower case
-            from sqlalchemy import inspect
+            sqlalchemy = import_optional_dependency("sqlalchemy", errors="ignore")
 
             with self.connectable.connect() as conn:
-                insp = inspect(conn)
+                insp = sqlalchemy.inspect(conn)

should fix it

@phofl
Copy link
Member

phofl commented Sep 23, 2022

Labelling as 1.5.1 then, if you think this is a minimal fix.

Any idea if this breaks something internally?

@phofl phofl added this to the 1.5.1 milestone Sep 23, 2022
@phofl phofl added Regression Functionality that used to work in a prior pandas version and removed Bug labels Sep 23, 2022
@MarcoGorelli
Copy link
Member

I think it should only cause issues in cases where the code would've thrown a warning

@jorisvandenbossche
Copy link
Member

I think it should only cause issues in cases where the code would've thrown a warning

Indeed, but so that's still a breaking change.

@jorisvandenbossche
Copy link
Member

@huka81 for context, are you actually using SQLDatabase directly? Or did you just use it in the above code snippet to provide a minimal reproducible example?
If you are using it in actual code, can you explain a bit how you use this / why the read_sql and to_sql functions are not sufficient in your case?

@huka81
Copy link
Author

huka81 commented Sep 26, 2022

Hi @jorisvandenbossche

I used SQLDatabase just to reproduce and demonstrate the issue, actual stack trace is:

2022-09-20T01:59:01.3560281Z _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
2022-09-20T01:59:01.3561146Z python/cdptest/test_data_load.py:88: in add_handcrafted_data
2022-09-20T01:59:01.3561859Z     df_to_write.to_sql(
2022-09-20T01:59:01.3562772Z /usr/local/lib/python3.10/dist-packages/pandas/core/generic.py:2986: in to_sql
2022-09-20T01:59:01.3563513Z     return sql.to_sql(
2022-09-20T01:59:01.3564347Z /usr/local/lib/python3.10/dist-packages/pandas/io/sql.py:696: in to_sql
2022-09-20T01:59:01.3565086Z     return pandas_sql.to_sql(
2022-09-20T01:59:01.3565950Z /usr/local/lib/python3.10/dist-packages/pandas/io/sql.py:1751: in to_sql
2022-09-20T01:59:01.3566768Z     self.check_case_sensitive(name=name, schema=schema)
2022-09-20T01:59:01.3567536Z _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
2022-09-20T01:59:01.3567890Z 
2022-09-20T01:59:01.3568529Z self = <pandas.io.sql.SQLDatabase object at 0x7f2acaed8580>
2022-09-20T01:59:01.3569365Z name = 'EDM_LOCATION', schema = 'RAW_RMS_EDM'
2022-09-20T01:59:01.3569719Z 
2022-09-20T01:59:01.3570276Z     def check_case_sensitive(
2022-09-20T01:59:01.3570863Z         self,
2022-09-20T01:59:01.3571381Z         name,
2022-09-20T01:59:01.3571908Z         schema,
2022-09-20T01:59:01.3572486Z     ) -> None:
2022-09-20T01:59:01.3573009Z         """
2022-09-20T01:59:01.3573729Z         Checks table name for issues with case-sensitivity.
2022-09-20T01:59:01.3574460Z         Method is called after data is inserted.
2022-09-20T01:59:01.3575056Z         """
2022-09-20T01:59:01.3575701Z         if not name.isdigit() and not name.islower():
2022-09-20T01:59:01.3576461Z             # check for potentially case sensitivity issues (GH7815)
2022-09-20T01:59:01.3577266Z             # Only check when name is not a number and name is not lower case
2022-09-20T01:59:01.3578102Z             from sqlalchemy import inspect
2022-09-20T01:59:01.3578836Z     
2022-09-20T01:59:01.3579454Z             with self.connectable.connect() as conn:
2022-09-20T01:59:01.3580126Z                 insp = inspect(conn)
2022-09-20T01:59:01.3581148Z                 table_names = insp.get_table_names(schema=schema or self.meta.schema)
2022-09-20T01:59:01.3581940Z             if name not in table_names:
2022-09-20T01:59:01.3582556Z                 msg = (
2022-09-20T01:59:01.3583376Z                     f"The provided table name '{name}' is not found exactly as "
2022-09-20T01:59:01.3584177Z                     "such in the database after writing the table, possibly "
2022-09-20T01:59:01.3584971Z                     "due to case sensitivity issues. Consider using lower "
2022-09-20T01:59:01.3585689Z                     "case table names."
2022-09-20T01:59:01.3586470Z                 )
2022-09-20T01:59:01.3587037Z                 warnings.warn(
2022-09-20T01:59:01.3587610Z                     msg,
2022-09-20T01:59:01.3588204Z                     UserWarning,
2022-09-20T01:59:01.3589035Z >                   stacklevel=find_stack_level(inspect.currentframe()),
2022-09-20T01:59:01.3590112Z                 )
2022-09-20T01:59:01.3591216Z E               AttributeError: 'function' object has no attribute 'currentframe'
2022-09-20T01:59:01.3591627Z 
2022-09-20T01:59:01.3592716Z /usr/local/lib/python3.10/dist-packages/pandas/io/sql.py:1662: AttributeError

and this is caused by NDFrame.to_sql() method

@jorisvandenbossche
Copy link
Member

@huka81 thanks, that good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants