adriangb / di

Pythonic dependency injection
https://www.adriangb.com/di/
MIT License
301 stars 13 forks source link

feature / handle generics (actually GenericAlias) in bind_by_type #109

Closed maxzhenzhera closed 1 year ago

maxzhenzhera commented 1 year ago

For now, the support of generics is not guaranteed.

typing.Generic do caching. But cache size is limited. When new instances of GenericAlias are being created - the current hook in bind_by_type does not handle this case because it does the is check. https://github.com/adriangb/di/blob/e88c8f9f1e6ddd06d41095025ea209f6f8d15056/di/_container.py#L81-L98

I propose to do a == check. It is completely safe for simple types ([default __eq__ is actualy is](https://docs.python.org/3/reference/datamodel.html?highlight=By%20default,%20object%20implements%20__eq__()%20by%20using%20is#object.__eq__)). But also this will handle GenericAlias __eq__.