Skip to content
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

Update linter and add type checker #4

Merged
merged 7 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ on:
jobs:
build:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
strategy:
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 @@ -38,4 +44,5 @@ jobs:
if: "matrix.python-version == '3.11'"
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
24 changes: 9 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ repos:
rev: 22.12.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.224
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: [--force-exclude, --fix]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
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.
..
1,082 changes: 626 additions & 456 deletions poetry.lock

Large diffs are not rendered by default.

21 changes: 15 additions & 6 deletions 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 <ryuusuke@gmail.com>"]
Expand All @@ -10,26 +10,35 @@ repository = "https://github.com/IdoKendo/mysql-context-manager"

[tool.poetry.dependencies]
python = "^3.8"
databases = "^0.6.2"
databases = "^0.7.0"
PyMySQL = "^1.0.2"
aiomysql = "^0.1.1"

[tool.poetry.group.dev]
optional = true

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
pre-commit = "^2.20.0"
scriv = { extras = ["toml"], version = "^1.0.0" }
tox = "^4.0.8"
pytest = "^7.2.1"
pre-commit = "^3.0.2"
scriv = { extras = ["toml"], version = "^1.2.0" }
tox = "^4.4.2"
pytest-asyncio = "^0.20.3"
pytest-mock = "^3.10.0"
pytest-cov = "^4.0.0"
mypy = "^0.991"

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

[tool.ruff]
exclude = [".tox"]
select = ["E", "F", "W", "B", "I", "UP", "C4", "TID"]
target-version = "py311"

[tool.roff.isort]
IdoKendo marked this conversation as resolved.
Show resolved Hide resolved
force-single-line = true

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
30 changes: 20 additions & 10 deletions src/mysql_context_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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 +53,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,15 +65,20 @@ 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:
Expand All @@ -88,9 +93,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)
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def mock_db(mocker):
mocker.patch(
"databases.Database.fetch_all",
return_value=[
[("username", "Aang")],
[("name", "Aang")],
],
)
mocker.patch("databases.Database.execute", return_value=None)
29 changes: 24 additions & 5 deletions tests/test_mysql_context_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from mysql_context_manager import __version__
from mysql_context_manager import MysqlConnector
from mysql_context_manager import MysqlConnector, __version__


def test_version():
Expand Down Expand Up @@ -29,12 +28,32 @@ async def test_false_disconnect():
async def test_query(mock_db):
connector = MysqlConnector(hostname="localhost", schema="team_avatar")
async with connector as conn:
result = await conn.query("select username from users;")
assert result[0]["username"] == "Aang"
result = await conn.query("select name from users;")
assert result[0]["name"] == "Aang"


@pytest.mark.asyncio
async def test_execute(mock_db):
connector = MysqlConnector(hostname="localhost", schema="team_avatar")
async with connector as conn:
await conn.execute("update users set avatar_state = 1 where username = :username;", username="Aang")
await conn.execute(
"update users set avatar_state = 1 where name = :name;",
name="Aang",
)


@pytest.mark.asyncio
async def test_query_without_connection(mock_db):
connector = MysqlConnector(hostname="localhost", schema="team_avatar")
with pytest.raises(ConnectionError):
await connector.query("select name from users;")


@pytest.mark.asyncio
async def test_execute_without_connection(mock_db):
connector = MysqlConnector(hostname="localhost", schema="team_avatar")
with pytest.raises(ConnectionError):
await connector.execute(
"update users set avatar_state = 1 where name = :name;",
name="Aang",
)
12 changes: 7 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ envlist =
py39,
py310,
py311,
py312,
lint,

[testenv]
Expand All @@ -20,16 +21,17 @@ commands =
[testenv:lint]
deps =
black
flake8
pylint
ruff
mypy
commands =
pylint src/
black --line-length=120 --check src/
flake8 --max-line-length=120 src/
black --check src/
ruff --force-exclude src/
mypy --no-strict-optional --ignore-missing-imports src/

[gh-actions]
python =
3.8: py38
3.9: py39
3.10: py310
3.11: py311, lint
3.12-dev: py312