web-poet icon indicating copy to clipboard operation
web-poet copied to clipboard

create a Class Factory to customize arguments for injected dependencies

Open BurnzZ opened this issue 2 years ago • 1 comments

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.

BurnzZ avatar Dec 13 '21 07:12 BurnzZ

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']

kmike avatar Dec 15 '21 12:12 kmike

We can also use Annotated from stdlib.

kmike avatar Jan 31 '23 19:01 kmike

Should we close this now that we started using Annotated?

Gallaecio avatar Feb 09 '24 10:02 Gallaecio

Yes we can 👍

BurnzZ avatar Feb 09 '24 10:02 BurnzZ