web-poet
web-poet copied to clipboard
create a Class Factory to customize arguments for injected dependencies
This issue aims to discuss customizable arguments to injected dependencies as proposed by @gatufo.
This is in-line with the idea proposed in https://github.com/scrapinghub/web-poet/issues/18.
The idea is to create a Class Factory that allows us to have a finer control for creating the injected dependencies. Here's an example:
@attr.define
class JsonPage(WebPage):
semantic_data: ClassFactory(JsonParser, library='json')
@attr.define
class AnotherJsonPage(WebPage):
semantic_data: ClassFactory(JsonParser, library='demjson')
In the example above, the ClassFactory
returns a class which points to the appropriate implementation of the JSON Parser. A class should be returned to comply with the type annotation format which is later used for type inference on andi's build plan.
I was thinking about syntax similar to Python's built-in typing - something like
@attr.define
class JsonPage(WebPage):
semantic_data: JsonParser['json']
@attr.define
class AnotherJsonPage(WebPage):
semantic_data: JsonParser['demjson']
We can also use Annotated from stdlib.
Should we close this now that we started using Annotated
?
Yes we can 👍