Skip to content

Commit

Permalink
fix: doc-comments + thread-sharable feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshorr committed Nov 12, 2022
1 parent f302abe commit 2da6a76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 2 additions & 3 deletions udepend/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ class _TreatAsRootParentType(Singleton):
"""


# todo: Remove ContextType
class UContext:
"""
See [Quick Start](#quick-start) in the `udepend.context` module if your new to the UContext
Expand Down Expand Up @@ -875,8 +874,8 @@ class provides a class method `udepend.resource.Resource.resource` to easily get
# So, code using a Dependency in general should never have to worry about this None case.

if self._is_root_context_for_app:
from udepend import Dependency
if issubclass(for_type, Dependency) and not for_type.resource_thread_safe:
from udepend.dependency import is_dependency_thread_sharable
if not is_dependency_thread_sharable(for_type):
return None

parent_value = None
Expand Down
9 changes: 6 additions & 3 deletions udepend/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def attributes_to_skip_while_copying(dependency: 'Dependency') -> Set[str]:
return dependency._dependency__meta.get('attributes_to_skip_while_copying', set())


class Dependency(thread_sharable=True):
class Dependency:
"""
If you have not already done so, you should also read the udepend project's
[README.md](https://github.com/xyngular/py-u-depend#active-resource-proxy) for an overview
Expand Down Expand Up @@ -347,7 +347,8 @@ def __init_subclass__(
When app-root does not have the dependency, it potentially needs to lazily create
the dependency depending on if Dependency is thread-safe.
So at this point, if `resource_thread_safe` value is (as a class attribute):
So at this point, if you call `is_dependency_thread_sharable` on type/cls,
and if returned value is:
- `False`: The app-root context will return `None` instead of lazily creating the
Dependency. It's expected a thread-root UContext is the thing that asked the
Expand Down Expand Up @@ -641,7 +642,9 @@ class PerThreadDependency(Dependency, thread_sharable=False):
This makes the object available to be seen/used by other threads.
When a dependency makes a subclass from `PerThreadDependency` or otherwise set's
the `Dependency.resource_thread_safe` to False at the Dependency class-level.
the `Dependency.__init_subclass__`'s `thread_sharable` to `False` via the Dependency
class arguments (so that `is_dependency_thread_sharable` will return `False` when its passed
the new Dependency subclass/type).
When a thread asks for that dependency for first time it will be lazily created like expected,
but the resulting object is placed in the root-context instead (and NOT the app-root-context).
Expand Down

0 comments on commit 2da6a76

Please sign in to comment.