python-injector / python-injector/injector

Using default arguments when nothing is injected

Open
#118 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
94
PR merge metrics
No merged PRs in 30d

Description

Using NewType (or Key) is a great way to inject configuration into classes, but there are cases where there is a sane default that is rarely overridden. In these cases it would be nice to have a way to define the default value close to where it is used.

Injector currently does not consider default values unless @noninjectable is used, but @noninjectable does not allow configuration to be injected when the default is not to be used.

Is there a way to do this? If not, what are your thoughts on the topic?

The following is an example of how it would be useful:

SleepTime = NewType('SleepTime', float)


class WaitStrategy:

    @inject
    def __init__(self, sleep_time: SleepTime = 1.0) -> None:
        self._sleep_time = sleep_time

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the injector's handling of default arguments and the existing @noninjectable behavior described in the issue. Compare both paths against the WaitStrategy example and determine the intended API before making changes. Done means the default can be used while still allowing the dependency to be injected when configured, with behavior covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.