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

Added static typing to data.py and filepath_or_buffer.py #682

Merged
merged 3 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dataprofiler/data_readers/data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Contains factory class reading various kinds of data."""
from __future__ import absolute_import, division
from typing import Any, Dict, List, Optional

from .. import dp_logging
from .avro_data import AVROData
Expand All @@ -16,7 +17,7 @@
class Data(object):
"""Factory class for reading various kinds of data."""

data_classes = [
data_classes: List[Dict] = [
dict(data_class=JSONData, kwargs=dict()),
dict(data_class=GraphData, kwargs=dict()),
dict(data_class=CSVData, kwargs=dict()),
Expand All @@ -25,7 +26,7 @@ class Data(object):
dict(data_class=TextData, kwargs=dict()),
]

def __new__(cls, input_file_path=None, data=None, data_type=None, options=None):
def __new__(cls, input_file_path: Optional[str]=None, data: Optional[Any]=None, data_type: Optional[str]=None, options: Optional[Dict]=None):
"""
Create Factory Data object.

Expand Down
37 changes: 20 additions & 17 deletions dataprofiler/data_readers/filepath_or_buffer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Contains functions and classes for handling filepaths and buffers."""
from io import BytesIO, StringIO, TextIOWrapper, open
from typing import IO, Any, Optional, Type, Union, cast


def is_stream_buffer(filepath_or_buffer):
def is_stream_buffer(filepath_or_buffer: Any) -> bool:
"""
Determine whether a given argument is a filepath or buffer.

Expand All @@ -25,12 +26,12 @@ class FileOrBufferHandler:

def __init__(
self,
filepath_or_buffer,
open_method="r",
encoding=None,
seek_offset=None,
seek_whence=0,
):
filepath_or_buffer: Union[str, StringIO, BytesIO],
open_method: str ="r",
encoding: Optional[str]=None,
seek_offset: Optional[int]=None,
seek_whence: int=0,
) -> None:
taylorfturner marked this conversation as resolved.
Show resolved Hide resolved
"""
Initialize Context manager class.

Expand All @@ -45,15 +46,15 @@ def __init__(
:type seek_offset: int
:return: TextIOBase or BufferedIOBase class/subclass
"""
self._filepath_or_buffer = filepath_or_buffer
self.open_method = open_method
self.seek_offset = seek_offset
self.seek_whence = seek_whence
self._encoding = encoding
self.original_type = type(filepath_or_buffer)
self._is_wrapped = False

def __enter__(self):
self._filepath_or_buffer: Union[str, StringIO, BytesIO, IO] = filepath_or_buffer
self.open_method: str = open_method
self.seek_offset: Optional[int] = seek_offset
self.seek_whence: int = seek_whence
self._encoding: Optional[str] = encoding
self.original_type: Union[Type[str], Type[StringIO], Type[BytesIO], Type[IO]] = type(filepath_or_buffer)
self._is_wrapped: bool = False

def __enter__(self) -> IO:
"""Open resources."""
if isinstance(self._filepath_or_buffer, str):
self._filepath_or_buffer = open(
Expand All @@ -79,15 +80,17 @@ def __enter__(self):

return self._filepath_or_buffer

def __exit__(self, exc_type, exc_value, exc_traceback):
def __exit__(self, exc_type: Any, exc_value: Any, exc_traceback: Any) -> None:
"""Release resources."""
# Need to detach buffer if wrapped (i.e. BytesIO opened with 'r')
if self._is_wrapped:
self._filepath_or_buffer = cast(TextIOWrapper, self._filepath_or_buffer) # guaranteed by self._is_wrapped
Copy link
Contributor

Choose a reason for hiding this comment

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

potential for follow-up around casting and ensuring typing declaration is right and/or code is corrected to avoid the change of a wrong type

wrapper = self._filepath_or_buffer
self._filepath_or_buffer = wrapper.buffer
wrapper.detach()

if isinstance(self._filepath_or_buffer, (StringIO, BytesIO)):
self._filepath_or_buffer.seek(0)
else:
self._filepath_or_buffer = cast(IO, self._filepath_or_buffer) # can't be str due to conversion in __enter__
Copy link
Contributor

Choose a reason for hiding this comment

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

potential for follow-up around casting and ensuring typing declaration is right and/or code is corrected to avoid the change of a wrong type

self._filepath_or_buffer.close()