Skip to content

Update linter and add type checker #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]
experimental: [false]
include:
- python-version: "3.12-dev"
os: ubuntu-latest
experimental: true

steps:
- name: Checkout sources
Expand All @@ -33,6 +38,7 @@ jobs:

- name: Run tox
run: tox
continue-on-error: ${{ matrix.experimental }}

- name: upload coverage to Codecov
if: "matrix.python-version == '3.11'"
Expand Down
26 changes: 10 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
repos:
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.3.0
hooks:
- id: black
args: [--safe, --line-length=120]
args: [--safe]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
Expand All @@ -13,19 +13,13 @@ repos:
- id: check-added-large-files
- id: debug-statements
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.269
hooks:
- id: flake8
args: [--max-line-length=120]
language_version: python3
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
hooks:
- id: reorder-python-imports
args: [--application-directories=.src/, --py310-plus]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
- id: ruff
args: [--fix]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
hooks:
- id: pyupgrade
args: [--py311-plus]
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
34 changes: 34 additions & 0 deletions changelog.d/20230130_102418_ryuusuke.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.. A new scriv changelog fragment.
..
.. Uncomment the header that is right (remove the leading dots).
..
.. Removed
.. -------
..
.. - A bullet item for the Removed category.
..
Added
-----

- Added mypy type checker

Changed
-------

- Changed linting to ruff

.. Deprecated
.. ----------
..
.. - A bullet item for the Deprecated category.
..
.. Fixed
.. -----
..
.. - A bullet item for the Fixed category.
..
.. Security
.. --------
..
.. - A bullet item for the Security category.
..
34 changes: 34 additions & 0 deletions changelog.d/20230130_111713_ryuusuke_infra_update.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.. A new scriv changelog fragment.
..
.. Uncomment the header that is right (remove the leading dots).
..
.. Removed
.. -------
..
.. - A bullet item for the Removed category.
..
Added
-----

- Added support for python 3.12-dev
..
.. Changed
.. -------
..
.. - A bullet item for the Changed category.
..
.. Deprecated
.. ----------
..
.. - A bullet item for the Deprecated category.
..
.. Fixed
.. -----
..
.. - A bullet item for the Fixed category.
..
.. Security
.. --------
..
.. - A bullet item for the Security category.
..
34 changes: 34 additions & 0 deletions changelog.d/20230130_120134_ryuusuke_infra_update.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.. A new scriv changelog fragment.
..
.. Uncomment the header that is right (remove the leading dots).
..
.. Removed
.. -------
..
.. - A bullet item for the Removed category.
..
.. Added
.. -----
..
.. - A bullet item for the Added category.
..
Changed
-------

- query() and execute() will raise ConnectionError in case they are run without active connection.
..
.. Deprecated
.. ----------
..
.. - A bullet item for the Deprecated category.
..
.. Fixed
.. -----
..
.. - A bullet item for the Fixed category.
..
.. Security
.. --------
..
.. - A bullet item for the Security category.
..
73 changes: 72 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mysql-context-manager"
version = "0.1.6"
version = "0.1.5"
description = "Work with MySQL databases asynchronously, and in context."
license = "MIT"
authors = ["IdoKendo <[email protected]>"]
Expand All @@ -25,11 +25,37 @@ tox = "^4.5"
pytest-asyncio = "^0.21"
pytest-mock = "^3.10"
pytest-cov = "^4.0"
mypy = "^1.3"

[tool.pytest.ini_options]
addopts = "--cov=mysql_context_manager --cov-report xml"
testpaths = ["tests"]

[tool.ruff]
exclude = [".tox"]
target-version = "py38"
select = [
"A", # builtins
"ARG", # unsued arguments
"B", # bugbear
"C4", # comprehensions
"C90", # mccabe
"COM", # commas
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"N", # pep8-naming
"PT", # pytest style
"RUF", # ruff
"SIM", # simplify
"TID", # tidy imports
"UP", # pyupgrade
"W", # warnings
]

[tool.ruff.isort]
force-single-line = true

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
35 changes: 22 additions & 13 deletions src/mysql_context_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

__version__ = "0.1.6"

import contextlib
import json
from json import JSONDecodeError
from typing import Any
Expand Down Expand Up @@ -32,7 +33,7 @@ def __init__(
if schema is None:
schema = ""
self.connection_string = f"mysql://{credentials}@{hostname}:{port}/{schema}"
self.connection = None
self.connection: databases.Database | None = None
self.engine = None

async def __aenter__(self):
Expand All @@ -53,8 +54,8 @@ async def disconnect(self) -> None:
async def connect(self) -> None:
"""Establishes the connection to the database"""
self.connection = databases.Database(self.connection_string)

await self.connection.connect()
if self.connection is not None:
await self.connection.connect()

async def query(self, sql_query: str, **kwargs) -> list[dict[str, Any]]:
"""Queries the database
Expand All @@ -65,21 +66,24 @@ async def query(self, sql_query: str, **kwargs) -> list[dict[str, Any]]:
Returns:
list[dict[str, Any]]: List of rows represented in dictioary format

Raises:
ConnectionError: in case of method call before running connect()

Examples:
>>> query = "select username, element from users where team_name = :team_name limit 2;"
>>> query = "select name, elem from users where team = :team limit 2;"
>>> async with MysqlConnector(hostname="localhost") as conn:
>>> print(await conn.query(query, team_name="Team Avatar"))
[{"username": "Katara", "element": "water"}, {"username": "Toph", "element": "earth"}]
>>> print(await conn.query(query, team="Team Avatar"))
[{"name": "Katara", "elem": "water"}, {"name": "Toph", "elem": "earth"}]

"""
result = await self.connection.fetch_all(query=sql_query, values=kwargs)
result = [dict(i) for i in result]
if self.connection is None:
raise ConnectionError("No active connection")
records = await self.connection.fetch_all(query=sql_query, values=kwargs)
result = [dict(i) for i in records]
for res in result:
for key, val in res.items():
try:
with contextlib.suppress(JSONDecodeError, TypeError):
res[key] = json.loads(val)
except (JSONDecodeError, TypeError):
pass
return result

async def execute(self, sql_query: str, **kwargs) -> None:
Expand All @@ -88,9 +92,14 @@ async def execute(self, sql_query: str, **kwargs) -> None:
Args:
sql_query (str): SQL query, with placeholders as :placeholder

Raises:
ConnectionError: in case of method call before running connect()

Examples:
>>> query = "update users set username = :username where user_id = :user_id;"
>>> query = "update users set name = :name where user_id = :user_id;"
>>> async with MysqlConnector(hostname="localhost") as conn:
>>> await conn.query(query, username="Truth", user_id=42)
>>> await conn.query(query, name="Truth", user_id=42)
"""
if self.connection is None:
raise ConnectionError("No active connection")
await self.connection.execute(query=sql_query, values=kwargs)
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@


@pytest.fixture()
def mock_db(mocker):
def _mock_db(mocker):
mocker.patch("databases.Database.connect", return_value=None)
mocker.patch("databases.Database.disconnect", return_value=None)
mocker.patch(
"databases.Database.fetch_all",
return_value=[
[("username", "Aang")],
[("name", "Aang")],
],
)
mocker.patch("databases.Database.execute", return_value=None)
Loading