From a017f7b93298f8fcf5252afd8674466de95f04f9 Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Tue, 7 Mar 2017 14:44:27 +0100 Subject: [PATCH] Warn about modules where duplicate spider names were found --- scrapy/spiderloader.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scrapy/spiderloader.py b/scrapy/spiderloader.py index 1322c01d1..27a909c9f 100644 --- a/scrapy/spiderloader.py +++ b/scrapy/spiderloader.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import +from collections import defaultdict import traceback import warnings @@ -19,14 +20,21 @@ class SpiderLoader(object): def __init__(self, settings): self.spider_modules = settings.getlist('SPIDER_MODULES') self._spiders = {} + self._found = defaultdict(list) self._load_all_spiders() def _load_spiders(self, module): for spcls in iter_spider_classes(module): + self._found[spcls.name].append((module.__name__, spcls.__name__)) if spcls.name in self._spiders.keys(): import warnings - warnings.warn("There are several spiders with the same name (" + spcls.name + - "), this can cause unexpected behavior", UserWarning) + msg = ("There are several spiders with the same name {!r}:\n" + "{}\n This can cause unexpected behavior.".format( + spcls.name, + "\n".join( + " {1} (in {0})".format(mod, cls) + for (mod, cls) in self._found[spcls.name]))) + warnings.warn(msg, UserWarning) self._spiders[spcls.name] = spcls def _load_all_spiders(self):