Skip to content

Commit

Permalink
chore: some reverb suggestions and cleanup old Python 2-3.6 stuff (#657)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Oct 8, 2022
1 parent 56e5582 commit 15013bd
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 61 deletions.
6 changes: 3 additions & 3 deletions nox/_decorators.py
Expand Up @@ -18,11 +18,11 @@
import functools
import inspect
import types
from typing import Any, Callable, Iterable, TypeVar, cast
from typing import TYPE_CHECKING, Any, Callable, Iterable, TypeVar, cast

from . import _typing

if _typing.TYPE_CHECKING:
if TYPE_CHECKING:
from ._parametrize import Param


Expand Down Expand Up @@ -68,7 +68,7 @@ def __init__(
self.reuse_venv = reuse_venv
self.venv_backend = venv_backend
self.venv_params = venv_params
self.should_warn = should_warn or dict()
self.should_warn = should_warn or {}
self.tags = tags or []

def __call__(self, *args: Any, **kwargs: Any) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion nox/_option_set.py
Expand Up @@ -237,7 +237,7 @@ def parser(self) -> ArgumentParser:
}

for option in self.options.values():
if option.hidden is True:
if option.hidden:
continue

# Every option must have a group (except for hidden options)
Expand Down
8 changes: 4 additions & 4 deletions nox/_options.py
Expand Up @@ -157,15 +157,15 @@ def _color_finalizer(value: bool, args: argparse.Namespace) -> bool:
Returns:
The new value for the "color" option.
"""
if args.forcecolor is True and args.nocolor is True:
if args.forcecolor and args.nocolor:
raise _option_set.ArgumentError(
None, "Can not specify both --no-color and --force-color."
)

if args.forcecolor is True:
if args.forcecolor:
return True

if args.nocolor is True:
if args.nocolor:
return False

return sys.stdout.isatty()
Expand Down Expand Up @@ -510,7 +510,7 @@ def _session_completer(
"invoked_from",
group=None,
hidden=True,
default=lambda: os.getcwd(),
default=os.getcwd,
),
)

Expand Down
2 changes: 1 addition & 1 deletion nox/_parametrize.py
Expand Up @@ -36,7 +36,7 @@ def __init__(
arg_names: Sequence[str] | None = None,
id: str | None = None,
) -> None:
self.args = tuple(args)
self.args = args
self.id = id

if arg_names is None:
Expand Down
31 changes: 3 additions & 28 deletions nox/_typing.py
Expand Up @@ -14,33 +14,8 @@

from __future__ import annotations

__all__ = ["TYPE_CHECKING", "ClassVar", "NoReturn", "Python"]
__all__ = ["Python"]

import typing as _typing
from typing import Sequence, Union

try:
from typing import TYPE_CHECKING
except ImportError:
try:
from typing import TYPE_CHECKING
except ImportError:
TYPE_CHECKING = False

try:
from typing import NoReturn
except ImportError:
try:
from typing import NoReturn
except ImportError:
pass


try:
from typing import ClassVar
except ImportError:
try:
from typing import ClassVar
except ImportError:
pass

Python = _typing.Optional[_typing.Union[str, _typing.Sequence[str], bool]]
Python = Union[str, Sequence[str], bool, None]
5 changes: 2 additions & 3 deletions nox/logger.py
Expand Up @@ -97,7 +97,7 @@ def output(self, msg: str, *args: Any, **kwargs: Any) -> None:


def _get_formatter(color: bool, add_timestamp: bool) -> logging.Formatter:
if color is True:
if color:
return NoxColoredFormatter(
reset=True,
log_colors={
Expand All @@ -112,8 +112,7 @@ def _get_formatter(color: bool, add_timestamp: bool) -> logging.Formatter:
secondary_log_colors=None,
add_timestamp=add_timestamp,
)
else:
return NoxFormatter(add_timestamp=add_timestamp)
return NoxFormatter(add_timestamp=add_timestamp)


def setup_logging(
Expand Down
25 changes: 17 additions & 8 deletions nox/sessions.py
Expand Up @@ -24,15 +24,23 @@
import sys
import unicodedata
from types import TracebackType
from typing import Any, Callable, Generator, Iterable, Mapping, Sequence
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generator,
Iterable,
Mapping,
NoReturn,
Sequence,
)

import nox.command
from nox import _typing
from nox._decorators import Func
from nox.logger import logger
from nox.virtualenv import CondaEnv, PassthroughEnv, ProcessEnv, VirtualEnv

if _typing.TYPE_CHECKING:
if TYPE_CHECKING:
from nox.manifest import Manifest


Expand Down Expand Up @@ -89,7 +97,7 @@ def _dblquote_pkg_install_arg(pkg_req_str: str) -> str:
)

if "<" in pkg_req_str or ">" in pkg_req_str:
if pkg_req_str[0] == '"' and pkg_req_str[-1] == '"':
if pkg_req_str[0] == pkg_req_str[-1] == '"':
# already double-quoted string
return pkg_req_str
else:
Expand Down Expand Up @@ -646,11 +654,11 @@ def debug(self, *args: Any, **kwargs: Any) -> None:
"""Outputs a debug-level message during the session."""
logger.debug(*args, **kwargs)

def error(self, *args: Any) -> _typing.NoReturn:
def error(self, *args: Any) -> NoReturn:
"""Immediately aborts the session and optionally logs an error."""
raise _SessionQuit(*args)

def skip(self, *args: Any) -> _typing.NoReturn:
def skip(self, *args: Any) -> NoReturn:
"""Immediately skips the session and optionally logs a warning."""
raise _SessionSkip(*args)

Expand Down Expand Up @@ -817,11 +825,12 @@ def imperfect(self) -> str:
"""
if self.status == Status.SUCCESS:
return "was successful"

status = self.status.name.lower()
if self.reason:
return f"{status}: {self.reason}"
else:
return status

return status

def log(self, message: str) -> None:
"""Log a message using the appropriate log function.
Expand Down
21 changes: 9 additions & 12 deletions nox/virtualenv.py
Expand Up @@ -14,18 +14,18 @@

from __future__ import annotations

import contextlib
import os
import platform
import re
import shutil
import subprocess
import sys
from socket import gethostbyname
from typing import Any, Mapping
from typing import Any, ClassVar, Mapping

import nox
import nox.command
from nox import _typing
from nox.logger import logger

# Problematic environment variables that are stripped from all commands inside
Expand All @@ -52,7 +52,7 @@ class ProcessEnv:
is_sandboxed = False

# Special programs that aren't included in the environment.
allowed_globals: _typing.ClassVar[tuple[Any, ...]] = ()
allowed_globals: ClassVar[tuple[Any, ...]] = ()

def __init__(
self, bin_paths: None = None, env: Mapping[str, str] | None = None
Expand Down Expand Up @@ -207,7 +207,7 @@ def __init__(
self.location = os.path.abspath(location)
self.interpreter = interpreter
self.reuse_existing = reuse_existing
self.venv_params = venv_params if venv_params else []
self.venv_params = venv_params or []
self.conda_cmd = conda_cmd
super().__init__(env={"CONDA_PREFIX": self.location})

Expand All @@ -227,10 +227,8 @@ def _clean_location(self) -> bool:
]
nox.command.run(cmd, silent=True, log=False)
# Make sure that location is clean
try:
with contextlib.suppress(FileNotFoundError):
shutil.rmtree(self.location)
except FileNotFoundError:
pass

return True

Expand All @@ -247,8 +245,8 @@ def bin_paths(self) -> list[str]:
os.path.join(self.location, "Scripts"),
os.path.join(self.location, "bin"),
]
else:
return [os.path.join(self.location, "bin")]

return [os.path.join(self.location, "bin")]

def create(self) -> bool:
"""Create the conda env."""
Expand Down Expand Up @@ -332,7 +330,7 @@ def __init__(
self._resolved: None | str | InterpreterNotFound = None
self.reuse_existing = reuse_existing
self.venv_or_virtualenv = "venv" if venv else "virtualenv"
self.venv_params = venv_params if venv_params else []
self.venv_params = venv_params or []
super().__init__(env={"VIRTUAL_ENV": self.location})

def _clean_location(self) -> bool:
Expand Down Expand Up @@ -465,8 +463,7 @@ def bin_paths(self) -> list[str]:
"""Returns the location of the virtualenv's bin folder."""
if _SYSTEM == "Windows":
return [os.path.join(self.location, "Scripts")]
else:
return [os.path.join(self.location, "bin")]
return [os.path.join(self.location, "bin")]

def create(self) -> bool:
"""Create the virtualenv or venv."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -76,7 +76,7 @@ branch = true
omit = [ "nox/_typing.py" ]

[tool.coverage.report]
exclude_lines = [ "pragma: no cover", "if _typing.TYPE_CHECKING:", "@overload" ]
exclude_lines = [ "pragma: no cover", "if TYPE_CHECKING:", "@overload" ]

[tool.pytest.ini_options]
minversion = "6.0"
Expand Down

0 comments on commit 15013bd

Please sign in to comment.