Skip to content

Commit dba7374

Browse files
dawnhoganDawnHoganhowso-automation
authored
21292: Improves typehinting for utilities module (#267)
Co-authored-by: DawnHogan <[email protected]> Co-authored-by: howso-automation <[email protected]>
1 parent 7c0378f commit dba7374

File tree

3 files changed

+108
-64
lines changed

3 files changed

+108
-64
lines changed

Diff for: howso/utilities/features.py

+26-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from enum import Enum
66
from functools import partial
77
import locale
8-
from typing import Any, Optional
8+
import typing as t
99
import warnings
1010

1111
import numpy as np
@@ -62,31 +62,42 @@ class FeatureSerializer:
6262
@classmethod
6363
def serialize( # noqa: C901
6464
cls,
65-
data: pd.DataFrame | np.ndarray | Iterable[Any] | None,
65+
data: t.Optional[pd.DataFrame | np.ndarray | Iterable[t.Any]],
6666
columns: Iterable[str] | None,
6767
features: Mapping,
6868
*,
6969
warn: bool = False
70-
) -> list[list[Any]] | None:
70+
) -> list[list[t.Any]] | None:
7171
"""
7272
Serialize case data into list of lists.
7373
7474
Parameters
7575
----------
76-
data : pandas.DataFrame or numpy.ndarray or list of list or None
77-
The data to serialize.
78-
columns : list of str or None
79-
The case column mapping. The order corresponds to the order of cases
80-
in output.
76+
data : pd.DataFrame or np.ndarray or Iterable, default None
77+
The data to serialize, typically in a Pandas DataFrame, Numpy ndarray or Python Iterable such as a list.
78+
columns : Iterable of str, default None
79+
The case column mapping. The order corresponds to the order of cases in output.
80+
`columns` must be provided for non-DataFrame Iterables.
8181
features : Mapping
8282
The dictionary of feature name to feature attributes.
8383
warn : bool, default False
8484
If warnings should be raised by serializer.
8585
8686
Returns
8787
-------
88-
list of list or None
88+
list of list or Any or None
8989
The serialized data from DataFrame.
90+
91+
...
92+
93+
Raises
94+
------
95+
HowsoError
96+
An `pd.ndarray` or `Iterable` is provided, `columns` was left undefined
97+
or the given columns does not match the columns defined within a given `pd.DataFrame`.
98+
ValueError
99+
The provided `pd.DataFrame` contains non-unique columns or, an unexpected datatype
100+
was received (should be either pd.DataFrame, np.ndarray or Python Iterable (non-str)).
90101
"""
91102
# Import locally to prevent a circular import
92103
from howso.client.exceptions import HowsoError
@@ -158,9 +169,9 @@ def serialize( # noqa: C901
158169
@classmethod
159170
def deserialize(
160171
cls,
161-
data: Iterable[Iterable[Any]] | Iterable[Mapping[str, Any]],
172+
data: Iterable[Iterable[t.Any] | Mapping[str, t.Any]],
162173
columns: Iterable[str],
163-
features: Optional[Mapping] = None
174+
features: t.Optional[Mapping] = None
164175
) -> pd.DataFrame:
165176
"""
166177
Deserialize case data into a DataFrame.
@@ -172,8 +183,9 @@ def deserialize(
172183
----------
173184
data : list of list or list of dict
174185
The context data.
175-
columns : list of str
176-
The case column mapping.
186+
columns : Iterable of str
187+
The case column mapping. The order corresponds to the order of cases in output.
188+
`columns` must be provided for non-DataFrame Iterables.
177189
178190
The order corresponds to how the data will be mapped to columns in
179191
the output. Ignored for list of dict where the dict key is the column
@@ -635,7 +647,7 @@ def format_unknown_column(cls, column: pd.Series, feature: Mapping
635647
return column
636648

637649
@staticmethod
638-
def _get_typing_info(feature: Optional[Mapping]) -> dict:
650+
def _get_typing_info(feature: t.Optional[Mapping]) -> dict:
639651
"""
640652
Get typing info from feature attributes.
641653

Diff for: howso/utilities/installation_verification.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import random
1515
import sys
1616
import traceback
17-
from typing import Callable, Iterable, List, Optional, Tuple, Union
17+
import typing as t
1818
import warnings
1919

2020
from faker.config import AVAILABLE_LOCALES
@@ -67,17 +67,17 @@ class Status(IntEnum):
6767
OK = 4
6868

6969

70-
Requirements = Iterable[Union[type, object]]
70+
Requirements = t.Iterable[t.Union[type, object]]
7171

7272

7373
@dataclass
7474
class Check:
7575
"""Store the specification of a single check."""
7676

7777
name: str
78-
fn: Callable
79-
client_required: Optional[str] = None
80-
other_requirements: Optional[Requirements] = None
78+
fn: t.Callable
79+
client_required: t.Optional[str] = None
80+
other_requirements: t.Optional[Requirements] = None
8181

8282

8383
class InstallationCheckRegistry:
@@ -106,9 +106,9 @@ def __init__(self):
106106
)
107107

