Skip to content

Commit

Permalink
Only inject members of instances
Browse files Browse the repository at this point in the history
  • Loading branch information
ZechCodes committed Nov 1, 2023
1 parent 1251b23 commit b38c874
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions bevy/injectors/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def __init__(self):
self._key: Option[_K] = Null()

def __get__(self, instance: object, owner: Type):
if instance is None:
return self

return self._inject_dependency()

def __set_name__(self, owner: Type, name: str):
ns = _get_class_namespace(owner)
annotations = get_annotations(owner, globals=ns, eval_str=True)
self._key = Value(annotations[name])

def _inject_dependency(self):
match self._key:
case Value(key):
repo = get_repository()
Expand All @@ -29,11 +40,6 @@ def __get__(self, instance: object, owner: Type):
case Null():
raise Exception("The dependency has not been setup")

def __set_name__(self, owner: Type, name: str):
ns = _get_class_namespace(owner)
annotations = get_annotations(owner, globals=ns, eval_str=True)
self._key = Value(annotations[name])


def dependency() -> Any:
"""This helper function allows instances of the `Dependency` type to be assigned to fields without breaking type
Expand Down

0 comments on commit b38c874

Please sign in to comment.