scrapy icon indicating copy to clipboard operation
scrapy copied to clipboard

Simplify create_instance calls

Open Gallaecio opened this issue 2 years ago • 1 comments

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):

Gallaecio avatar Jun 09 '22 09:06 Gallaecio

+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.

kmike avatar Jun 26 '22 19:06 kmike

@Gallaecio, @kmike I have made a PR to implement the two separate functions. Feedback awaited. :-)

ayushanand308 avatar Apr 02 '23 20:04 ayushanand308

Hi - has this been resolved or could i work on this?

sa2415 avatar Nov 28 '23 20:11 sa2415

Still open, and the previous attempt seems abandoned.

Gallaecio avatar Nov 29 '23 07:11 Gallaecio