108108
def add_check(self, name: str,
109-
fn: Callable,
110-
client_required: Optional[str] = None,
111-
other_requirements: Optional[Requirements] = None
109+
fn: t.Callable,
110+
client_required: t.Optional[str] = None,
111+
other_requirements: t.Optional[Requirements] = None
112112
):
113113
"""
114114
Add a check for this installation.
@@ -129,7 +129,7 @@ def add_check(self, name: str,
129129
"""
130130
if (
131131
other_requirements and
132-
not isinstance(other_requirements, Iterable)
132+
not isinstance(other_requirements, t.Iterable)
133133
):
134134
other_requirements = [other_requirements]
135135
self._checks.append(
@@ -167,8 +167,15 @@ def client(self) -> AbstractHowsoClient:
167167
return self._client
168168

169169
@property
170-
def client_classes(self) -> List[str]:
171-
"""Return list of super class names for the current cached client."""
170+
def client_classes(self) -> list[str]:
171+
"""
172+
Return list of super class names for the current cached client.
173+
174+
Returns
175+
-------
176+
list of class names
177+
"""
178+
172179
if self._client is None:
173180
return []
174181
if self._client_classes == []:
@@ -460,8 +467,8 @@ def get_nonce(length=8) -> str:
460467

461468
def generate_dataframe(*, client: AbstractHowsoClient,
462469
num_samples: int = 150,
463-
timeout: Optional[int] = None
464-
) -> Tuple[pd.DataFrame, Union[float, int]]:
470+
timeout: t.Optional[int] = None
471+
) -> tuple[pd.DataFrame, float | int]:
465472
"""
466473
Use HowsoClient to create a dataframe of random data.
467474
@@ -582,7 +589,7 @@ def check_not_emulated(*, registry: InstallationCheckRegistry):
582589

583590

584591
def check_generate_dataframe(*, registry: InstallationCheckRegistry,
585-
threshold: Optional[float] = None):
592+
threshold: t.Optional[float] = None):
586593
"""
587594
Rate the speed in which a dataframe was able to be generated.
588595
@@ -696,7 +703,7 @@ def check_locales_available(*, registry: InstallationCheckRegistry):
696703

697704

698705
def check_save(*, registry: InstallationCheckRegistry,
699-
source_df: Optional[pd.DataFrame] = None):
706+
source_df: t.Optional[pd.DataFrame] = None):
700707
"""
701708
Ensure that a model can can be saved.
702709
@@ -745,7 +752,7 @@ def check_save(*, registry: InstallationCheckRegistry,
745752

746753

747754
def check_synthesizer_create_delete(*, registry: InstallationCheckRegistry,
748-
source_df: Optional[pd.DataFrame] = None):
755+
source_df: t.Optional[pd.DataFrame] = None):
749756
"""
750757
Ensure that a model can can be created and deleted.
751758
@@ -799,7 +806,7 @@ def check_synthesizer_create_delete(*, registry: InstallationCheckRegistry,
799806

800807

801808
def check_latency(*, registry: InstallationCheckRegistry,
802-
source_df: Optional[pd.DataFrame] = None,
809+
source_df: t.Optional[pd.DataFrame] = None,
803810
notice_threshold: int = 10, warning_threshold: int = 20,
804811
timeout: int = 10):
805812
"""
@@ -922,7 +929,7 @@ def check_performance(*, registry: InstallationCheckRegistry,
922929
def check_engine_operation(
923930
*,
924931
registry: InstallationCheckRegistry,
925-
source_df: Optional[pd.DataFrame] = None
932+
source_df: t.Optional[pd.DataFrame] = None
926933
):
927934
"""
928935
Ensure that Howso Engine operates as it should.
@@ -991,7 +998,7 @@ def check_engine_operation(
991998

992999
def check_validator_operation(
9931000
*, registry: InstallationCheckRegistry,
994-
source_df: Optional[pd.DataFrame] = None,
1001+
source_df: t.Optional[pd.DataFrame] = None,
9951002
):
9961003
"""
9971004
Ensure that Validator-Enterprise operates as it should.

0 commit comments

Comments
 (0)