You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Per a suggestion, moving a conversation from a gist to here.
I have a type-hinting-based injector that I would like to rewrite to use PEP 593 annotations. I currently put the injector hints in dataclass field metadata. Annotations would make it work with functions, namedtuple etc. Similar to what the inject package does. My injection though needs zero or more arguments.
Here's code from a test that fails at the end, when switching to an alias. Exception info is in the comment.
Perhaps I should either get rid of the marker or find some other way to determine it's one of my annotations? Or presume I'm the only consumer of annotations?
deftest_handle_field_injected_customer_experiment(this_container, this_injector, this_injector2):
this_container.register_singleton(Customer(), Customer)
InjectT=TypeVar('InjectT')
defsimple_factory(customer: Customer):
returncustomerresult=this_injector(target=simple_factory)
assert'Base Customer'==result.name# ==============================================================# Let's start using Annotated so we can bring in the other# features from the current injector.defannotated_factory(customer: Annotated[Customer, _inject_marker]):
returncustomerresult=this_injector(target=annotated_factory)
assert'Base Customer'==result.name# ==============================================================# The current injector lets the return type be different than# the lookup type. We want the result to be FrenchCustomer,# but ask the injector for the registered Customer.deffrenchcustomer_factory(customer: Annotated[FrenchCustomer, Customer, _inject_marker]):
# The injector looked up "Customer" whichreturncustomerresult=this_injector2(target=frenchcustomer_factory)
assert'French Customer'==result.name# ==============================================================# The current injector also lets you pluck off just an# attribute, which is handy for building a props-based# component system.defattr_factory(customer_name: Annotated[str, Customer, Attr('name'), _inject_marker]):
# The injector looked up "Customer" whichreturnf'Name: {customer_name}'result=this_injector2(target=attr_factory)
assert'Name: French Customer'==result# ==============================================================# That's getting a little noisy. Would be nice to use a TypeAlias# to at least cut down that last part. But the alias fails at# runtime with:# TypeError: Too many parameters for typing.Annotated[~InjectT,# <object object at 0x10a4ce8c0>]; actual 3, expected 1## mypy fails with:# tests/test_593_injector.py:177: error: Bad number of arguments for type alias, expected: 1, given: 3# tests/test_593_injector.py:177: error: Invalid type comment or annotation# tests/test_593_injector.py:177: note: Suggestion: use Attr[...] instead of Attr(...)Injected=Annotated[InjectT, _inject_marker]
definjected_factory(customer_name: Injected[str, Customer, Attr('name')]):
# The injector looked up "Customer" whichreturnf'Name: {customer_name}'result=this_injector2(target=injected_factory)
assert'Name: French Customer'==result
The text was updated successfully, but these errors were encountered:
@pauleveritt I am sorry for the very late answer, but could you condense your example. I assume the problem are the following lines, where mypy complains?
Hi, thanks for coming back on this. I agree, the example is way too long.
FWIW, I worked around it by having a single object in the second spot, which then collected the "pipeline" and behaved like a marker. Here's an example
I think this could be closed, as this is a reasonable solution for me.
(Per a suggestion, moving a conversation from a gist to here.
I have a type-hinting-based injector that I would like to rewrite to use PEP 593 annotations. I currently put the injector hints in dataclass field metadata. Annotations would make it work with functions, namedtuple etc. Similar to what the inject package does. My injection though needs zero or more arguments.
Here's code from a test that fails at the end, when switching to an alias. Exception info is in the comment.
Perhaps I should either get rid of the marker or find some other way to determine it's one of my annotations? Or presume I'm the only consumer of annotations?
The text was updated successfully, but these errors were encountered: