Commit Graph

455 Commits

Author SHA1 Message Date
ljnsn 587f855c25
rename setup -> setup_method (#221) 2023-07-11 08:30:54 +02:00
Jakub Stasiak 80b4f1a94f
Remove one dependency (#224) 2023-06-29 02:59:44 +02:00
Jakub Stasiak aa2b884b7b
Test with all recent Python/PyPy versions (#223)
Just to make sure the software actually works with them and we
don't miss errors.
2023-06-29 02:51:33 +02:00
Jakub Stasiak f1a659c986
Make Mypy happy (#222)
We're assigning to an attribute Mypy doesn't know about. The error it
raised:

    injector/__init__.py:862: error: "FunctionType" has no attribute "__binding__"  [attr-defined]
    Found 1 error in 1 file (checked 1 source file)
2023-06-29 02:41:59 +02:00
David Pärsson d00556c898
Create unbound `@singleton` instances in the parent injector (#216)
This change introduces consistency to how instances for unbound
classes decorated with a `@singleton` are shared among parent/child
injectors, when auto-binding is enabled.

Classes decorated with `@singleton`, that have not been explicitly
bound, are now created by and bound to the parent injector closest
to the root where all dependencies are fulfilled.

The behavior was like this before, but only when the parent injector
had created the singleton instance (and its implicit binding) before
the child injector. This allows sharing singletons between child
injectors without creating them on the parent injector first.
2023-04-24 15:51:47 +02:00
Jakub Stasiak 87826b3542
Fix an Injector.get() thread safety regression (#215)
One of the thread safety tests has been failing on various PyPy
versions[1]:

    _____________ TestThreadSafety.test_singleton_scope_is_thread_safe _____________
    Traceback (most recent call last):
      File "/home/runner/work/injector/injector/injector_test.py", line 863, in test_singleton_scope_is_thread_safe
        assert a is b
    AssertionError: assert <injector_test.TestThreadSafety.setup.<locals>.XXX object at 0x0000000006b80d08> is <injector_test.TestThreadSafety.setup.<locals>.XXX object at 0x0000000006b810c0>

SingletonScope's get() was protected with a lock already but Injector.get()
wasn't and there are things in there that are totally likely to produce
invalid results if executed from multiple contexts concurrently.

Rather than try to nail down the exact issue that caused this, figure
out why it's been failing only on PyPy etc. I figured it's ok to go for
the big hammer here and protect the whole method with the lock. This way
we don't have to wonder about the details that can be *really* difficult
to figure out.

I don't think this is gonna make anything meaningfully slower –
ClassProvider and CallableProvider are already locked (indirecty,
through Injector.args_to_inject() being synchronized) and I expect
these to be involved in vast majority of the injection work.

[1] https://github.com/python-injector/injector/actions/runs/4156039132/jobs/7189544034
2023-02-12 13:51:15 +01:00
Jakub Stasiak 90aa2cc877
Reformat using the latest Black (#214)
We need this to make the CI green again.
2023-02-12 12:12:23 +01:00
David Pärsson 353594656a
Ignore most dotfiles in the root (#210)
To allow some flexibility in the tools used by contributors.
2023-02-11 14:17:05 +01:00
Jakub Wilk ccf0645306
Remove stray reST markup from README (#213) 2023-02-11 13:35:16 +01:00
David Pärsson 49ad2c5c67
Ignore `.dmypy.json`, created by the mypy daemon (#209) 2022-12-09 16:16:37 +01:00
David Pärsson 3942351225
Typing fixes (#208)
Explicit optional type hints

This is the default in recent `mypy` versions. It is fully backwards
compatible, but the previous typing did not work with explicit
optional type hints.

Declare classes as generic

To fix the following `mypy` error:

> A function returning TypeVar should receive at least one argument
> containing the same TypeVar [type-var]
2022-12-09 16:10:39 +01:00
Jonathan 93fe2e8a9c
Highlight that the code snippet will not work (#204)
Highlight that the code snippet will not work. Users most of the time will
skim read the docs trying to spot snippets on how to use the code.

Since this is the only snippet on the welcome page, we might want to
highlight that it's an example that won't work.
2022-12-02 13:53:37 +01:00
Jakub Stasiak 1db1f56cfb Release version 0.20.1 2022-08-17 01:03:10 +02:00
Søren Fuglede Jørgensen 8fb79dc7b5
Use pypandoc.convert_file instead of pypandoc.convert (#201)
This fixes an issue that breaks the build when using `pypandoc` 1.8
and above in which `convert` has been removed; see
https://github.com/man-group/pytest-plugins/issues/87#issuecomment-1123830409
2022-07-11 22:18:04 +02:00
David Pärsson f66285c194
Support PEP 604 optional & union types (#203)
Fixes #202.
2022-07-11 22:12:08 +02:00
Jakub Stasiak 97aef806fd Release version 0.20.0 2022-06-15 01:28:49 +02:00
Jakub Stasiak 42f58c6f4c
Handle some invalid forward references in return type annotations (#200)
Seems like something that's good to have anyway but the main purpose is
to fix https://github.com/alecthomas/flask_injector/issues/69.
2022-06-15 01:25:19 +02:00
Erik Cederberg c8fb73ef83
Do not check parent binder for `AssistedBuilder` (#196)
Only check the current binder for bindings when resolving
`AssistedBuilder` instances, since any injected argument to the built
class should use the active injector when creating the `AssistedBuilder`
and not one of its parents.

Fixes #186.
2022-06-15 01:16:40 +02:00
Tobias Nilsson ce957ab42a
fix: inspect union arguments for annotations (#195)
NoInject[T] = None has the runtime typehint NoInject[T] | None,
instead of the expected NoInject[T | None].
NoInject[str] = 'blarb' works as expected

Resolves #192
2022-06-15 01:01:32 +02:00
Jakub Stasiak 92212fe08b
Drop Python 3.6 suport, it's EOL (#198)
There are a few changes that need to happen together with this one,
namely we need to specify explicit 3.7+ PyPy3 versions and in order
to do that we have to upgrade the setup-python GH action.
2022-06-15 00:45:23 +02:00
Jakub Stasiak 8122b898e6 Release version 0.19.0 2021-12-20 23:18:57 +01:00
Jakub Stasiak fa5fd3f89a Test with newer, stable Python 3.10
I forgot to bump this version after 3.10 was released.
2021-12-20 23:14:29 +01:00
Jakub Stasiak ba0409d40b Fix accidentally broken Python pre-3.10 compatibility
Tests started failing like

________________________ test_newtype_integration_works ________________________
Traceback (most recent call last):
  File "/home/runner/work/injector/injector/injector_test.py", line 1398, in test_newtype_integration_works
    injector = Injector([configure])
  File "/home/runner/work/injector/injector/injector/__init__.py", line 904, in __init__
    self.binder.install(module)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 573, in install
    instance(self)
  File "/home/runner/work/injector/injector/injector_test.py", line 1396, in configure
    binder.bind(UserID, to=123)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 474, in bind
    self._bindings[interface] = self.create_binding(interface, to, scope)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 578, in create_binding
    provider = self.provider_for(interface, to)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 640, in provider_for
    raise UnknownProvider('couldn\'t determine provider for %r to %r' % (interface, to))
injector.UnknownProvider: couldn't determine provider for <function NewType.<locals>.new_type at 0x7f64edc69d90> to 123

when I merged d7f6f396eb.
2021-12-20 23:13:27 +01:00
Jakub Stasiak 3a63a32e1e Make the build pass Mypy inspection
Newer Mypy detects/complains about more things, let's fix those.
2021-12-20 23:12:57 +01:00
Torge Matthies d7f6f396eb
Fix for Python 3.10 (#193)
Makes GreenWithEnvy work again.

Fixes errors like

    Traceback (most recent call last):
      File "/Users/user/projects/injector/injector_test.py", line 1398, in test_newtype_integration_works
        injector = Injector([configure])
      File "/Users/user/projects/injector/injector/__init__.py", line 904, in __init__
        self.binder.install(module)
      File "/Users/user/projects/injector/injector/__init__.py", line 573, in install
        instance(self)
      File "/Users/user/projects/injector/injector_test.py", line 1396, in configure
        binder.bind(UserID, to=123)
      File "/Users/user/projects/injector/injector/__init__.py", line 474, in bind
        self._bindings[interface] = self.create_binding(interface, to, scope)
      File "/Users/user/projects/injector/injector/__init__.py", line 578, in create_binding
        provider = self.provider_for(interface, to)
      File "/Users/user/projects/injector/injector/__init__.py", line 640, in provider_for
        raise UnknownProvider('couldn\'t determine provider for %r to %r' % (interface, to))
    injector.UnknownProvider: couldn't determine provider for injector_test.UserID to 123

Fixes GH-192
2021-12-20 22:20:03 +01:00
Janusz Skonieczny bbaac17330
Fix manifest so src dist can be used to install or build a wheel (#189) 2021-08-31 21:42:57 +02:00
Takahiro Kojima dc441ba2ab
Fix a tiny document mistake (#184) 2021-05-22 13:03:22 +02:00
Jakub Stasiak d99785247b Enable and fix tests on Python 3.10
from __future__ annotations are the default on Python 3.10 so the tricks
we used to keep the test suite nice can no longer be applied.

CI addition to make sure it stays compatible.
2021-02-11 19:23:42 +01:00
Jakub Stasiak d0534882a6 Switch from Travis CI and Coveralls to GH Actions and Codecov
Free Travis CI is shutting down and since I already have Codecov set up
in other projects instead of Coveralls I'm switching that too for
consistency.

There's no nightly so I left it out for now.
2021-02-11 19:15:49 +01:00
Greg Eremeev dcc9e6772a
Add requirements.txt and requirements-dev.txt (#172) 2021-01-06 01:56:00 +01:00
Jakub Stasiak 94e5b175d7 Correct two typos 2020-12-09 19:49:37 +01:00
Jakub Stasiak 4e3a28911c
Verify that noninjectable/inject ordering doesn't matter (#166)
Closes GH-141.
2020-12-06 13:59:41 +01:00
Jakub Stasiak 1d3d5e1003 Change some comments to variable annotations for clarity
Since we only support Python 3.6+ now we can do this.
2020-12-03 22:09:36 +01:00
Jakub Stasiak 7abb04e90b Drop Python 3.5 support, it's EOL now 2020-12-03 19:10:54 +01:00
Jakub Stasiak 098f46def2 Test with Python 3.9, it's been released two months ago 2020-12-03 19:04:10 +01:00
Jakub Stasiak e05eb082c6 Remove some no longer used Python 2-specific code 2020-12-03 19:02:06 +01:00
Jakub Stasiak a189dfcb9d Gitignore some Python artifacts 2020-12-03 19:02:06 +01:00
Joshua Adelman 0edfc5524d
include license file in source distribution (#160) 2020-10-14 20:49:20 +02:00
Jakub Stasiak 6e7941d6d0 Release version 0.18.4 2020-09-22 20:45:13 +02:00
Jakub Stasiak 6b2d563acf Actually interpret all NoInjects when multiple are present
Fixes GH-154.
2020-09-22 20:43:01 +02:00
Jakub Stasiak d4251f3962 Reformat using the latest black 2020-09-22 20:21:02 +02:00
Jakub Stasiak e912acfdd3
Add a note regarding client code knowing about Injector 2020-09-22 20:15:40 +02:00
Jakub Stasiak e686f36d88 Update terminology in the documentation 2020-05-23 12:10:12 +02:00
Jakub Stasiak 072e868bd2 Don't require typing_extensions on Python 3.9 unnecessarily 2020-05-23 12:05:15 +02:00
Jakub Stasiak 211994f48e Fix some links to avoid redirections 2020-02-05 11:19:43 +01:00
Jakub Stasiak 2293a8905d Release version 0.18.3 2020-02-03 15:35:27 +01:00
Jakub Stasiak a639cd5afb
Fix Python 3.5.3 compatibility (#136)
Fixes GH-135.
2020-02-03 15:34:02 +01:00
Sanjay Siddhanti 4137c35b7e Fix typos in README (#134) 2020-01-24 00:20:53 +01:00
Jakub Stasiak e6d55adf6f Document some of the Binder.bind() behavior 2020-01-21 19:00:14 +01:00
Jakub Stasiak fbbdbb1c61 Document injecting into CallableProvider callables 2020-01-21 18:59:38 +01:00