77from datetime import datetime
88from pathlib import Path
99from types import TracebackType
10- from typing import Any , Final , Optional , Tuple , Type , Union
10+ from typing import Any , Final , Optional , Tuple , Type
1111
1212import pandas as pd
1313import snsql
1414import yaml
1515from black import FileMode , format_str
1616from jinja2 import Environment , FileSystemLoader , Template
1717from mimesis .providers .base import BaseProvider
18- from sqlalchemy import CursorResult , Engine , MetaData , UniqueConstraint , text
18+ from sqlalchemy import CursorResult , Engine , MetaData , text
1919from sqlalchemy .dialects import postgresql
2020from sqlalchemy .engine import Connection
2121from 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+ )
2329from sqlalchemy .sql import Executable , sqltypes
2430from typing_extensions import Self
2531
2834from datafaker .settings import get_source_dsn , get_source_schema
2935from 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
126122class 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-
483466def _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
586570class 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