Replies: 1 comment
-
You're not missing something; There is no way to temporarily turn off validation for this. Litestar is rather strict about this (you might even say pedantic 🙂). That being said, a subclass should work. The reason why it does not comes down to a limitation of msgspec. This for example fails as well, even though it would be fine for e.g. a type checker: import msgspec
class Foo(msgspec.Struct):
pass
class Bar(Foo):
pass
class Container(msgspec.Struct):
foo: Foo
msgspec.convert({"foo": Bar()}, type=Container, strict=False) The other reason this doesn't work is because Litestar implicitly validates the injected dependencies. This wasn't always the case and not intentionally designed as a feature. What you're experiencing here is one of the drawbacks of this approach. We have considered changing this behaviour in v3, but for now I'm afraid there's not much you can do other than providing the correct type in your tests as well or turn of validation for this dependency. A completely different approach we've discussed a while ago (and might consider picking up again @litestar-org/maintainers?) would be to change how dependency overrides with the test client work. Currently, you can provide dependency providers. An alternative would be to allow providing the values, to be injected directly into the handler. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I feel like I'm missing something. For testing an edge case, I would like to use a fake/mock instead of a real dependency1.
Easy enough:
However, this explodes with:
Unless I mark the argument as unconditionally unvalidated:
prince: Annotated[Prince, Dependency(skip_validation=True)]
And as far as I can tell, there is no way to have it validated in prod and temporarily disabled for testing like this? Even a subclass doesn't work? This seems… wrong? Am I missing something?
FWIW I've seen https://github.com/orgs/litestar-org/discussions/1789 but it doesn't seem to answer this.
Footnotes
I use https://pypi.org/project/pretend/ here, but
unittest.mock
has the same problem. ↩Beta Was this translation helpful? Give feedback.
All reactions