Skip to content

Commit 3bb44b9

Browse files
Dev-iLskrawcz
authored andcommitted
Enable the UP006 and UP045 inspections
1 parent bb1dee8 commit 3bb44b9

300 files changed

Lines changed: 3019 additions & 3183 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

contrib/hamilton/contrib/dagworks/text_summarization/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import concurrent
1919
import logging
2020
import tempfile
21-
from typing import Generator, Union
21+
from collections.abc import Generator
22+
from typing import Union
2223

2324
logger = logging.getLogger(__name__)
2425

@@ -49,7 +50,7 @@ def summarize_text_from_summaries_prompt(content_type: str = "an academic paper"
4950

5051

5152
@config.when(file_type="pdf")
52-
def raw_text__pdf(pdf_source: Union[str, bytes, tempfile.SpooledTemporaryFile]) -> str:
53+
def raw_text__pdf(pdf_source: str | bytes | tempfile.SpooledTemporaryFile) -> str:
5354
"""Takes a filepath to a PDF and returns a string of the PDF's contents
5455
:param pdf_source: the path, or the temporary file, to the PDF.
5556
:return: the text of the PDF.
@@ -64,7 +65,7 @@ def raw_text__pdf(pdf_source: Union[str, bytes, tempfile.SpooledTemporaryFile])
6465

6566

6667
@config.when(file_type="txt")
67-
def raw_text__txt(text_file: Union[str, tempfile.SpooledTemporaryFile]) -> str:
68+
def raw_text__txt(text_file: str | tempfile.SpooledTemporaryFile) -> str:
6869
"""Takes a filepath to a text file and returns a string of the text file's contents
6970
:param text_file: the path, or the temporary file, to the text file.
7071
:return: the contents of the file as a string.

contrib/hamilton/contrib/user/elijahbenizzy/caption_images/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def openai_client() -> openai.OpenAI:
4040
return openai.OpenAI()
4141

4242

43-
def _encode_image(image_path_or_file: Union[str, IO], ext: str):
43+
def _encode_image(image_path_or_file: str | IO, ext: str):
4444
"""Helper fn to return a base-64 encoded image"""
4545
file_like_object = (
4646
image_path_or_file
@@ -79,8 +79,8 @@ def processed_image_url(image_url: str) -> str:
7979

8080
def caption_prompt(
8181
core_prompt: str,
82-
additional_prompt: Optional[str] = None,
83-
descriptiveness: Optional[str] = None,
82+
additional_prompt: str | None = None,
83+
descriptiveness: str | None = None,
8484
) -> str:
8585
"""Returns the prompt used to describe an image"""
8686
out = core_prompt
@@ -128,7 +128,7 @@ def caption_embeddings(
128128
openai_client: openai.OpenAI,
129129
embeddings_model: str = DEFAULT_EMBEDDINGS_MODEL,
130130
generated_caption: str = None,
131-
) -> List[float]:
131+
) -> list[float]:
132132
"""Returns the embeddings for a generated caption"""
133133
data = (
134134
openai_client.embeddings.create(
@@ -158,7 +158,7 @@ def caption_metadata(
158158

159159
@config.when(include_embeddings=True)
160160
def embeddings_metadata(
161-
caption_embeddings: List[float],
161+
caption_embeddings: list[float],
162162
embeddings_model: str = DEFAULT_EMBEDDINGS_MODEL,
163163
) -> dict:
164164
"""Returns metadata for the embeddings portion of the workflow"""
@@ -170,9 +170,9 @@ def embeddings_metadata(
170170

171171
def metadata(
172172
embeddings_metadata: dict,
173-
caption_metadata: Optional[dict] = None,
174-
additional_metadata: Optional[dict] = None,
175-
) -> Dict[str, Any]:
173+
caption_metadata: dict | None = None,
174+
additional_metadata: dict | None = None,
175+
) -> dict[str, Any]:
176176
"""Returns the response to a given chat"""
177177
out = embeddings_metadata
178178
if caption_metadata is not None:

contrib/hamilton/contrib/user/elijahbenizzy/convert_images_s3/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def converted_and_saved(
9292
image: Image,
9393
file_to_convert: ToConvert,
9494
new_format: str = "jpeg",
95-
image_params: Optional[Dict[str, Any]] = None,
95+
image_params: dict[str, Any] | None = None,
9696
) -> Converted:
9797
"""Returns a list of all files to convert."""
9898
s3 = _s3()
@@ -121,7 +121,7 @@ def converted_and_saved(
121121
)
122122

123123

124-
def all_converted_and_saved(converted_and_saved: Collect[Converted]) -> List[Converted]:
124+
def all_converted_and_saved(converted_and_saved: Collect[Converted]) -> list[Converted]:
125125
"""Returns a list of all downloaded locations"""
126126
return list(converted_and_saved)
127127

contrib/hamilton/contrib/user/elijahbenizzy/parallel_load_dataframes_s3/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def downloaded_data(
107107
return download_location
108108

109109

110-
def all_downloaded_data(downloaded_data: Collect[str]) -> List[str]:
110+
def all_downloaded_data(downloaded_data: Collect[str]) -> list[str]:
111111
"""Returns a list of all downloaded locations"""
112112
out = []
113113
for path in downloaded_data:
@@ -120,7 +120,7 @@ def _jsonl_parse(path: str) -> pd.DataFrame:
120120
return pd.read_json(path, lines=True)
121121

122122

123-
def processed_dataframe(all_downloaded_data: List[str]) -> pd.DataFrame:
123+
def processed_dataframe(all_downloaded_data: list[str]) -> pd.DataFrame:
124124
"""Processes everything into a dataframe"""
125125
out = []
126126
for floc in all_downloaded_data:

contrib/hamilton/contrib/user/skrawcz/customize_embeddings/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464

6565
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
66-
def _get_embedding(text: str, model="text-similarity-davinci-001", **kwargs) -> List[float]:
66+
def _get_embedding(text: str, model="text-similarity-davinci-001", **kwargs) -> list[float]:
6767
"""Get embedding from OpenAI API.
6868
:param text: text to embed.
6969
:param model: the embedding model to use.
@@ -394,7 +394,7 @@ def embedded_data_set(
394394

395395
def _accuracy_and_se(
396396
cosine_similarity: list[float], labeled_similarity: list[int]
397-
) -> Tuple[float, float]:
397+
) -> tuple[float, float]:
398398
"""Calculate accuracy (and its standard error) of predicting label=1 if similarity>x
399399
400400
x is optimized by sweeping from -1 to 1 in steps of 0.01
@@ -465,7 +465,7 @@ def accuracy_computation(
465465
return a, se
466466

467467

468-
def _embedding_multiplied_by_matrix(embedding: List[float], matrix: torch.tensor) -> np.array:
468+
def _embedding_multiplied_by_matrix(embedding: list[float], matrix: torch.tensor) -> np.array:
469469
"""Helper function to multiply an embedding by a matrix."""
470470
embedding_tensor = torch.tensor(embedding).float()
471471
modified_embedding = embedding_tensor @ matrix
@@ -530,7 +530,7 @@ def tensors_from_dataframe(
530530
embedding_column_1: str,
531531
embedding_column_2: str,
532532
similarity_label_column: str,
533-
) -> Tuple[torch.tensor, torch.tensor, torch.tensor]:
533+
) -> tuple[torch.tensor, torch.tensor, torch.tensor]:
534534
e1 = np.stack(np.array(df[embedding_column_1].values))
535535
e2 = np.stack(np.array(df[embedding_column_2].values))
536536
s = np.stack(np.array(df[similarity_label_column].astype("float").values))
@@ -638,7 +638,7 @@ def mse_loss(predictions, targets):
638638
optimization_result_matrices=group(*[source(k) for k in optimization_parameterization.keys()])
639639
)
640640
def optimization_results(
641-
optimization_result_matrices: List[pd.DataFrame],
641+
optimization_result_matrices: list[pd.DataFrame],
642642
) -> pd.DataFrame:
643643
"""Combine optimization results into one dataframe."""
644644
return pd.concat(optimization_result_matrices)

contrib/hamilton/contrib/user/skrawcz/fine_tuning/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def raw_dataset(
6262
validation_size: float = 0.8,
6363
input_text_key: str = "question",
6464
output_text_key: str = "reply",
65-
) -> Dict[str, Dataset]:
65+
) -> dict[str, Dataset]:
6666
"""Loads the raw dataset from disk and splits it into train and test sets.
6767
6868
:param data_path: the path to the dataset.

contrib/hamilton/contrib/user/zilto/lancedb_vdb/__init__.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
# under the License.
1717

1818
import logging
19+
from collections.abc import Iterable
1920
from pathlib import Path
20-
from typing import Dict, Iterable, List, Optional, Union
21+
from typing import Dict, List, Optional, Union
2122

2223
logger = logging.getLogger(__name__)
2324

@@ -33,11 +34,11 @@
3334
from hamilton.function_modifiers import tag
3435

3536
VectorType = Union[list, np.ndarray, pa.Array, pa.ChunkedArray]
36-
DataType = Union[Dict, List[Dict], pd.DataFrame, pa.Table, Iterable[pa.RecordBatch]]
37+
DataType = Union[dict, list[dict], pd.DataFrame, pa.Table, Iterable[pa.RecordBatch]]
3738
TableSchema = Union[pa.Schema, LanceModel]
3839

3940

40-
def client(uri: Union[str, Path] = "./.lancedb") -> lancedb.DBConnection:
41+
def client(uri: str | Path = "./.lancedb") -> lancedb.DBConnection:
4142
"""Create a LanceDB connection.
4243
4344
:param uri: path to local LanceDB
@@ -49,7 +50,7 @@ def client(uri: Union[str, Path] = "./.lancedb") -> lancedb.DBConnection:
4950
def _create_table(
5051
client: lancedb.DBConnection,
5152
table_name: str,
52-
schema: Optional[TableSchema] = None,
53+
schema: TableSchema | None = None,
5354
overwrite_table: bool = False,
5455
) -> lancedb.db.LanceTable:
5556
"""Create a new table based on schema."""
@@ -62,7 +63,7 @@ def _create_table(
6263
def table_ref(
6364
client: lancedb.DBConnection,
6465
table_name: str,
65-
schema: Optional[TableSchema] = None,
66+
schema: TableSchema | None = None,
6667
overwrite_table: bool = False,
6768
) -> lancedb.db.LanceTable:
6869
"""Create or reference a LanceDB table
@@ -91,7 +92,7 @@ def table_ref(
9192

9293

9394
@tag(side_effect="True")
94-
def reset(client: lancedb.DBConnection) -> Dict[str, List[str]]:
95+
def reset(client: lancedb.DBConnection) -> dict[str, list[str]]:
9596
"""Drop all existing tables.
9697
9798
:param vdb_client: LanceDB connection.
@@ -106,7 +107,7 @@ def reset(client: lancedb.DBConnection) -> Dict[str, List[str]]:
106107

107108

108109
@tag(side_effect="True")
109-
def insert(table_ref: lancedb.db.LanceTable, data: DataType) -> Dict:
110+
def insert(table_ref: lancedb.db.LanceTable, data: DataType) -> dict:
110111
"""Push new data to the specified table.
111112
112113
:param table_ref: Reference to the LanceDB table.
@@ -121,7 +122,7 @@ def insert(table_ref: lancedb.db.LanceTable, data: DataType) -> Dict:
121122

122123

123124
@tag(side_effect="True")
124-
def delete(table_ref: lancedb.db.LanceTable, delete_expression: str) -> Dict:
125+
def delete(table_ref: lancedb.db.LanceTable, delete_expression: str) -> dict:
125126
"""Delete existing data using an SQL expression.
126127
127128
:param table_ref: Reference to the LanceDB table.
@@ -138,8 +139,8 @@ def delete(table_ref: lancedb.db.LanceTable, delete_expression: str) -> Dict:
138139
def vector_search(
139140
table_ref: lancedb.db.LanceTable,
140141
vector_query: VectorType,
141-
columns: Optional[List[str]] = None,
142-
where: Optional[str] = None,
142+
columns: list[str] | None = None,
143+
where: str | None = None,
143144
prefilter_where: bool = False,
144145
limit: int = 10,
145146
) -> pd.DataFrame:
@@ -169,8 +170,8 @@ def vector_search(
169170
def full_text_search(
170171
table_ref: lancedb.db.LanceTable,
171172
full_text_query: str,
172-
full_text_index: Union[str, List[str]],
173-
where: Optional[str] = None,
173+
full_text_index: str | list[str],
174+
where: str | None = None,
174175
limit: int = 10,
175176
rebuild_index: bool = True,
176177
) -> pd.DataFrame:

contrib/hamilton/contrib/user/zilto/llm_generate_code/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import openai
3131

3232

33-
def llm_client(api_key: Optional[str] = None) -> openai.OpenAI:
33+
def llm_client(api_key: str | None = None) -> openai.OpenAI:
3434
"""Create an OpenAI client."""
3535
if api_key is None:
3636
api_key = os.environ.get("OPENAI_API_KEY")

contrib/hamilton/contrib/user/zilto/nixtla_mlforecast/__init__.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# under the License.
1717

1818
import logging
19-
from typing import Any, Callable, Iterable, Optional, Union
19+
from collections.abc import Callable, Iterable
20+
from typing import Any, TypeAlias
2021

2122
logger = logging.getLogger(__name__)
2223

@@ -38,12 +39,12 @@
3839

3940

4041
# sklearn compliant models (including XGBoost and LightGBM) subclass BaseEstimator
41-
MODELS_TYPE = Union[
42-
BaseEstimator, list[BaseEstimator], dict[str, BaseEstimator]
43-
] # equivalent to mlforecast.core.Models
44-
LAG_TRANSFORMS_TYPE = dict[int, list[Union[Callable, tuple[Callable, Any]]]]
45-
DATE_FEATURES_TYPE = Iterable[Union[str, Callable]]
46-
CONFIDENCE_INTERVAL_TYPE = Optional[list[Union[int, float]]]
42+
MODELS_TYPE = (
43+
BaseEstimator | list[BaseEstimator] | dict[str, BaseEstimator]
44+
) # equivalent to mlforecast.core.Models
45+
LAG_TRANSFORMS_TYPE: TypeAlias = dict[int, list[Callable | tuple[Callable, Any]]]
46+
DATE_FEATURES_TYPE: TypeAlias = Iterable[str | Callable]
47+
CONFIDENCE_INTERVAL_TYPE: TypeAlias = list[int | float] | None
4748

4849

4950
def base_models() -> MODELS_TYPE:
@@ -81,11 +82,11 @@ def date_features() -> DATE_FEATURES_TYPE:
8182

8283
def forecaster(
8384
base_models: MODELS_TYPE,
84-
freq: Union[int, str] = "M",
85-
lags: Optional[list[int]] = None,
86-
lag_transforms: Optional[LAG_TRANSFORMS_TYPE] = None,
87-
date_features: Optional[DATE_FEATURES_TYPE] = None,
88-
target_transforms: Optional[list[BaseTargetTransform]] = None,
85+
freq: int | str = "M",
86+
lags: list[int] | None = None,
87+
lag_transforms: LAG_TRANSFORMS_TYPE | None = None,
88+
date_features: DATE_FEATURES_TYPE | None = None,
89+
target_transforms: list[BaseTargetTransform] | None = None,
8990
num_threads: int = 1,
9091
) -> MLForecast:
9192
"""Create the forecasting harness with data and models
@@ -106,15 +107,15 @@ def forecaster(
106107
def cross_validation_predictions(
107108
forecaster: MLForecast,
108109
dataset: pd.DataFrame,
109-
static_features: Optional[list[str]] = None,
110+
static_features: list[str] | None = None,
110111
dropna: bool = True,
111-
keep_last_n_inputs: Optional[int] = None,
112-
train_models_for_n_horizons: Optional[int] = None,
112+
keep_last_n_inputs: int | None = None,
113+
train_models_for_n_horizons: int | None = None,
113114
confidence_percentile: CONFIDENCE_INTERVAL_TYPE = None,
114115
cv_n_windows: int = 2,
115116
cv_forecast_horizon: int = 12,
116-
cv_step_size: Optional[int] = None,
117-
cv_input_size: Optional[int] = None,
117+
cv_step_size: int | None = None,
118+
cv_input_size: int | None = None,
118119
cv_refit: bool = True,
119120
cv_save_train_predictions: bool = True,
120121
) -> pd.DataFrame:
@@ -182,10 +183,10 @@ def best_model_per_series(cross_validation_evaluation: pd.DataFrame) -> pd.Serie
182183
def fitted_forecaster(
183184
forecaster: MLForecast,
184185
dataset: pd.DataFrame,
185-
static_features: Optional[list[str]] = None,
186+
static_features: list[str] | None = None,
186187
dropna: bool = True,
187-
keep_last_n: Optional[int] = None,
188-
train_models_for_n_horizons: Optional[int] = None,
188+
keep_last_n: int | None = None,
189+
train_models_for_n_horizons: int | None = None,
189190
save_train_predictions: bool = True,
190191
) -> MLForecast:
191192
"""Fit models over full dataset"""
@@ -202,9 +203,9 @@ def fitted_forecaster(
202203
def inference_predictions(
203204
fitted_forecaster: MLForecast,
204205
inference_forecast_horizon: int = 12,
205-
inference_uids: Optional[list[str]] = None,
206-
inference_dataset: Optional[pd.DataFrame] = None,
207-
inference_exogenous: Optional[pd.DataFrame] = None,
206+
inference_uids: list[str] | None = None,
207+
inference_dataset: pd.DataFrame | None = None,
208+
inference_exogenous: pd.DataFrame | None = None,
208209
confidence_percentile: CONFIDENCE_INTERVAL_TYPE = None,
209210
) -> pd.DataFrame:
210211
"""Infer values using the trained models
@@ -221,8 +222,8 @@ def inference_predictions(
221222

222223
def plotting_config(
223224
plot_max_n_series: int = 4,
224-
plot_uids: Optional[list[str]] = None,
225-
plot_models: Optional[list[str]] = None,
225+
plot_uids: list[str] | None = None,
226+
plot_models: list[str] | None = None,
226227
plot_anomalies: bool = False,
227228
plot_confidence_percentile: CONFIDENCE_INTERVAL_TYPE = None,
228229
plot_engine: str = "matplotlib",

contrib/hamilton/contrib/user/zilto/nixtla_statsforecast/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# under the License.
1717

1818
import logging
19-
from typing import Callable, Optional
19+
from collections.abc import Callable
20+
from typing import Optional
2021

2122
logger = logging.getLogger(__name__)
2223

@@ -155,8 +156,8 @@ def inference_predictions(
155156

156157

157158
def plotting_config(
158-
plot_uids: Optional[list[str]] = None,
159-
plot_models: Optional[list[str]] = None,
159+
plot_uids: list[str] | None = None,
160+
plot_models: list[str] | None = None,
160161
plot_anomalies: bool = False,
161162
plot_confidence_percentile: list[float] = [90.0], # noqa: B006
162163
plot_engine: str = "matplotlib",

0 commit comments

Comments
 (0)