Skip to content

Commit f914804

Browse files
author
Tim Band
committed
Cleaned pre-commit checks
1 parent c1c2858 commit f914804

13 files changed

Lines changed: 130 additions & 127 deletions

File tree

datafaker/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import gzip
33
import os
44
import random
5-
from abc import ABC, abstractmethod
65
from collections.abc import Callable
76
from dataclasses import dataclass
87
from io import TextIOWrapper
@@ -12,7 +11,7 @@
1211
import yaml
1312
from sqlalchemy import Connection, insert
1413
from sqlalchemy.exc import SQLAlchemyError
15-
from sqlalchemy.schema import MetaData, Table
14+
from sqlalchemy.schema import Table
1615

1716
from datafaker.utils import (
1817
MAKE_VOCAB_PROGRESS_REPORT_EVERY,

datafaker/create.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from sqlalchemy.schema import CreateColumn, CreateSchema, CreateTable, MetaData, Table
1313

1414
from datafaker.base import FileUploader
15-
from datafaker.make import StoryGeneratorInfo, get_generation_info
15+
from datafaker.make import FunctionCall, StoryGeneratorInfo, get_generation_info
1616
from datafaker.populate import (
1717
TableGenerator,
1818
call_function,
@@ -159,12 +159,12 @@ def create_db_data(
159159
try:
160160
with src_stats_filename.open(encoding="utf-8") as fh:
161161
src_stats = yaml.load(fh, yaml.SafeLoader)
162-
except FileNotFoundError:
162+
except FileNotFoundError as exc:
163163
logger.error(
164-
"No source stats file '%', this should be the output of the 'make-stats' command",
164+
"No source stats file '%s', this should be the output of the 'make-stats' command",
165165
src_stats_filename,
166166
)
167-
raise typer.Exit(1)
167+
raise typer.Exit(1) from exc
168168
else:
169169
src_stats = None
170170
return create_db_data_into(
@@ -205,7 +205,7 @@ def create_db_data_into(
205205
context = get_symbols(
206206
gen_info.row_generator_module_name,
207207
gen_info.story_generator_module_name,
208-
get_property(config, "object_instantiation", dict, {}),
208+
get_property(config, "object_instantiation", {}),
209209
src_stats,
210210
metadata,
211211
)
@@ -229,6 +229,14 @@ def create_db_data_into(
229229
return row_counts
230230

231231

232+
def empty_story_generator() -> (
233+
Generator[tuple[str, dict[str, Any]], dict[str, Any], None]
234+
):
235+
"""Get a story generator that generates no values."""
236+
empt: list[tuple[str, dict[str, Any]]] = []
237+
yield from empt
238+
239+
232240
# pylint: disable=too-many-instance-attributes
233241
class StoryIterator:
234242
"""Iterates through all the rows produced by all the stories."""
@@ -250,9 +258,10 @@ def __init__(
250258
self._final_values: dict[str, Any] | None = None
251259
# Number of times the current story should be run
252260
self._story_counts = 1
253-
self._story_function_call = None
261+
self._story_function_call: FunctionCall
254262
self._context = context
255-
self._story = iter([])
263+
self._story = empty_story_generator()
264+
self._provided_values: dict[str, Any]
256265
self.next()
257266

258267
def _get_next_story(self) -> bool:
@@ -276,6 +285,7 @@ def _get_next_story(self) -> bool:
276285
return True
277286

278287
def _get_values(self) -> None:
288+
"""Get the values from the current story and advance the iterator."""
279289
if self._final_values is None:
280290
self._table_name, self._provided_values = next(self._story)
281291
else:
@@ -342,7 +352,7 @@ def next(self) -> None:
342352
try:
343353
self._get_values()
344354
return
345-
except StopIteration as exc:
355+
except StopIteration:
346356
self._final_values = None
347357
self._story_counts -= 1
348358
if 0 < self._story_counts:

datafaker/dump.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io
44
from abc import ABC, abstractmethod
55
from pathlib import Path
6-
from typing import TYPE_CHECKING
76

87
import pandas as pd
98
import sqlalchemy

datafaker/generators/partitioned.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def get_named_tables(self) -> Mapping[str, str]:
445445

446446
def __init__(self, config: Mapping[str, Any]) -> None:
447447
"""Initialize the null partitioned generator factory."""
448-
tables = get_property(config, "tables", dict, {})
448+
tables: dict[str, Any] = get_property(config, "tables", {})
449449
self._named_tables = {
450450
table_name: table_conf["name_column"]
451451
for table_name, table_conf in tables.items()

datafaker/main.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
generated_tables,
4545
generators_require_stats,
4646
get_flag,
47-
import_file,
4847
logger,
4948
read_config_file,
5049
sorted_non_vocabulary_tables,
@@ -263,22 +262,22 @@ def create_tables(
263262

264263
@app.command()
265264
def create_generators(
266-
orm_file: Path = Option(
265+
_orm_file: Path = Option(
267266
ORM_FILENAME,
268267
help="The name of the ORM yaml file",
269268
dir_okay=False,
270269
),
271-
df_file: Path = Option(
270+
_df_file: Path = Option(
272271
DF_FILENAME,
273272
help="Path to write Python generators to.",
274273
dir_okay=False,
275274
),
276-
config_file: Path = Option(
275+
_config_file: Path = Option(
277276
CONFIG_FILENAME,
278277
help="The configuration file",
279278
dir_okay=False,
280279
),
281-
stats_file: Optional[Path] = Option(
280+
_stats_file: Optional[Path] = Option(
282281
None,
283282
help=(
284283
"Statistics file (output of make-stats); default is src-stats.yaml if the "
@@ -287,7 +286,7 @@ def create_generators(
287286
show_default=False,
288287
dir_okay=False,
289288
),
290-
force: bool = Option(
289+
_force: bool = Option(
291290
False, "--force", "-f", help="Overwrite any existing Python generators file."
292291
),
293292
) -> None:

datafaker/make.py

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@
77
from datetime import datetime
88
from pathlib import Path
99
from types import TracebackType
10-
from typing import Any, Final, Optional, Tuple, Type, Union
10+
from typing import Any, Final, Optional, Tuple, Type
1111

1212
import pandas as pd
1313
import snsql
1414
import yaml
1515
from black import FileMode, format_str
1616
from jinja2 import Environment, FileSystemLoader, Template
1717
from mimesis.providers.base import BaseProvider
18-
from sqlalchemy import CursorResult, Engine, MetaData, UniqueConstraint, text
18+
from sqlalchemy import CursorResult, Engine, MetaData, text
1919
from sqlalchemy.dialects import postgresql
2020
from sqlalchemy.engine import Connection
2121
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
22-
from sqlalchemy.schema import Column, Table
22+
from sqlalchemy.schema import (
23+
Column,
24+
ColumnCollectionConstraint,
25+
PrimaryKeyConstraint,
26+
Table,
27+
UniqueConstraint,
28+
)
2329
from sqlalchemy.sql import Executable, sqltypes
2430
from typing_extensions import Self
2531

@@ -28,10 +34,12 @@
2834
from datafaker.settings import get_source_dsn, get_source_schema
2935
from datafaker.utils import (
3036
MaybeAsyncEngine,
37+
constraint_name,
3138
create_db_engine,
3239
download_table,
3340
get_columns_assigned,
3441
get_property,
42+
get_property_or_none,
3543
get_related_table_names,
3644
get_row_generators,
3745
get_sync_engine,
@@ -66,8 +74,8 @@ class FunctionCall:
6674
"""Which function to call with what."""
6775

6876
function_name: str
69-
args: list[Any]
70-
kwargs: dict[str, Any]
77+
args: Sequence[Any]
78+
kwargs: Mapping[str, Any]
7179

7280

7381
@dataclass
@@ -110,18 +118,6 @@ def make_column_choices(
110118
]
111119

112120

113-
@dataclass
114-
class _PrimaryConstraint:
115-
"""
116-
Describes a Uniqueness constraint for a multi-column primary key.
117-
118-
Not a real constraint, but enough to write df.py.
119-
"""
120-
121-
columns: list[Column]
122-
name: str
123-
124-
125121
@dataclass
126122
class TableGeneratorInfo:
127123
"""Contains the df.py content related to regular tables."""
@@ -132,7 +128,7 @@ class TableGeneratorInfo:
132128
column_choices: list[ColumnChoice]
133129
rows_per_pass: int
134130
row_gens: list[RowGeneratorInfo] = field(default_factory=list)
135-
unique_constraints: Sequence[Union[UniqueConstraint, _PrimaryConstraint]] = field(
131+
unique_constraints: Sequence[ColumnCollectionConstraint] = field(
136132
default_factory=list
137133
)
138134

@@ -467,19 +463,6 @@ def _get_provider_for_column(column: Column) -> Tuple[list[str], str, dict[str,
467463
return variable_names, generator_function, generator_arguments
468464

469465

470-
def _constraint_sort_key(constraint: UniqueConstraint) -> str:
471-
"""Extract a string out of a UniqueConstraint that is unique to that constraint.
472-
473-
We sort the constraints so that the output of make_tables is deterministic, this is
474-
the sort key.
475-
"""
476-
return (
477-
constraint.name
478-
if isinstance(constraint.name, str)
479-
else "_".join(map(str, constraint.columns))
480-
)
481-
482-
483466
def _get_generator_for_table(
484467
table_config: Mapping[str, Any],
485468
table: Table,
@@ -491,12 +474,12 @@ def _get_generator_for_table(
491474
for constraint in table.constraints
492475
if isinstance(constraint, UniqueConstraint)
493476
),
494-
key=_constraint_sort_key,
477+
key=constraint_name,
495478
)
496479
primary_keys = [c for c in table.columns if c.primary_key]
497-
constraints: Sequence[UniqueConstraint | _PrimaryConstraint] = unique_constraints
480+
constraints: Sequence[ColumnCollectionConstraint] = unique_constraints
498481
if 1 < len(primary_keys):
499-
primary_constraint = _PrimaryConstraint(
482+
primary_constraint = PrimaryKeyConstraint(
500483
columns=primary_keys, name=make_primary_key_name(table.name)
501484
)
502485
constraints = unique_constraints + [primary_constraint]
@@ -514,7 +497,7 @@ def _get_generator_for_table(
514497
class_name=table.name.title().replace(".", "") + "Generator",
515498
nonnull_columns=nonnull_columns,
516499
column_choices=column_choices,
517-
rows_per_pass=get_property(table_config, "num_rows_per_pass", int, 1),
500+
rows_per_pass=get_property(table_config, "num_rows_per_pass", 1),
518501
unique_constraints=constraints,
519502
)
520503

@@ -583,6 +566,7 @@ def make_vocabulary_tables(
583566

584567

585568
@dataclass
569+
# pylint: disable=too-many-instance-attributes
586570
class GenerationInfo:
587571
"""Information for the generation of all data."""
588572

@@ -618,14 +602,16 @@ def get_generation_info(
618602
619603
:return: A string that is a valid Python module, once written to file.
620604
"""
621-
row_generator_module_name = get_property(
622-
config, "row_generators_module", str | None, None
605+
row_generator_module_name = get_property_or_none(
606+
config, "row_generators_module", str
607+
)
608+
story_generator_module_name = get_property_or_none(
609+
config, "story_generators_module", str
623610
)
624-
story_generator_module_name = get_property(
625-
config, "story_generators_module", str | None, None
611+
object_instantiation: dict[str, Any] = get_property(
612+
config, "object_instantiation", {}
626613
)
627-
object_instantiation = get_property(config, "object_instantiation", dict, {})
628-
tables_config = get_property(config, "tables", dict, {})
614+
tables_config: dict[str, Any] = get_property(config, "tables", {})
629615

630616
tables: list[TableGeneratorInfo] = []
631617
vocabulary_tables: list[VocabularyTableGeneratorInfo] = []
@@ -654,8 +640,8 @@ def get_generation_info(
654640

655641
story_generators = _get_story_generators(config)
656642

657-
max_unique_constraint_tries = get_property(
658-
config, "max-unique-constraint-tries", int | None, None
643+
max_unique_constraint_tries = get_property_or_none(
644+
config, "max-unique-constraint-tries", int
659645
)
660646
return GenerationInfo(
661647
provider_imports=PROVIDER_IMPORTS,
@@ -724,7 +710,7 @@ def make_tables_file(
724710
if parquet_dir is not None:
725711
extra_meta = get_parquet_orm(parquet_dir)
726712
if extra_meta:
727-
md_tables = get_property(meta_dict, "tables", dict, {})
713+
md_tables: dict[str, Any] = get_property(meta_dict, "tables", {})
728714
new_tables = {**extra_meta, **md_tables}
729715
meta_dict["tables"] = new_tables
730716

0 commit comments

Comments
 (0)