Skip to content

bug: factory methods only receive activating type instead of registered type #53

@StummeJ

Description

@StummeJ

Given the following, factory methods fail to produce the expected outcome, and instead raise the NotImplementedError exception.

class Dep:
  def __init__(_, value: str):
    ...

TypeA = Annotated[Dep, "type_a"]
TypeB = Annotated[Dep, "type_b"]

class Fetcher:
  def __init__(_, dep: Dep):
    ...

def factory(scope, dep_type) -> Dep:
  if dep_type == TypeA:
    value = "a"
  elif dep_type == TypeB:
    value = "b"
  else:
    raise NotImplementedError(f"Unknown type: {dep_type}")

  return Dep(value)

container.add_transient_by_factory(factory, TypeA)
container.add_transient_by_factory(factory, TypeB)
container.add_transient(Fetcher)

fetcher = container.get(Fetcher)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions