Skip to content

injector.Injector.create_object on pydantic-settings BaseSettings does not without factory function in Injector #253

@Guibod

Description

@Guibod

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

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