Skip to content

Commit

Permalink
📦 build: update dependencies and pre-commit (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilbottom authored Aug 31, 2024
1 parent 2f7e5ad commit c8c3a66
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 423 deletions.
67 changes: 7 additions & 60 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
ci:
autoupdate_schedule: quarterly
skip: ["poetry-lock", "sourcery", "pylint"]
skip: ["identity", "poetry-lock"]

repos:
- repo: meta
Expand All @@ -12,84 +12,31 @@ repos:
rev: v4.6.0
hooks:
- id: no-commit-to-branch
name: Don't allow commits to the main branch
args: ["--branch", "main"]
- id: trailing-whitespace
name: Remove trailing whitespace
- id: end-of-file-fixer
name: Ensure files end with a newline character
- id: mixed-line-ending
name: Align mixed line ending
- id: check-added-large-files
name: Check for large files
- id: check-json
name: Check JSON files are valid and parseable
- id: check-yaml
name: Check YAML files are valid and parseable
- id: check-toml
name: Check TOML files are valid and parseable
- id: check-ast
name: Validate Python

- repo: https://github.com/python-poetry/poetry
rev: 1.8.0
rev: 1.8.2
hooks:
- id: poetry-check
name: Check that the Poetry configuration is valid
- id: poetry-lock
name: Check that the lock file is up-to-date

# Formatting
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier
name: Make code pretty

- repo: https://github.com/asottile/pyupgrade
rev: v3.16.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.2
hooks:
- id: pyupgrade
args: ["--py311-plus"]
name: Align Python code to latest syntax

- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
name: Check Python formatting

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: Sort Python import statements
args: ["--filter-files"]

- repo: https://github.com/hadialqattan/pycln
rev: v2.4.0
hooks:
- id: pycln
name: Remove unused imports and variables
args: ["--config=pyproject.toml"]

# Sourcery (refactoring)
- repo: https://github.com/sourcery-ai/sourcery
rev: v1.18.1b30
hooks:
- id: sourcery
name: Check for refactoring opportunities
args: ["--diff=git diff HEAD", "--no-summary"]
files: src/.*
exclude: \.*/__init__\.py

# Final linting
- repo: local
hooks:
- id: pylint
name: Lint Python code with pylint
entry: pylint
language: system
types: ["python"]
args: ["-rn", "--rcfile=pyproject.toml"]
exclude: "^.*$" # Exclude everything for now
- id: ruff
args: ["--fix"]
- id: ruff-format
651 changes: 332 additions & 319 deletions poetry.lock

Large diffs are not rendered by default.

49 changes: 27 additions & 22 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ packages = [
[tool.poetry.dependencies]
python = "^3.11"
duckdb = "1.0.0"
pyodbc = "^5.1.0"
pyodbc = "^5.1.0" # SQL Server driver
psycopg2 = "^2.9.9" # PostgreSQL driver

[tool.poetry.group]
dev.optional = true
test.optional = true
docs.optional = true
ide.optional = true

[tool.poetry.group.dev.dependencies]
coverage-badge = "^1.1.0"
Expand All @@ -43,29 +43,34 @@ mkdocs-callouts = "^1.10.0"
markdown-callouts = "^0.3.0"
mdx-truly-sane-lists = "^1.3"

[tool.poetry.group.ide.dependencies]
black = "*"


[tool.pytest.ini_options]
addopts = "--cov=src --cov-report term-missing"
testpaths = [
"tests",
]
testpaths = ["tests"]


[tool.black]
target_version = ["py311"]
[tool.ruff]
line-length = 80


[tool.isort]
profile = "black"
line_length = 80


[tool.pylint.format]
max-line-length = 80

[tool.pylint.MASTER]
ignore-paths = "^tests/.*$"
indent-width = 4
target-version = "py311"

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[tool.ruff.lint]
select = ["F", "I", "N", "PL", "R", "RUF", "S", "UP", "W"]
ignore = []
fixable = ["ALL"]
unfixable = []
# Allow unused variables when underscore-prefixed
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# https://github.com/astral-sh/ruff/issues/4368
[tool.ruff.lint.extend-per-file-ignores]
"tests/**/*.py" = [
"S101", # Use of `assert` detected
"PLR2004", # Magic value used in comparison
]
28 changes: 18 additions & 10 deletions src/metabase/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def json(self) -> Any:
return output


def request(
def request( # noqa: PLR0913
url: str,
data: dict = None,
params: dict = None,
headers: dict = None,
data: dict | None = None,
params: dict | None = None,
headers: dict | None = None,
method: str = "GET",
data_as_json: bool = True,
error_count: int = 0,
Expand Down Expand Up @@ -76,12 +76,15 @@ def request(
else:
request_data = urllib.parse.urlencode(data).encode()

httprequest = urllib.request.Request(
url, data=request_data, headers=headers, method=method
httprequest = urllib.request.Request( # noqa: S310
url,
data=request_data,
headers=headers,
method=method,
)

try:
with urllib.request.urlopen(httprequest) as httpresponse:
with urllib.request.urlopen(httprequest) as httpresponse: # noqa: S310
response = Response(
headers=httpresponse.headers,
status=httpresponse.status,
Expand Down Expand Up @@ -129,7 +132,12 @@ class MetabaseConnector:
password: str
_token: str

def __init__(self, username: str, password: str, token: str = None) -> None:
def __init__(
self,
username: str,
password: str,
token: str | None = None,
) -> None:
self.username = username
self.password = password
self._token = token
Expand Down Expand Up @@ -157,7 +165,7 @@ def get(self, endpoint: str) -> Response:
headers=self.headers,
)

def post(self, endpoint: str, payload: dict = None) -> Response:
def post(self, endpoint: str, payload: dict | None = None) -> Response:
"""
Post to Metabase.
"""
Expand Down Expand Up @@ -201,7 +209,7 @@ def add_database(self, database: Database) -> None:
def main() -> None:
metabase = MetabaseConnector(
username=input("Metabase username: "),
password="Test@12345",
password="Test@12345", # noqa: S106
)
databases = tomllib.loads((SRC / "metabase/databases.toml").read_text())

Expand Down
24 changes: 15 additions & 9 deletions src/profiler/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import db_query_profiler
import duckdb
import psycopg2
import pyodbc

from src import SRC
Expand All @@ -19,7 +20,7 @@ def sqlite_connector() -> Connection:


def duckdb_connector() -> Connection:
return duckdb.connect(database=str(DB_PATH / "duckdb/loan.db")) # type: ignore
return duckdb.connect(database=str(DB_PATH / "duckdb/duckdb.db")) # type: ignore


def mssql_connector() -> Connection:
Expand All @@ -36,21 +37,26 @@ def mssql_connector() -> Connection:


def postgres_connector() -> Connection:
pass
# TODO: Grab from `src/metabase/databases.toml`
connection = psycopg2.connect(
"dbname=postgres user=postgres password=Test@12345"
)
return connection.cursor()


def main() -> None:
"""
Time the queries in the queries directory.
"""
db_conn = (
# sqlite_connector()
duckdb_connector()
# mssql_connector()
# postgres_connector()
)
db_conn = {
"sqlite": sqlite_connector,
"duckdb": duckdb_connector,
"mssql": mssql_connector,
"postgres": postgres_connector,
}["postgres"]

db_query_profiler.time_queries(
conn=db_conn,
conn=db_conn(),
repeat=1_000,
directory=SRC / "profiler/queries",
)
Expand Down
4 changes: 2 additions & 2 deletions src/resources/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Resource:
database_file: pathlib.Path | None
skip: bool

def __init__(
def __init__( # noqa: PLR0913
self,
type_: DatabaseType,
name: str,
Expand Down Expand Up @@ -172,7 +172,7 @@ def get_resource(self) -> None:
Download the resource.
"""
logging.info(f"Downloading from '{self.url}'...")
urllib.request.urlretrieve(self.url, self.destination)
urllib.request.urlretrieve(self.url, self.destination) # noqa: S310
if self.destination.suffix == ".gz":
_unzip_file(self.destination)

Expand Down
2 changes: 1 addition & 1 deletion tests/test__resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test__resource__get_resource(
"""
import urllib.request

def mock_urlretrieve(url, filename): # noqa
def mock_urlretrieve(url, filename):
"""
Mock the ``urlretrieve`` function, which downloads a file from ``url``
and saves it to ``filename``.
Expand Down

0 comments on commit c8c3a66

Please sign in to comment.