Skip to content

Commit

Permalink
Add type hints (cont.) (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
sitingren authored Dec 15, 2023
1 parent 4a13f85 commit c146228
Show file tree
Hide file tree
Showing 83 changed files with 233 additions and 191 deletions.
2 changes: 1 addition & 1 deletion vertica_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .vertica.connection import Connection, connect, parse_dsn

Expand Down
28 changes: 16 additions & 12 deletions vertica_python/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
# THE SOFTWARE.


from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from datetime import date, datetime, time, timezone
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Optional


# noinspection PyPep8Naming
Expand Down Expand Up @@ -298,7 +301,7 @@ def __ne__(self, other):
}

def getTypeName(data_type_oid: int, type_modifier: int) -> str:
"""Returns the base type name according to data_type_oid and type_modifier"""
"""Returns the base type name according to data_type_oid and type_modifier."""
if data_type_oid in TYPENAME:
return TYPENAME[data_type_oid]
elif data_type_oid in (VerticaType.INTERVAL, VerticaType.INTERVALYM):
Expand All @@ -310,12 +313,12 @@ def getTypeName(data_type_oid: int, type_modifier: int) -> str:
else:
return "Unknown"

def getComplexElementType(data_type_oid: int):
"""For 1D ARRAY or SET, returns the type of its elements"""
def getComplexElementType(data_type_oid: int) -> Optional[int]:
"""For 1D ARRAY or SET, returns the type of its elements."""
return COMPLEX_ELEMENT_TYPE.get(data_type_oid)

def getIntervalRange(data_type_oid: int, type_modifier: int):
"""Extracts an interval's range from the bits set in its type_modifier"""
def getIntervalRange(data_type_oid: int, type_modifier: int) -> str:
"""Extracts an interval's range from the bits set in its type_modifier."""

if data_type_oid not in (VerticaType.INTERVAL, VerticaType.INTERVALYM):
raise ValueError("Invalid data type OID: {}".format(data_type_oid))
Expand Down Expand Up @@ -361,7 +364,7 @@ def getIntervalRange(data_type_oid: int, type_modifier: int):
return "Day to Second"


def getIntervalLeadingPrecision(data_type_oid: int, type_modifier: int):
def getIntervalLeadingPrecision(data_type_oid: int, type_modifier: int) -> int:
"""
Returns the leading precision for an interval, which is the largest number
of digits that can fit in the leading field of the interval.
Expand Down Expand Up @@ -394,7 +397,7 @@ def getIntervalLeadingPrecision(data_type_oid: int, type_modifier: int):
raise ValueError("Invalid interval range: {}".format(interval_range))


def getPrecision(data_type_oid: int, type_modifier: int):
def getPrecision(data_type_oid: int, type_modifier: int) -> Optional[int]:
"""
Returns the precision for the given Vertica type with consideration of
the type modifier.
Expand Down Expand Up @@ -423,22 +426,23 @@ def getPrecision(data_type_oid: int, type_modifier: int):
return None # None if no meaningful values can be provided


def getScale(data_type_oid: int, type_modifier: int):
def getScale(data_type_oid: int, type_modifier: int) -> Optional[int]:
"""
Returns the scale for the given Vertica type with consideration of
the type modifier.
the type modifier. Returns None if no meaningful values can be provided.
"""

if data_type_oid == VerticaType.NUMERIC:
return 15 if type_modifier == -1 else (type_modifier - 4) & 0xFF
else:
return None # None if no meaningful values can be provided
return None


