ruff banned imports
I was thinking about SQLAlchemy and how it has specific features for different backends. PostgresSQL has JSON support, for example, but Sqlite doesn't. So how might you make sure that your SQLAlchemy code only uses models that can be used across different backends?
Although it's probably not perfect, I did learn about banned imports via ruff. You can do something like this:
# pyproject.toml
[tool.ruff]
select = ["F", "E", "W", "TID"] # TID enables banned imports
[tool.ruff.lint.flake8-tidy-imports]
banned-module-level-imports = [
"sqlalchemy.dialects.postgresql",
"sqlalchemy.dialects.mysql",
"sqlalchemy.dialects.oracle",
"sqlalchemy.dialects.mssql",
"sqlalchemy.dialects.sqlite",
]
# Or ban the entire dialects module
[tool.ruff.lint.flake8-tidy-imports.banned-api]
"sqlalchemy.dialects".msg = "Use only portable SQLAlchemy types, not dialect-specific ones"
This way, ruff
will complain every time that you import from the dialects
submodule of sqlalchemy.