Skip to content

Conversation

@cswartzvi
Copy link

This PR attempts to address issue #234 by introducing a unique key dictionary in the Binder when unique=True.

Copy link
Collaborator

@davidparsson davidparsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution. Your changes look fine to me, but please consider my comments below.

"""A dictionary that raises an exception when trying to add duplicate bindings."""
def __setitem__(self, key: type, value: Binding) -> None:
if key in self.data:
raise NonUniqueBinding(key.__name__)
Copy link
Collaborator

@davidparsson davidparsson Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see a more human readable error message here. Our other error messages help the more novice users understad what is wrong.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely agree! I will add a message that explains "Binding for 'X' already exists". Do you think that is detailed enough, or would you like to seeing some mention of the unique flag?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first message would be good enough, but that it would be even better to also mention the flag.

injector_test.py Outdated
Comment on lines 1587 to 1593
def test_binder_with_uniqueness_checking_raises_error():
def configure(binder):
binder.bind(int, to=123)
binder.bind(int, to=456)

with pytest.raises(NonUniqueBinding):
_ = Injector([configure], unique=True)
Copy link
Collaborator

@davidparsson davidparsson Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

I would like to see how the unique flag interacts with parent/child injectors though, both to verify the functionality and as a form of documentation of the expected behavior. I would expect child injectors to be allowed to override binds in parent injectors (and I see no reason to why this should not work). What do you think of adding another test for that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! It looks like there are a couple of tests keying on similar concepts - I will borrow from them.


parent = Injector(configure)
child = parent.create_child_injector()
child = parent.create_child_injector(unique=unique)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be even more interesting to add the unique flag to the parent injector as well, to see that they interact as expected, since it's an even stricter case? I think that it should be allowed and work with the current implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I agree. Also: I'm wondering if child injectors shouldn't inherit the unique value of the parent injector by default.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That crossed my mind as well, and yes, I think that would be reasonable.

@codecov-commenter
Copy link

codecov-commenter commented Oct 5, 2023

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.37%. Comparing base (395e7e8) to head (5784937).
⚠️ Report is 27 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #235      +/-   ##
==========================================
+ Coverage   95.20%   95.37%   +0.17%     
==========================================
  Files           1        1              
  Lines         563      584      +21     
  Branches       95       99       +4     
==========================================
+ Hits          536      557      +21     
  Misses         20       20              
  Partials        7        7              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@davidparsson davidparsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ready to approve this, just a final question

Comment on lines +1587 to +1593
def test_injector_with_uniqueness_checking_raises_error():
def configure(binder):
binder.bind(int, to=123)
binder.bind(int, to=456)

with pytest.raises(NonUniqueBinding, match="Binding for 'int' already exists"):
_ = Injector([configure], unique=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want a test for the opposite case, where overriding works without unique=True?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cswartzvi bump?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@craig8 apologies, I completely forgot about this PR. I will try and push the requested changes this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants