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

Add type hints and enable mypy for client.py and dbapi.py #324

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

damian3031
Copy link
Member

part of #292

Added type hints and enabled mypy checks for client.py, dbapi.py

@cla-bot cla-bot bot added the cla-signed label Jan 18, 2023
@damian3031 damian3031 requested review from mdesmet, hovaesco and hashhar and removed request for mdesmet January 18, 2023 13:26
trino/dbapi.py Outdated Show resolved Hide resolved
trino/dbapi.py Outdated Show resolved Hide resolved
trino/dbapi.py Outdated Show resolved Hide resolved
trino/dbapi.py Outdated Show resolved Hide resolved
trino/dbapi.py Outdated Show resolved Hide resolved
@@ -584,66 +603,22 @@ def describe(self, sql: str) -> List[DescribeOutput]:

return list(map(lambda x: DescribeOutput.from_row(x), result))

def genall(self):
return self._query.result
def genall(self) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def genall(self) -> Any:
def genall(self) -> Optional[trino.client.TrinoResult]:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy throws error then:
trino/dbapi.py:615: error: Argument 1 to "list" has incompatible type "Optional[TrinoResult]"; expected "Iterable[List[Any]]" [arg-type]

changing to Iterable[List[Any]] throws error elsewhere, and so on

trino/exceptions.py Outdated Show resolved Hide resolved
trino/client.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mdesmet mdesmet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % the Iterator comment

trino/dbapi.py Outdated Show resolved Hide resolved
@hashhar hashhar changed the title Hovaesco/mypy2 Add type hints and enable mypy for client.py and dbapi.py Jan 19, 2023
Copy link
Member

@hashhar hashhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first quick pass

@@ -75,7 +87,7 @@
else:
PROXIES = {}

_HEADER_EXTRA_CREDENTIAL_KEY_REGEX = re.compile(r'^\S[^\s=]*$')
_HEADER_EXTRA_CREDENTIAL_KEY_REGEX = re.compile(r"^\S[^\s=]*$")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate out a commit, having functional and non-functional changes in single commit makes it harder to read history (bisecting for example) in future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted to separate commit

trino/client.py Outdated Show resolved Hide resolved
trino/client.py Outdated Show resolved Hide resolved
trino/client.py Outdated Show resolved Hide resolved
trino/client.py Outdated Show resolved Hide resolved
trino/dbapi.py Outdated
def commit(self):
if self.transaction is None:
def commit(self) -> None:
if self._transaction is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why go directly to field?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used transaction property, instead of directly _transaction attribute

trino/dbapi.py Outdated
@@ -207,7 +212,7 @@ def _create_request(self):
self.request_timeout,
)

def cursor(self, legacy_primitive_types: bool = None):
def cursor(self, legacy_primitive_types: Optional[bool] = None) -> 'Cursor':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> 'Cursor'? Why no qualify the name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here and other places where we use "strings" as return types

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to use Postponed Evaluation of Annotations when type hint contains names that have not been defined yet.

@@ -503,6 +521,7 @@ def executemany(self, operation, seq_of_params):
for parameters in seq_of_params[:-1]:
self.execute(operation, parameters)
self.fetchall()
assert self._query is not None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential bugfix, extract a commit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted to separate commit

Comment on lines +607 to +612
if self._query:
return self._query.result
return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential bugfix, extract a commit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted to separate commit

trino/dbapi.py Outdated Show resolved Hide resolved
@damian3031 damian3031 force-pushed the hovaesco/mypy2 branch 2 times, most recently from d906660 to e4e8bce Compare January 20, 2023 09:23
@damian3031 damian3031 force-pushed the hovaesco/mypy2 branch 2 times, most recently from 96998ec to edc9e9a Compare January 20, 2023 14:35
@damian3031
Copy link
Member Author

@hashhar I've adjusted to all your comments, PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants