Skip to content

Commit

Permalink
OdbcHook returns None. Related to #15016 issue. (#15510)
Browse files Browse the repository at this point in the history
* OdbcHook returns None. Related to #15016 issue.

This PR is related to #15016 issue.
OdbcHook returns None for non-boolean-like string values in connect_kwargs dict arg, however connect_kwarg values should remain as is in this case.
According to the discussion on #15016, we agreed to remove the clean_bool function, as it is not needed actually for processing boolean values in JSON .
  • Loading branch information
Goodkat authored Jun 13, 2021
1 parent 9cd7930 commit 8a4cfd7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
14 changes: 1 addition & 13 deletions airflow/providers/odbc/hooks/odbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,8 @@ def connect_kwargs(self) -> dict:
Hook ``connect_kwargs`` precedes ``connect_kwargs`` from conn extra.
String values for 'true' and 'false' are converted to bool type.
If ``attrs_before`` provided, keys and values are converted to int, as required by pyodbc.
"""

def clean_bool(val): # pylint: disable=inconsistent-return-statements
if hasattr(val, 'lower'):
if val.lower() == 'true':
return True
elif val.lower() == 'false':
return False
else:
return val

conn_connect_kwargs = self.connection_extra_lower.get('connect_kwargs', {})
hook_connect_kwargs = self._connect_kwargs or {}
merged_connect_kwargs = merge_dicts(conn_connect_kwargs, hook_connect_kwargs)
Expand All @@ -183,7 +171,7 @@ def clean_bool(val): # pylint: disable=inconsistent-return-statements
int(k): int(v) for k, v in merged_connect_kwargs['attrs_before'].items()
}

return {k: clean_bool(v) for k, v in merged_connect_kwargs.items()}
return merged_connect_kwargs

def get_conn(self) -> pyodbc.Connection:
"""Returns a pyodbc connection object."""
Expand Down
4 changes: 2 additions & 2 deletions docs/apache-airflow-providers-odbc/connections/odbc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ Extra (optional)
"ApplicationIntent": "ReadOnly",
"TrustedConnection": "Yes",
"connect_kwargs": {
"autocommit": "false",
"ansi": "true"
"autocommit": false,
"ansi": true
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/odbc/hooks/test_odbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_connect_kwargs_bool_from_uri(self):
"""
Bools will be parsed from uri as strings
"""
conn_extra = json.dumps(dict(connect_kwargs={'ansi': 'true'}))
conn_extra = json.dumps(dict(connect_kwargs={'ansi': True}))
hook = self.get_hook(conn_params=dict(extra=conn_extra))
assert hook.connect_kwargs == {
'ansi': True,
Expand Down

0 comments on commit 8a4cfd7

Please sign in to comment.