Skip to content

Commit

Permalink
Revert to namedtuple over dataclass for python 3.6
Browse files Browse the repository at this point in the history
This means we can't use the registration to cache the resolution
result any more so instead we just introduce a new 'singletons'
property on the Container itself, which is nice and simple I guess.
  • Loading branch information
bobthemighty committed Feb 2, 2020
1 parent d33ad92 commit 5d9ae69
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions punq/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from dataclasses import dataclass
from typing import Callable, Any, List, get_type_hints
from typing import Callable, Any, List, get_type_hints, NamedTuple
import inspect
from collections import defaultdict, namedtuple
from collections import defaultdict
from enum import Enum

from pkg_resources import DistributionNotFound, get_distribution
Expand Down Expand Up @@ -97,8 +96,7 @@ class Scope(Enum):
singleton = 1


@dataclass
class Registration:
class Registration(NamedTuple):
service: str
scope: Scope
builder: Callable[[], Any]
Expand Down Expand Up @@ -295,6 +293,7 @@ class Container:

def __init__(self):
self.registrations = Registry()
self._singletons = {}

def register(
self, service, factory=empty, instance=empty, scope=Scope.transient, **kwargs
Expand Down Expand Up @@ -428,7 +427,7 @@ def _build_impl(self, registration, resolution_args, context):
result = registration.builder(**args)

if registration.scope == Scope.singleton:
registration.builder = lambda: result
self._singletons[registration.service] = result

context[registration.service] = result

Expand All @@ -438,6 +437,9 @@ def _resolve_impl(self, service_key, kwargs, context):

context = self.registrations.build_context(service_key, context)

if service_key in self._singletons:
return self._singletons[service_key]

if context.has_cached(service_key):
return context[service_key]

Expand Down

0 comments on commit 5d9ae69

Please sign in to comment.