Skip to content

Commit

Permalink
Enable subset of lint rules for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Apr 6, 2024
1 parent ebbf06a commit b2f9383
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ check-fmt:
.venv/bin/ruff format . --check

check-ruff:
.venv/bin/ruff check wireup $(RUFF_ARGS)
.venv/bin/ruff check wireup test $(RUFF_ARGS)

check-mypy:
.venv/bin/mypy wireup --strict
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ lint.ignore = [
"COM812",
"ISC001"
]
[tool.ruff.lint.per-file-ignores]
"test/*" = ["D", "ANN", "PT", "SLF001", "T201", "EM101", "TRY", "FA100", "B008", "RUF009", "F401", "SIM117"]


[build-system]
requires = ["poetry-core"]
Expand Down
7 changes: 3 additions & 4 deletions test/integration/test_fastapi_integration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import unittest
from test.unit.services.no_annotations.random.random_service import RandomService

from fastapi import FastAPI, Depends
from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient
from typing_extensions import Annotated

from test.unit.services.no_annotations.random.random_service import RandomService
from wireup import Wire, ParameterBag, DependencyContainer
from wireup import DependencyContainer, ParameterBag, Wire
from wireup.errors import UnknownServiceRequestedError
from wireup.integration.fastapi_integration import wireup_init_fastapi_integration

Expand Down
4 changes: 1 addition & 3 deletions test/integration/test_flask_integration.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import unittest
from dataclasses import dataclass

from test.fixtures import FooBar, FooBase
from test.unit.services.no_annotations.random.random_service import RandomService

from flask import Flask
from typing_extensions import Annotated

from test.unit.services.no_annotations.random.random_service import RandomService
from wireup import DependencyContainer, ParameterBag, Wire
from wireup.integration.flask_integration import wireup_init_flask_integration

Expand Down
9 changes: 5 additions & 4 deletions test/performance_test/test_inject_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import timeit
import unittest
from dataclasses import dataclass
from typing import Optional

from typing_extensions import Annotated
from wireup import DependencyContainer, ParameterBag, Wire
Expand Down Expand Up @@ -60,10 +61,10 @@ def autowired(
a: A,
b: B,
c: C,
_d: unittest.TestCase = None,
_e: unittest.TestCase = None,
_f: unittest.TestCase = None,
_g: unittest.TestCase = None,
_d: Optional[unittest.TestCase] = None,
_e: Optional[unittest.TestCase] = None,
_f: Optional[unittest.TestCase] = None,
_g: Optional[unittest.TestCase] = None,
):
return c.c() + b.b() + a.a()

Expand Down
1 change: 0 additions & 1 deletion test/unit/services/no_annotations/foo/foo_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass

from test.unit.services.no_annotations.db_service import DbService


Expand Down
38 changes: 26 additions & 12 deletions test/unit/test_container.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import datetime
import functools
import unittest
from dataclasses import dataclass
from test.fixtures import Counter, FooBar, FooBase, FooBaz
from unittest.mock import Mock, patch

from typing_extensions import Annotated

from test.unit import services
from test.unit.services.no_annotations.random.random_service import RandomService
from test.unit.services.no_annotations.random.truly_random_service import TrulyRandomService
from typing import Optional
from unittest.mock import Mock, patch