def getDisplaySize(data_type_oid: int, type_modifier: int):
def getDisplaySize(data_type_oid: int, type_modifier: int) -> Optional[int]:
"""
Returns the column display size for the given Vertica type with
consideration of the type modifier.
Returns None if no meaningful values can be provided.
The display size of a column is the maximum number of characters needed to
display data in character form.
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
# THE SOFTWARE.


from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import re

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/os_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import errno
import os
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import os
import sys
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import pytest

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonIntegrationTestCase

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from multiprocessing import Process
import pytest
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonIntegrationTestCase

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import getpass
import socket
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from datetime import date, datetime, time
from dateutil.relativedelta import relativedelta
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from datetime import date, datetime, time
from dateutil.relativedelta import relativedelta
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from collections import namedtuple
from datetime import date
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonIntegrationTestCase

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_loadbalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonIntegrationTestCase

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from collections import namedtuple
from datetime import datetime
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import os
import socket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonIntegrationTestCase

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/integration_tests/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonIntegrationTestCase

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/unit_tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import pytest
from ..common.base import VerticaPythonTestCase
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/unit_tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonUnitTestCase
from ...errors import VerticaSyntaxError
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/unit_tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import logging
import os
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/unit_tests/test_notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

import mock

Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/unit_tests/test_parsedsn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from .base import VerticaPythonUnitTestCase
from ...vertica.connection import parse_dsn
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/unit_tests/test_sql_literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from collections import namedtuple
from decimal import Decimal
Expand Down
2 changes: 1 addition & 1 deletion vertica_python/tests/unit_tests/test_timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from collections import namedtuple
from datetime import datetime
Expand Down
22 changes: 15 additions & 7 deletions vertica_python/vertica/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
# THE SOFTWARE.


from __future__ import print_function, division, absolute_import
from __future__ import print_function, division, absolute_import, annotations

from collections import namedtuple
from typing import TYPE_CHECKING, NamedTuple
if TYPE_CHECKING:
from typing import Optional

from ..datatypes import getDisplaySize, getPrecision, getScale
from ..compat import as_str, as_text
Expand All @@ -49,12 +51,18 @@ class FormatCode(object):
BINARY = 1


ColumnTuple = namedtuple('Column', ['name', 'type_code', 'display_size', 'internal_size',
'precision', 'scale', 'null_ok'])
class ColumnTuple(NamedTuple):
name: str
type_code: int
display_size: Optional[int]
internal_size: int
precision: Optional[int]
scale: Optional[int]
null_ok: bool


class Column(object):
def __init__(self, col):
def __init__(self, col) -> None:
# Describe one query result column
self.name = col['name']
self.type_code = col['data_type_oid']
Expand All @@ -74,7 +82,7 @@ def __init__(self, col):
self.props = ColumnTuple(self.name, self.type_code, self.display_size, self.internal_size,
self.precision, self.scale, self.null_ok)

def add_child_column(self, col):
def add_child_column(self, col: Column) -> None:
"""
Complex types involve multiple columns arranged in a hierarchy of parents and children.
Each parent column stores references to child columns in a list.
Expand All @@ -83,7 +91,7 @@ def add_child_column(self, col):
self.child_columns = []
self.child_columns.append(col)

def debug_info(self):
def debug_info(self) -> str:
childs = ""
if self.child_columns:
c = ", ".join([col.debug_info() for col in self.child_columns])
Expand Down
8 changes: 4 additions & 4 deletions vertica_python/vertica/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def ssl(self) -> bool:
"""Returns True if the TCP socket is a SSL socket."""
return self.socket is not None and isinstance(self.socket, ssl.SSLSocket)

def write(self, message, vsocket=None) -> None:
def write(self, message: FrontendMessage, vsocket: Optional[Union[socket.socket, ssl.SSLSocket]] = None) -> None:
if not isinstance(message, FrontendMessage):
raise TypeError("invalid message: ({0})".format(message))
if vsocket is None:
Expand Down Expand Up @@ -670,14 +670,14 @@ def reset_connection(self) -> None:
self.close()
self.startup_connection()

def is_asynchronous_message(self, message) -> bool:
def is_asynchronous_message(self, message: BackendMessage) -> bool:
# Check if it is an asynchronous response message
# Note: ErrorResponse is a subclass of NoticeResponse
return (isinstance(message, messages.ParameterStatus) or
(isinstance(message, messages.NoticeResponse) and
not isinstance(message, messages.ErrorResponse)))

def handle_asynchronous_message(self, message) -> None:
def handle_asynchronous_message(self, message: Union[messages.ParameterStatus, messages.NoticeResponse]) -> None:
if isinstance(message, messages.ParameterStatus):
if message.name == 'protocol_version':
message.value = int(message.value)
Expand All @@ -700,7 +700,7 @@ def read_string(self) -> bytearray:
s.extend(char)
return s

def read_message(self):
def read_message(self) -> BackendMessage:
while True:
try:
type_ = self.read_bytes(1)
Expand Down
Loading

0 comments on commit c146228

Please sign in to comment.