Skip to content

Commit fbd4d11

Browse files
committed
Added pflake8 pre-commit hook and wrapped code to 88 columns
1 parent c673bb2 commit fbd4d11

File tree

5 files changed

+122
-78
lines changed

5 files changed

+122
-78
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ repos:
3131
hooks:
3232
- id: isort
3333

34+
- repo: https://github.com/csachs/pyproject-flake8
35+
rev: v0.0.1a4
36+
hooks:
37+
- id: pyproject-flake8
38+
3439
- repo: https://github.com/pre-commit/mirrors-mypy
3540
rev: v0.942
3641
hooks:

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ skip_gitignore = true
6565
profile = "black"
6666

6767
[tool.flake8]
68-
max-line-length = 99
69-
70-
[tool.autopep8]
71-
max_line_length = 99
68+
max-line-length = 88
7269

7370
[tool.mypy]
7471
python_version = "3.7"

src/sqlacodegen/generators.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def generate(self) -> str:
142142
if "nocomments" in self.options:
143143
column.comment = None
144144

145-
# Use information from column constraints to figure out the intended column types
145+
# Use information from column constraints to figure out the intended column
146+
# types
146147
for table in self.metadata.tables.values():
147148
self.fix_column_types(table)
148149

@@ -233,9 +234,10 @@ def add_import(self, obj: Any) -> None:
233234
type_ = type(obj) if not isinstance(obj, type) else obj
234235
pkgname = type_.__module__
235236

236-
# The column types have already been adapted towards generic types if possible, so if this
237-
# is still a vendor specific type (e.g., MySQL INTEGER) be sure to use that rather than the
238-
# generic sqlalchemy type as it might have different constructor parameters.
237+
# The column types have already been adapted towards generic types if possible,
238+
# so if this is still a vendor specific type (e.g., MySQL INTEGER) be sure to
239+
# use that rather than the generic sqlalchemy type as it might have different
240+
# constructor parameters.
239241
if pkgname.startswith("sqlalchemy.dialects."):
240242
dialect_pkgname = ".".join(pkgname.split(".")[0:3])
241243
dialect_pkg = import_module(dialect_pkgname)
@@ -316,7 +318,8 @@ def render_table(self, table: Table) -> str:
316318
args: list[str] = [f"{table.name!r}, metadata"]
317319
kwargs: dict[str, object] = {}
318320
for column in table.columns:
319-
# Cast is required because of a bug in the SQLAlchemy stubs regarding Table.columns
321+
# Cast is required because of a bug in the SQLAlchemy stubs regarding
322+
# Table.columns
320323
args.append(self.render_column(column, True))
321324

322325
for constraint in sorted(table.constraints, key=get_constraint_sort_key):
@@ -383,8 +386,8 @@ def render_column(self, column: Column[Any], show_name: bool) -> str:
383386
if show_name:
384387
args.append(repr(column.name))
385388

386-
# Render the column type if there are no foreign keys on it or any of them points back to
387-
# itself
389+
# Render the column type if there are no foreign keys on it or any of them
390+
# points back to itself
388391
if not dedicated_fks or any(fk.column is column for fk in dedicated_fks):
389392
args.append(self.render_column_type(column.type))
390393

@@ -522,13 +525,17 @@ def add_fk_options(*opts: Any) -> None:
522525
return render_callable(constraint.__class__.__name__, *args, kwargs=kwargs)
523526

524527
def should_ignore_table(self, table: Table) -> bool:
525-
# Support for Alembic and sqlalchemy-migrate -- never expose the schema version tables
528+
# Support for Alembic and sqlalchemy-migrate -- never expose the schema version
529+
# tables
526530
return table.name in ("alembic_version", "migrate_version")
527531

528532
def find_free_name(
529533
self, name: str, global_names: set[str], local_names: Collection[str] = ()
530534
) -> str:
531-
"""Generate an attribute name that does not clash with other local or global names."""
535+
"""
536+
Generate an attribute name that does not clash with other local or global names.
537+
538+
"""
532539
name = name.strip()
533540
assert name, "Identifier cannot be empty"
534541
name = _re_invalid_identifier.sub("_", name)
@@ -611,7 +618,8 @@ def get_adapted_type(self, coltype: Any) -> Any:
611618
if not supercls.__name__.startswith("_") and hasattr(
612619
supercls, "__visit_name__"
613620
):
614-
# Hack to fix adaptation of the Enum class which is broken since SQLAlchemy 1.2
621+
# Hack to fix adaptation of the Enum class which is broken since
622+
# SQLAlchemy 1.2
615623
kw = {}
616624
if supercls is Enum:
617625
kw["name"] = coltype.name
@@ -629,12 +637,12 @@ def get_adapted_type(self, coltype: Any) -> Any:
629637
new_coltype.item_type = self.get_adapted_type(new_coltype.item_type)
630638

