Do not shadow submodules

using object that have the same name as submodules as the weird effect
of makeing `import pip.commands.<something> as <anothername>` fail with
a key error. This fixes it by renamin commands as command_dict and fixin
a few imports to accomodate.

Related to #2149
This commit is contained in:
Bussonnier Matthias 2014-12-02 16:47:36 +01:00
parent 6388435996
commit 0582a91e62
7 changed files with 26 additions and 14 deletions

View File

@ -6,7 +6,7 @@ from docutils import nodes
from docutils.parsers import rst
from docutils.statemachine import ViewList
from textwrap import dedent
from pip import commands
from pip.commands import commands_dict as commands
from pip import cmdoptions
from pip.utils import get_prog

View File

@ -14,7 +14,11 @@ from pip.utils import get_installed_distributions, get_prog
from pip.utils import deprecation
from pip.vcs import git, mercurial, subversion, bazaar # noqa
from pip.baseparser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
from pip.commands import commands, get_summaries, get_similar_commands
from pip.commands import get_summaries, get_similar_commands
from pip.commands import commands_dict
# assignment for flake8 to be happy
# This fixes a peculiarity when importing via __import__ - as we are
# initialising the pip module, "from pip import cmdoptions" is recursive
@ -72,7 +76,7 @@ def autocomplete():
print(dist)
sys.exit(1)
subcommand = commands[subcommand_name]()
subcommand = commands_dict[subcommand_name]()
options += [(opt.get_opt_string(), opt.nargs)
for opt in subcommand.parser.option_list_all
if opt.help != optparse.SUPPRESS_HELP]
@ -158,7 +162,7 @@ def parseopts(args):
# the subcommand name
cmd_name = args_else[0]
if cmd_name not in commands:
if cmd_name not in commands_dict:
guess = get_similar_commands(cmd_name)
msg = ['unknown command "%s"' % cmd_name]
@ -203,7 +207,7 @@ def main(args=None):
sys.stderr.write(os.linesep)
sys.exit(1)
command = commands[cmd_name](isolated=check_isolated(cmd_args))
command = commands_dict[cmd_name](isolated=check_isolated(cmd_args))
return command.main(cmd_args)

View File

@ -16,7 +16,7 @@ from pip.commands.zip import ZipCommand
from pip.commands.wheel import WheelCommand
commands = {
commands_dict = {
CompletionCommand.name: CompletionCommand,
FreezeCommand.name: FreezeCommand,
HelpCommand.name: HelpCommand,
@ -49,9 +49,9 @@ def get_summaries(ignore_hidden=True, ordered=True):
"""Yields sorted (command name, command summary) tuples."""
if ordered:
cmditems = _sort_commands(commands, commands_order)
cmditems = _sort_commands(commands_dict, commands_order)
else:
cmditems = commands.items()
cmditems = commands_dict.items()
for name, command_class in cmditems:
if ignore_hidden and command_class.hidden:
@ -66,7 +66,7 @@ def get_similar_commands(name):
name = name.lower()
close_commands = get_close_matches(name, commands.keys())
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]

View File

@ -12,7 +12,7 @@ class HelpCommand(Command):
summary = 'Show help for commands.'
def run(self, options, args):
from pip.commands import commands, get_similar_commands
from pip.commands import commands_dict, get_similar_commands
try:
# 'pip help' with no args is handled by pip.__init__.parseopt()
@ -20,7 +20,7 @@ class HelpCommand(Command):
except IndexError:
return SUCCESS
if cmd_name not in commands:
if cmd_name not in commands_dict:
guess = get_similar_commands(cmd_name)
msg = ['unknown command "%s"' % cmd_name]
@ -29,7 +29,7 @@ class HelpCommand(Command):
raise CommandError(' - '.join(msg))
command = commands[cmd_name]()
command = commands_dict[cmd_name]()
command.parser.print_help()
return SUCCESS

View File

@ -3,7 +3,7 @@ import pytest
from pip.exceptions import CommandError
from pip.basecommand import ERROR, SUCCESS
from pip.commands.help import HelpCommand
from pip.commands import commands
from pip.commands import commands_dict as commands
from mock import Mock

View File

@ -52,3 +52,11 @@ def test_correct_pip_version(script):
'mismatched source files in %r and %r: %r' %
(pip_folder, pip_folder_outputed, mismatch_py)
)
def test_as_import(script):
""" test that pip.__init__.py does not shadow
the command submodule with a dictionary
"""
import pip.commands.install as inst
assert inst is not None

View File

@ -4,7 +4,7 @@ import pip.baseparser
from pip import main
from pip import cmdoptions
from pip.basecommand import Command
from pip.commands import commands
from pip.commands import commands_dict as commands
class FakeCommand(Command):