scrapy
scrapy copied to clipboard
Simplify create_instance calls
Calling https://github.com/scrapy/scrapy/blob/bbfa185664cef79299b48cb0ae22065439bb07fc/scrapy/utils/misc.py#L144 with a crawler is needlessly verbose:
create_instance(objclass, settings=None, crawler=crawler)
create_instance(objclass, settings=crawler.settings, crawler=crawler) # functionally identical
It would be great if we could support a shorter alternative:
create_instance(objclass, crawler=crawler)
It is not trivial to do in a backward-compatible way, given the function signature.
It may be also a good idea to log deprecation warnings when settings
and crawler
are used as positional arguments, as well as when objcls
is used as a named parameter, so that we can eventually stop supporting that and move to a better signature:
def create_instance(objcls, /, *args, crawler=None, settings=None, **kwargs):
Although, looking at it now, I realize that such a signature would cause settings
and crawler
to be after *args
, which could be confusing. Maybe it would be best to go for separate functions:
def build_from_crawler(objcls, crawler, /, *args, **kwargs):
def build_from_settings(objcls, settings, /, *args, **kwargs):
+1 to the issue. I like the idea of having 2 separate functions. It'd be nice to see how they simplify (or not) the code in Scrapy and in third-party projects.
@Gallaecio, @kmike I have made a PR to implement the two separate functions. Feedback awaited. :-)
Hi - has this been resolved or could i work on this?
Still open, and the previous attempt seems abandoned.