Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass package_name explicitly in click.version_option decorators for compatibility with click>=8.0 #1400

Merged
merged 10 commits into from
Jun 4, 2021
3 changes: 2 additions & 1 deletion piptools/_compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .click_compat import IS_CLICK_VER_8_PLUS
from .pip_compat import PIP_VERSION, parse_requirements

__all__ = ["PIP_VERSION", "parse_requirements"]
__all__ = ["PIP_VERSION", "IS_CLICK_VER_8_PLUS", "parse_requirements"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: why is this a list? It's usually recommended to make __all__ immutable, hence use a tuple.

P.S. This is out of the scope of this PR, just wanted to make a note.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be fair, this is a list as well in the python tutorial on modules: https://docs.python.org/3/tutorial/modules.html#importing-from-a-package and the pip package has this format as well.
Do you want to change this here or should I leave as it is? I actually don't have any preference here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, don't touch this because it's out of the scope. But in the future, this should probably be added. I've added this comment for history.

P.S. Normally, pylint would warn about this problem but I don't think it's integrated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least pycodestyle seems to do so, compare PyCQA/pydocstyle#62

5 changes: 5 additions & 0 deletions piptools/_compat/click_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import click
nicoa marked this conversation as resolved.
Show resolved Hide resolved
from pip._vendor.packaging.version import Version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like relying on how and whether pip vendors its deps. Plus it may cause problems when repackaged in distros that unbundle vendored software.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, was just in analogy to pip_compat module - would you agree with using int(click.__version__.split(".")[0])?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that should be enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, you may want to multi-line it to keep the inline complexity low

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean with multi-lining, as for me black would merge this into one line if not forcing to not do on the following:

int(
    click.__version__.split(".")[0]
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvmd, will simply add a comment then in between the brackeets

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's one of the reasons I dislike black...

As for the line complexity, I like this metric that https://wemake-python-stylegui.de introduced called Jones complexity. It really makes a difference maintainability/readability-wise!

Here's some more info on this https://sobolevn.me/2019/10/complexity-waterfall#lines.

I wish this set of linter plugins was integrated here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't knew the blog entry regarding the complexity until now, found it a good read and would recommend to colleagues, thanks a lot for that, very interesting!


CLICK_VERSION = Version(click.__version__).major
nicoa marked this conversation as resolved.
Show resolved Hide resolved
IS_CLICK_VER_8_PLUS = CLICK_VERSION > 7
7 changes: 5 additions & 2 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pip._internal.req.constructors import install_req_from_line
from pip._internal.utils.misc import redact_auth_from_url

from .._compat import parse_requirements
from .._compat import IS_CLICK_VER_8_PLUS, parse_requirements
from ..cache import DependencyCache
from ..exceptions import PipToolsError
from ..locations import CACHE_DIR
Expand All @@ -33,6 +33,9 @@
DEFAULT_REQUIREMENTS_OUTPUT_FILE = "requirements.txt"
METADATA_FILENAMES = frozenset({"setup.py", "setup.cfg", "pyproject.toml"})

# TODO: drop click 7 and remove this block, pass directly to version_option
version_option_kwargs = {} if IS_CLICK_VER_8_PLUS else {"package_name": "pip-tools"}


def _get_default_option(option_name: str) -> Any:
"""
Expand All @@ -45,7 +48,7 @@ def _get_default_option(option_name: str) -> Any:


@click.command(context_settings={"help_option_names": ("-h", "--help")})
@click.version_option()
@click.version_option(**version_option_kwargs)
@click.pass_context
@click.option("-v", "--verbose", count=True, help="Show more output")
@click.option("-q", "--quiet", count=True, help="Give less output")
Expand Down
7 changes: 5 additions & 2 deletions piptools/scripts/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
from pip._internal.utils.misc import get_installed_distributions

from .. import sync
from .._compat import parse_requirements
from .._compat import IS_CLICK_VER_8_PLUS, parse_requirements
from ..exceptions import PipToolsError
from ..logging import log
from ..repositories import PyPIRepository
from ..utils import flat_map

DEFAULT_REQUIREMENTS_FILE = "requirements.txt"

# TODO: drop click 7 and remove this block, pass directly to version_option
version_option_kwargs = {} if IS_CLICK_VER_8_PLUS else {"package_name": "pip-tools"}


@click.command(context_settings={"help_option_names": ("-h", "--help")})
@click.version_option()
@click.version_option(**version_option_kwargs)
@click.option(
"-a",
"--ask",
Expand Down