Skip to content

Commit

Permalink
Add black back in
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkandersson committed Aug 16, 2020
1 parent 6491aa3 commit d77f896
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes

## Version _next_

- Fix bug where iterating over constructable schemas did not handle some exceptions
- Add black dependency back in

## Version 1.4.1 - 2020-08-09

- Remove black dependency
Expand Down
1 change: 1 addition & 0 deletions open_alchemy/facades/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Facades for ringfenced modules."""
# pylint: disable=useless-import-alias

from . import code_formatter as code_formatter
from . import jsonschema as jsonschema
from . import models as models
from . import sqlalchemy as sqlalchemy
22 changes: 22 additions & 0 deletions open_alchemy/facades/code_formatter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Apply automatic code formatting to source code."""

import black


def apply(*, source: str) -> str:
"""
Apply automatic code formatting to source code.
Args:
source: The source code.
Returns:
The formatted source code.
"""
try:
return black.format_file_contents(
src_contents=source, fast=False, mode=black.FileMode()
)
except black.NothingChanged:
return source
3 changes: 2 additions & 1 deletion open_alchemy/models_file/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ def generate_models(self) -> str:
model_sources.append(model_source)

# Generate source code for models file
return _models.generate(models=model_sources)
raw_source = _models.generate(models=model_sources)
return facades.code_formatter.apply(source=raw_source)
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ markers =
init
slow
schemas
code_formatter
python_functions = test_*
mocked-sessions = examples.app.database.db.session

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"jsonschema>=3",
"Jinja2>=2",
"sqlalchemy-stubs>=0.3",
"black",
],
include_package_data=True,
extras_require={
Expand Down
25 changes: 25 additions & 0 deletions tests/open_alchemy/facades/test_code_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Tests for code formatting."""

import pytest

from open_alchemy import facades


@pytest.mark.parametrize(
"source, expected_source",
[
pytest.param("'test'\n", '"test"\n', id="apply formatting"),
pytest.param('"test"\n', '"test"\n', id="no formatting"),
],
)
@pytest.mark.facade
@pytest.mark.code_formatter
def test_apply(source, expected_source):
"""
GIVEN source code and expected source code
WHEN apply is called with the source code
THEN the expected source code is returned.
"""
returned_source = facades.code_formatter.apply(source=source)

assert returned_source == expected_source
2 changes: 1 addition & 1 deletion tests/open_alchemy/integration/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class TTable({expected_model_base}):
query: orm.Query
# Model properties
column: 'sqlalchemy.Column[typing.Optional[int]]'
column: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, column: typing.Optional[int] = None) -> None:
"""
Expand Down
28 changes: 16 additions & 12 deletions tests/open_alchemy/models_file/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Tests for models file."""

# pylint: disable=line-too-long

import sys

import pytest
Expand Down Expand Up @@ -59,7 +57,7 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down Expand Up @@ -157,7 +155,7 @@ class TModel1({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down Expand Up @@ -239,7 +237,7 @@ class TModel2({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[str]]'
id: "sqlalchemy.Column[typing.Optional[str]]"
def __init__(self, id: typing.Optional[str] = None) -> None:
"""
Expand Down Expand Up @@ -366,7 +364,7 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down Expand Up @@ -480,10 +478,12 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
name: 'sqlalchemy.Column[typing.Optional[str]]'
id: 'sqlalchemy.Column[typing.Optional[int]]'
name: "sqlalchemy.Column[typing.Optional[str]]"
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, name: typing.Optional[str] = None, id: typing.Optional[int] = None) -> None:
def __init__(
self, name: typing.Optional[str] = None, id: typing.Optional[int] = None
) -> None:
"""
Construct.
Expand All @@ -495,7 +495,9 @@ def __init__(self, name: typing.Optional[str] = None, id: typing.Optional[int] =
...
@classmethod
def from_dict(cls, name: typing.Optional[str] = None, id: typing.Optional[int] = None) -> "TModel":
def from_dict(
cls, name: typing.Optional[str] = None, id: typing.Optional[int] = None
) -> "TModel":
"""
Construct from a dictionary (eg. a POST payload).
Expand Down Expand Up @@ -542,7 +544,9 @@ def to_str(self) -> str:
Model: typing.Type[TModel] = models.Model # type: ignore
''' # noqa: E501
'''
print(repr(source))
print(repr(expected_source))
assert source == expected_source


Expand Down Expand Up @@ -593,7 +597,7 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down

0 comments on commit d77f896

Please sign in to comment.