Skip to content

Commit

Permalink
Merge branch 'master' into double-pluralization-of-names
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm authored Jan 23, 2025
2 parents ba86c8a + e34718c commit 7954f3d
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Create packages
run: python -m build
- name: Archive packages
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: dist
path: dist
Expand All @@ -38,7 +38,7 @@ jobs:
id-token: write
steps:
- name: Retrieve packages
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
- name: Upload packages
uses: pypa/gh-action-pypi-publish@release/v1

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# * Run "pre-commit install".
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-toml
- id: check-yaml
Expand All @@ -16,14 +16,14 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.4
rev: v0.9.2
hooks:
- id: ruff
args: [--fix, --show-fixes]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.0
rev: v1.14.1
hooks:
- id: mypy
additional_dependencies:
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Version history
===============

**UNRELEASED**

- Dropped support for Python 3.8

**3.0.0rc5**

- Fixed pgvector support not working
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ values must be delimited by commas, e.g. ``--options noconstraints,nobidi``):

* all the options from ``declarative``

* ``sqlmodel``
* ``sqlmodels``

* all the options from ``declarative``

Expand Down
29 changes: 15 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ classifiers = [
"Topic :: Software Development :: Code Generators",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
requires-python = ">=3.8"
requires-python = ">=3.9"
dependencies = [
"SQLAlchemy >= 2.0.23",
"inflect >= 4.0.0",
Expand All @@ -41,12 +41,13 @@ dynamic = ["version"]

[project.optional-dependencies]
test = [
"sqlacodegen[sqlmodel]",
"pytest >= 7.4",
"coverage >= 7",
"psycopg2-binary",
"mysql-connector-python",
]
sqlmodel = ["sqlmodel >= 0.0.12"]
sqlmodel = ["sqlmodel >= 0.0.22"]
citext = ["sqlalchemy-citext >= 1.7.0"]
geoalchemy2 = ["geoalchemy2 >= 0.11.1"]
pgvector = ["pgvector >= 0.2.4"]
Expand All @@ -65,21 +66,24 @@ version_scheme = "post-release"
local_scheme = "dirty-tag"

[tool.ruff]
select = [
"E", "F", "W", # default Flake8
src = ["src"]

[tool.ruff.lint]
extend-select = [
"I", # isort
"ISC", # flake8-implicit-str-concat
"PGH", # pygrep-hooks
"RUF100", # unused noqa (yesqa)
"UP", # pyupgrade
"W", # pycodestyle warnings
]
src = ["src"]

[tool.mypy]
strict = true

[tool.pytest.ini_options]
addopts = "-rsx --tb=short"
addopts = "-rsfE --tb=short"
testpaths = ["tests"]

[coverage.run]
Expand All @@ -90,13 +94,10 @@ relative_files = true
show_missing = true

[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py38, py39, py310, py311, py312
env_list = ["py39", "py310", "py311", "py312", "py313"]
skip_missing_interpreters = true
minversion = 4.0.0

[testenv]
extras = test
commands = python -m pytest {posargs}
"""
[tool.tox.env_run_base]
package = "editable"
commands = [["python", "-m", "pytest", { replace = "posargs", extend = true }]]
extras = ["test"]
20 changes: 17 additions & 3 deletions src/sqlacodegen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def main() -> None:
parser.add_argument(
"--tables", help="tables to process (comma-delimited, default: all)"
)
parser.add_argument("--noviews", action="store_true", help="ignore views")
parser.add_argument(
"--noviews",
action="store_true",
help="ignore views (always true for sqlmodels generator)",
)
parser.add_argument("--outfile", help="file to write output to (default: stdout)")
args = parser.parse_args()

Expand Down Expand Up @@ -81,13 +85,23 @@ def main() -> None:
tables = args.tables.split(",") if args.tables else None
schemas = args.schemas.split(",") if args.schemas else [None]
options = set(args.options.split(",")) if args.options else set()
for schema in schemas:
metadata.reflect(engine, schema, not args.noviews, tables)

# Instantiate the generator
generator_class = generators[args.generator].load()
generator = generator_class(metadata, engine, options)

if not generator.views_supported:
name = generator_class.__name__
print(
f"VIEW models will not be generated when using the '{name}' generator",
file=sys.stderr,
)

for schema in schemas:
metadata.reflect(
engine, schema, (generator.views_supported and not args.noviews), tables
)

# Open the target file (if given)
with ExitStack() as stack:
outfile: TextIO
Expand Down
25 changes: 21 additions & 4 deletions src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def __init__(
if invalid_options:
raise ValueError("Unrecognized options: " + ", ".join(invalid_options))

@property
@abstractmethod
def views_supported(self) -> bool:
pass

@abstractmethod
def generate(self) -> str:
"""
Expand Down Expand Up @@ -134,6 +139,10 @@ def __init__(
self.imports: dict[str, set[str]] = defaultdict(set)
self.module_imports: set[str] = set()

@property
def views_supported(self) -> bool:
return True

def generate_base(self) -> None:
self.base = Base(
literal_imports=[LiteralImport("sqlalchemy", "MetaData")],
Expand Down Expand Up @@ -482,6 +491,9 @@ def render_column(
if comment:
kwargs["comment"] = repr(comment)

return self.render_column_callable(is_table, *args, **kwargs)

def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> str:
if is_table:
self.add_import(Column)
return render_callable("Column", *args, kwargs=kwargs)
Expand Down Expand Up @@ -1334,10 +1346,7 @@ def generate_base(self) -> None:
LiteralImport("sqlalchemy.orm", "MappedAsDataclass"),
],
declarations=[
(
f"class {self.base_class_name}(MappedAsDataclass, "
"DeclarativeBase):"
),
(f"class {self.base_class_name}(MappedAsDataclass, DeclarativeBase):"),
f"{self.indentation}pass",
],
metadata_ref=f"{self.base_class_name}.metadata",
Expand All @@ -1362,6 +1371,14 @@ def __init__(
base_class_name=base_class_name,
)

@property
def views_supported(self) -> bool:
return False

def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> str:
self.add_import(Column)
return render_callable("Column", *args, kwargs=kwargs)

def generate_base(self) -> None:
self.base = Base(
literal_imports=[],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_cli_sqlmodels(db_path: Path, tmp_path: Path) -> None:
class Foo(SQLModel, table=True):
id: Optional[int] = Field(default=None, sa_column=Column('id', Integer, \
primary_key=True))
name: str = Field(sa_column=Column('name', Text, nullable=False))
name: str = Field(sa_column=Column('name', Text))
"""
)

Expand Down

0 comments on commit 7954f3d

Please sign in to comment.