laughingman7743 / PyAthena

PyAthena is a Python DB API 2.0 (PEP 249) client for Amazon Athena.
MIT License
463 stars 105 forks source link

SyntaxError when importing AthenaDialect #472

Closed rajeee closed 1 year ago

rajeee commented 1 year ago

I get the following error when importing AthenaDialect in version 3.0.7

>>> from pyathena.sqlalchemy.base import AthenaDialect
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/radhikar/mambaforge/envs/largeee/lib/python3.11/site-packages/pyathena/sqlalchemy/base.py", line 907, in <module>
    class AthenaDialect(DefaultDialect):
  File "/Users/radhikar/mambaforge/envs/largeee/lib/python3.11/site-packages/pyathena/sqlalchemy/base.py", line 1105, in AthenaDialect
    @reflection.cache
     ^^^^^^^^^^^^^^^^
  File "/Users/radhikar/mambaforge/envs/largeee/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 198, in decorate
    decorated = _exec_code_in_env(code, env, fn.__name__)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/radhikar/mambaforge/envs/largeee/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 228, in _exec_code_in_env
    exec(code, env)
  File "<string>", line 1
    def get_view_definition(self, connection: 'Connection', view_name: 'str', schema: 'Optional[str]'=None, **kw) -> 'str):
                                                                                                                     ^
SyntaxError: unterminated string literal (detected at line 1)

Version 3.0.6 works fine.

laughingman7743 commented 1 year ago

Strange, all tests pass. https://github.com/laughingman7743/PyAthena/actions/runs/6242621017 What version of SQLAlchemy are you using?

laughingman7743 commented 1 year ago

https://github.com/laughingman7743/PyAthena/blob/v3.0.7/pyathena/sqlalchemy/base.py#L1108 It seems that an error is occurring with this type hint, so it may be possible to remove this type hint. I don't think the type hint is required.

Tobotimus commented 1 year ago

I'm having this issue with SQLAlchemy 1.4.49.

laughingman7743 commented 1 year ago

I have confirmed that the error occurs in SQLAlchemy 1.x. I will fix it.

Tobotimus commented 1 year ago

Awesome, thanks @laughingman7743!

FWIW I found this commit https://github.com/sqlalchemy/sqlalchemy/commit/91501e06a17d873902114275d7149ba24973db6a which alludes to the cause of this issue:

Additionally, to support annotations for methods that make use of @decorator, @Generative etc., modified format_argspec_plus to no longer return "args", instead returns "grouped_args" which is always grouped and allows return annotations to format correctly.

This seems to be relevant since AthenaDialect.get_view_definition() is decorated with @sqlalchemy.engine.reflection.cache.