Skip to content

Commit

Permalink
Merge pull request #7 from Tishka17/feature/ruff
Browse files Browse the repository at this point in the history
Add ruff and tests in CI
  • Loading branch information
Tishka17 authored Jan 22, 2024
2 parents 2ec63c2 + 7ffaf42 commit 51de844
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 24 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: CI

on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]

jobs:
cpython:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
python-version:
- "3.10"
- "3.11"
- "3.12"

steps:
- uses: actions/checkout@v3
- name: Set up ${{ matrix.python-version }} on ${{ matrix.os }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install . -r requirements_dev.txt
- name: Run ruff
run: |
ruff check .
- name: Run tests
run: |
pytest
26 changes: 26 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
line-length = 79
src = ["src"]
include = ["src/**.py", "tests/**.py"]

select = [
"E",
"F",
"BLE",
"B",
"COM",
"C4",
"ERA",
"T20",
"I",
"N",
"ASYNC",
"PIE",
"RUF",
]


[isort]
no-lines-before = ["local-folder"]

[flake8-tidy-imports]
ban-relative-imports = "parents"
3 changes: 3 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ruff
pytest
pytest-asyncio
14 changes: 7 additions & 7 deletions src/dishka/async_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ async def _get_unlocked(self, dependency_type: Type[T]) -> T:

async def close(self):
e = None
for exit in self.exits:
for exit_generator in self.exits:
try:
if exit.type is ProviderType.ASYNC_GENERATOR:
await anext(exit.callable)
elif exit.type is ProviderType.GENERATOR:
next(exit.callable)
if exit_generator.type is ProviderType.ASYNC_GENERATOR:
await anext(exit_generator.callable)
elif exit_generator.type is ProviderType.GENERATOR:
next(exit_generator.callable)
except StopIteration:
pass
except StopAsyncIteration:
pass
except Exception as err:
except Exception as err: # noqa: BLE001
e = err
if e:
raise e
Expand Down Expand Up @@ -150,5 +150,5 @@ def make_async_container(
for scope in scopes
]
return AsyncContextWrapper(
AsyncContainer(*registries, context=context, with_lock=with_lock)
AsyncContainer(*registries, context=context, with_lock=with_lock),
)
8 changes: 4 additions & 4 deletions src/dishka/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ def _get_unlocked(self, dependency_type: Type[T]) -> T:

def close(self):
e = None
for exit in self.exits:
for exit_generator in self.exits:
try:
next(exit)
next(exit_generator)
except StopIteration:
pass
except Exception as err:
except Exception as err: # noqa: BLE001
e = err
if e:
raise e
Expand Down Expand Up @@ -131,5 +131,5 @@ def make_container(
for scope in scopes
]
return ContextWrapper(
Container(*registries, context=context, with_lock=with_lock)
Container(*registries, context=context, with_lock=with_lock),
)
7 changes: 6 additions & 1 deletion src/dishka/inject.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from inspect import Parameter, Signature, signature
from typing import (
Annotated, Any, Callable, Sequence, get_args, get_origin,
Annotated,
Any,
Callable,
Sequence,
get_args,
get_origin,
get_type_hints,
)

Expand Down
17 changes: 13 additions & 4 deletions src/dishka/provider.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from collections.abc import AsyncIterable, Iterable
from enum import Enum
from inspect import (
isasyncgenfunction, isclass, iscoroutinefunction,
isasyncgenfunction,
isclass,
iscoroutinefunction,
isgeneratorfunction,
)
from typing import (
Any, Callable, Optional, Sequence, Type, Union, get_args,
get_origin, get_type_hints,
Any,
Callable,
Optional,
Sequence,
Type,
Union,
get_args,
get_origin,
get_type_hints,
)

from .scope import BaseScope
Expand All @@ -33,7 +42,7 @@ def __init__(
result_type: Type,
scope: Optional[BaseScope],
type: ProviderType,
is_to_bound: bool
is_to_bound: bool,
):
self.dependencies = dependencies
self.callable = callable
Expand Down
18 changes: 13 additions & 5 deletions tests/test_container.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import pytest

from dishka import (
Provider, Scope, make_async_container, make_container,
Provider,
Scope,
make_async_container,
make_container,
provide,
)
from .sample_providers import (
ClassA, async_func_a, async_gen_a, async_iter_a,
sync_func_a, sync_gen_a, sync_iter_a,
ClassA,
async_func_a,
async_gen_a,
async_iter_a,
sync_func_a,
sync_gen_a,
sync_iter_a,
)


Expand All @@ -16,7 +24,7 @@
(sync_func_a, False),
(sync_iter_a, True),
(sync_gen_a, True),
]
],
)
def test_sync(factory, closed):
class MyProvider(Provider):
Expand All @@ -42,7 +50,7 @@ def get_int(self) -> int:
(async_func_a, False),
(async_iter_a, True),
(async_gen_a, True),
]
],
)
@pytest.mark.asyncio
async def test_async(factory, closed):
Expand Down
11 changes: 8 additions & 3 deletions tests/test_privider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
from dishka import Provider, Scope, alias, provide
from dishka.provider import ProviderType
from .sample_providers import (
ClassA, async_func_a, async_gen_a, async_iter_a,
sync_func_a, sync_gen_a, sync_iter_a,
ClassA,
async_func_a,
async_gen_a,
async_iter_a,
sync_func_a,
sync_gen_a,
sync_iter_a,
)


Expand All @@ -31,7 +36,7 @@ def foo(self, x: bool) -> str:
(async_func_a, ProviderType.ASYNC_FACTORY, True),
(async_iter_a, ProviderType.ASYNC_GENERATOR, True),
(async_gen_a, ProviderType.ASYNC_GENERATOR, True),
]
],
)
def test_parse_provider(factory, provider_type, is_to_bound):
dep_provider = provide(factory, scope=Scope.REQUEST)
Expand Down

0 comments on commit 51de844

Please sign in to comment.