from typing_extensions import Annotated
from wireup import ServiceLifetime, Wire, register_all_in_module, wire
from wireup.errors import (
DuplicateQualifierForInterfaceError,
Expand Down Expand Up @@ -88,7 +89,7 @@ def inner(random: Annotated[RandomService, Wire()]):

def test_inject_using_annotated_empty_wire_fails_to_inject_unknown(self):
@self.container.autowire
def inner(random: Annotated[unittest.TestCase, Wire()]): ...
def inner(_random: Annotated[unittest.TestCase, Wire()]): ...

with self.assertRaises(UnknownServiceRequestedError) as context:
inner()
Expand Down Expand Up @@ -296,7 +297,7 @@ def test_register_same_qualifier_should_raise(self):

def test_qualifier_raises_wire_called_on_unknown_type(self):
@self.container.autowire
def inner(sub1: FooBase = wire(qualifier="sub1")): ...
def inner(_sub1: FooBase = wire(qualifier="sub1")): ...

self.container.abstract(FooBase)
with self.assertRaises(UnknownQualifiedServiceRequestedError) as context:
Expand All @@ -310,7 +311,7 @@ def inner(sub1: FooBase = wire(qualifier="sub1")): ...

def test_inject_abstract_directly_raises(self):
@self.container.autowire
def inner(sub1: FooBase): ...
def inner(_sub1: FooBase): ...

self.container.abstract(FooBase)
self.container.register(FooBar, qualifier="foobar")
Expand All @@ -325,7 +326,7 @@ def inner(sub1: FooBase): ...

def test_inject_abstract_directly_with_no_impls_raises(self):
@self.container.autowire
def inner(sub1: FooBase): ...
def inner(_sub1: FooBase): ...

self.container.abstract(FooBase)
with self.assertRaises(Exception) as context:
Expand All @@ -342,7 +343,7 @@ def test_register_with_qualifier_fails_when_invoked_without(self):
class RegisterWithQualifierClass: ...

@self.container.autowire
def inner(foo: RegisterWithQualifierClass): ...
def inner(_foo: RegisterWithQualifierClass): ...

with self.assertRaises(UnknownQualifiedServiceRequestedError) as context:
inner()
Expand All @@ -369,7 +370,7 @@ def inner(foo: RegisterWithQualifierClass = wire(qualifier=__name__)):

def test_inject_qualifier_on_unknown_type(self):
@self.container.autowire
def inner(foo: str = wire(qualifier=__name__)): ...
def inner(_foo: str = wire(qualifier=__name__)): ...

with self.assertRaises(UsageOfQualifierOnUnknownObjectError) as context:
inner()
Expand Down Expand Up @@ -489,11 +490,24 @@ def test_get_returns_real_instance(self):
self.assertIsInstance(second, RandomService)

def test_shrinks_context_on_autowire(self):
def target(a: RandomService, _b: unittest.TestCase = None, _c: datetime.datetime = None):
class SomeClass:
pass

def provide_b(fn):
@functools.wraps(fn)
def __inner(*args, **kwargs):
return fn(*args, **kwargs, b=SomeClass())

return __inner

@provide_b
def target(a: RandomService, b: SomeClass, _c: Optional[datetime.datetime] = None):
self.assertEqual(a.get_random(), 4)
self.assertIsInstance(b, SomeClass)

autowired = self.container.autowire(target)
self.assertEqual(self.container.context.dependencies[target].keys(), {"a", "_b", "_c"})
self.assertEqual(self.container.context.dependencies[target].keys(), {"a", "b"})
# On the second call, container will drop b from dependencies as it is an unknown object.
autowired()
self.assertEqual(self.container.context.dependencies[target].keys(), {"a"})

Expand Down
3 changes: 1 addition & 2 deletions test/unit/test_container_optimized.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import unittest
from dataclasses import dataclass
from test.fixtures import FooBar, FooBase, FooBaz
from test.unit.services.no_annotations.random.random_service import RandomService

from typing_extensions import Annotated

from test.unit.services.no_annotations.random.random_service import RandomService
from wireup import DependencyContainer, ParameterBag, ServiceLifetime, Wire


Expand Down
6 changes: 2 additions & 4 deletions test/unit/test_container_override.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import unittest

from test.fixtures import FooBase, FooBar
from test.fixtures import FooBar, FooBase
from test.unit.services.no_annotations.random.random_service import RandomService
from unittest.mock import MagicMock, patch

from typing_extensions import Annotated

from test.unit.services.no_annotations.random.random_service import RandomService
from wireup import DependencyContainer, ParameterBag, Wire
from wireup.errors import UnknownOverrideRequestedError
from wireup.ioc.override_manager import OverrideManager
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_container_proxy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from test.unit.services.no_annotations.random.random_service import RandomService
from test.unit.services.no_annotations.random.truly_random_service import TrulyRandomService

from wireup.ioc.proxy import ContainerProxy


Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_container_static_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from test.fixtures import Counter, FooBar, FooBase
from test.unit.services.no_annotations.random.random_service import RandomService
from unittest import TestCase

from test.unit.services.no_annotations.random.random_service import RandomService
from wireup import DependencyContainer, ParameterBag, ServiceLifetime, warmup_container, wire
from wireup.errors import (
DuplicateServiceRegistrationError,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_initialization_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from test.unit.services.no_annotations.db_service import DbService

from wireup.ioc.initialization_context import InitializationContext
from wireup.ioc.types import AnnotatedParameter

Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_module_loading.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest

import wireup
from test.unit.services import no_annotations, with_annotations
from test.unit.services.with_annotations.env import EnvService

import wireup
from wireup import DependencyContainer, ParameterBag, register_all_in_module, warmup_container


Expand All @@ -11,7 +11,7 @@ def test_register_all_in_module_is_recursive(self):
container = DependencyContainer(ParameterBag())
register_all_in_module(container, module=no_annotations, pattern="*Service")

registered = {t.__name__ for t in container.context.dependencies.keys()}
registered = {t.__name__ for t in container.context.dependencies}
self.assertEqual(
registered, {"BarService", "DbService", "TrulyRandomService", "BazService", "FooService", "RandomService"}
)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_parameter_interpolation_is_cached(self):
self.bag.put("foo", "bar")
self.assertEqual(self.bag.get(TemplatedString("${foo}-${foo}")), "bar-bar")
self.assertEqual(self.bag.get(TemplatedString("${foo}-${foo}")), "bar-bar")
self.assertEqual(self.bag._ParameterBag__cache, {"${foo}-${foo}": "bar-bar"}) # noqa: SLF001
self.assertEqual(self.bag._ParameterBag__cache, {"${foo}-${foo}": "bar-bar"})

def test_get_parameter_unknown(self):
with self.assertRaises(UnknownParameterError) as context:
Expand Down
8 changes: 4 additions & 4 deletions test/unit/test_recursive_module_loading.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import unittest

from test.unit import services

from wireup import DependencyContainer, ParameterBag, register_all_in_module


Expand All @@ -10,7 +10,7 @@ def test_register_all_in_module_is_recursive(self):
container = DependencyContainer(ParameterBag())
register_all_in_module(container, module=services, pattern="*Service")
self.assertSetEqual(
{x.__name__ for x in container.context.dependencies.keys()},
{x.__name__ for x in container.context.dependencies},
{
"EnvService",
"TrulyRandomService",
Expand All @@ -26,15 +26,15 @@ def test_register_all_in_module_is_recursive_multiple_patterns(self):
container = DependencyContainer(ParameterBag())
register_all_in_module(container, module=services, pattern=re.compile("^Db.+|.+Repository$"))
self.assertSetEqual(
{x.__name__ for x in container.context.dependencies.keys()},
{x.__name__ for x in container.context.dependencies},
{"DbService", "FooRepository"},
)

def test_register_all_in_module_is_recursive_matches_pattern(self):
container = DependencyContainer(ParameterBag())
register_all_in_module(container, module=services, pattern="*Random*")
self.assertEqual(
{x.__name__ for x in container.context.dependencies.keys()},
{x.__name__ for x in container.context.dependencies},
{
"RandomService",
"TrulyRandomService",
Expand Down
3 changes: 1 addition & 2 deletions test/unit/test_service_registry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import unittest
from test.unit.services.no_annotations.random.random_service import RandomService

from typing_extensions import Annotated

from test.unit.services.no_annotations.random.random_service import RandomService
from wireup import ServiceLifetime, Wire
from wireup.errors import (
DuplicateServiceRegistrationError,
Expand Down

0 comments on commit b2f9383

Please sign in to comment.