631639
try:
632-
# If the adapted column type does not render the same as the original, don't
633-
# substitute it
640+
# If the adapted column type does not render the same as the
641+
# original, don't substitute it
634642
if new_coltype.compile(self.bind.engine.dialect) != compiled_type:
635-
# Make an exception to the rule for Float and arrays of Float, since at
636-
# least on PostgreSQL, Float can accurately represent both REAL and
637-
# DOUBLE_PRECISION
643+
# Make an exception to the rule for Float and arrays of Float,
644+
# since at least on PostgreSQL, Float can accurately represent
645+
# both REAL and DOUBLE_PRECISION
638646
if not isinstance(new_coltype, Float) and not (
639647
isinstance(new_coltype, ARRAY)
640648
and isinstance(new_coltype.item_type, Float)
@@ -692,13 +700,14 @@ def collect_imports_for_model(self, model: Model) -> None:
692700
def generate_models(self) -> list[Model]:
693701
models_by_table_name: dict[str, Model] = {}
694702

695-
# Pick association tables from the metadata into their own set, don't process them normally
703+
# Pick association tables from the metadata into their own set, don't process
704+
# them normally
696705
links: defaultdict[str, list[Model]] = defaultdict(lambda: [])
697706
for table in self.metadata.sorted_tables:
698707
qualified_name = qualified_table_name(table)
699708

700-
# Link tables have exactly two foreign key constraints and all columns are involved in
701-
# them
709+
# Link tables have exactly two foreign key constraints and all columns are
710+
# involved in them
702711
fk_constraints = sorted(
703712
table.foreign_key_constraints, key=get_constraint_sort_key
704713
)
@@ -710,8 +719,8 @@ def generate_models(self) -> list[Model]:
710719
links[tablename].append(model)
711720
continue
712721

713-
# Only form model classes for tables that have a primary key and are not association
714-
# tables
722+
# Only form model classes for tables that have a primary key and are not
723+
# association tables
715724
if not table.primary_key:
716725
models_by_table_name[qualified_name] = Model(table)
717726
else:
@@ -749,7 +758,8 @@ def generate_models(self) -> list[Model]:
749758
# Collect the imports
750759
self.collect_imports(models_by_table_name.values())
751760

752-
# Rename models and their attributes that conflict with imports or other attributes
761+
# Rename models and their attributes that conflict with imports or other
762+
# attributes
753763
global_names = {
754764
name for namespace in self.imports.values() for name in namespace
755765
}
@@ -809,7 +819,8 @@ def generate_relationships(
809819
]
810820

811821
# If the two tables share more than one foreign key constraint,
812-
# SQLAlchemy needs an explicit primaryjoin to figure out which column(s) it needs
822+
# SQLAlchemy needs an explicit primaryjoin to figure out which column(s)
823+
# it needs
813824
common_fk_constraints = get_common_fk_constraints(
814825
source.table, target.table
815826
)
@@ -875,7 +886,8 @@ def generate_relationships(
875886
relationship.backref = reverse_relationship
876887
target.relationships.append(reverse_relationship)
877888

878-
# Add a primary/secondary join for self-referential many-to-many relationships
889+
# Add a primary/secondary join for self-referential many-to-many
890+
# relationships
879891
if source is target:
880892
both_relationships = [relationship]
881893
reverse_flags = [False, True]
@@ -973,8 +985,8 @@ def generate_relationship_name(
973985
else:
974986
preferred_name = relationship.target.table.name
975987

976-
# If there's a constraint with a single column that ends with "_id", use the preceding
977-
# part as the relationship name
988+
# If there's a constraint with a single column that ends with "_id", use the
989+
# preceding part as the relationship name
978990
if relationship.constraint:
979991
is_source = relationship.source.table is relationship.constraint.table
980992
if is_source or relationship.type not in (

src/sqlacodegen/utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def get_compiled_expression(statement: ClauseElement, bind: Connectable) -> str:
3636
def get_common_fk_constraints(
3737
table1: Table, table2: Table
3838
) -> set[ForeignKeyConstraint]:
39-
"""Return a set of foreign key constraints the two tables have against each other."""
39+
"""
40+
Return a set of foreign key constraints the two tables have against each other.
41+
42+
"""
4043
c1 = {
4144
c
4245
for c in table1.constraints
@@ -67,7 +70,9 @@ def uses_default_name(constraint: Constraint | Index) -> bool:
6770
"column_0_N_label": "_".join(
6871
col.label(col.name).name for col in constraint.columns
6972
),
70-
"column_0N_key": "".join(col.key for col in constraint.columns), # type: ignore[misc]
73+
"column_0N_key": "".join(
74+
col.key for col in constraint.columns # type: ignore[misc]
75+
),
7176
"column_0_N_key": "_".join(
7277
col.key for col in constraint.columns # type: ignore[misc]
7378
),
@@ -144,8 +149,8 @@ def render_callable(
144149
:param name: name of the callable
145150
:param args: positional arguments
146151
:param kwargs: keyword arguments
147-
:param indentation: if given, each argument will be rendered on its own line with this value
148-
used as the indentation
152+
:param indentation: if given, each argument will be rendered on its own line with
153+
this value used as the indentation
149154
150155
"""
151156
if kwargs:

0 commit comments

Comments
 (0)