As stated in this closed bug at pydantic-settings, i cannot achieve an initialization through injector on python 3.8 and 3.9 (works ok on 3.10+) without a factory method.
The issue was turned down by the pydantic-settings team.
The error message
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
Why ?
Pydantic BaseSettings base class uses PEP-604 (str | None) annotation instead of PEP-484 (Union, Optional).
It seems that Pydantic BaseSettings class don’t like to be initialized through cls.__new__(cls).
This works
class Configuration(Module):
def configure(self, binder: Binder):
binder.bind(BaseSettings, scope=SingletonScope, to=lambda: BaseSettings())
This does not
class Configuration(Module):
def configure(self, binder: Binder):
binder.bind(BaseSettings, scope=SingletonScope)
Reproduced issue
https://github.com/Guibod/pydantic-settings-bug-298