Skip to content

FromDI fails with an AttributeError on None when the DI middleware never ran for a message #54

Description

@lesnik512

What

Dependency.__call__ reads the request container out of FastStream's ContextRepo:

request_container: Container = context.get(_REQUEST_CONTAINER_KEY)
return self.marker.resolve(request_container)

ContextRepo.get returns None for a key nobody set, so when the DI middleware did not run for a message the user sees modern-di's internals fail on the next line:

AttributeError: 'NoneType' object has no attribute 'resolve_dependency'

Nothing in that message points at this package, at setup_di, or at app startup.

When it happens

  • FromDI is used on an app where setup_di was never called.
  • The app was never started, so the on_startup hook that installs the middleware (fix: install the DI middleware on every broker, on startup #52) did not run. The common form is a test that enters TestNatsBroker(broker) without TestApp(app); the modern-di docs already warn about that pairing, but the error itself does not.

Before #52 this was also what a subscriber on any broker but the first produced (#42).

Proposal

Check for None in Dependency.__call__ and raise a RuntimeError that says the request container is missing for this message and names the two fixes: call setup_di(app, container) on the app that owns the broker, and start the app (pair the test broker with TestApp in tests).

One failing test first, in tests/test_faststream_di.py, for each of the two triggers above.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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