Skip to content

Commit

Permalink
Merge pull request #9411 from jdufresne/type-noxfile
Browse files Browse the repository at this point in the history
Complete typing of noxfile.py
  • Loading branch information
pradyunsg authored Feb 20, 2021
2 parents f3f4ef2 + 7ffd0ca commit b588c58
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ repos:
- id: mypy
exclude: docs|tests
args: ["--pretty"]
additional_dependencies: ['nox==2020.12.31']

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.7.0
Expand Down
Empty file.
27 changes: 19 additions & 8 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"""Automation using nox.
"""

# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False

import glob
import os
import shutil
import sys
from pathlib import Path
from typing import Iterator, List, Tuple

import nox

Expand All @@ -34,20 +32,22 @@


def run_with_protected_pip(session, *arguments):
# type: (nox.Session, *str) -> None
"""Do a session.run("pip", *arguments), using a "protected" pip.
This invokes a wrapper script, that forwards calls to original virtualenv
(stable) version, and not the code being tested. This ensures pip being
used is not the code being tested.
"""
env = {"VIRTUAL_ENV": session.virtualenv.location}
# https://github.com/theacodes/nox/pull/377
env = {"VIRTUAL_ENV": session.virtualenv.location} # type: ignore

command = ("python", LOCATIONS["protected-pip"]) + arguments
kwargs = {"env": env, "silent": True}
session.run(*command, **kwargs)
session.run(*command, env=env, silent=True)


def should_update_common_wheels():
# type: () -> bool
# If the cache hasn't been created, create it.
if not os.path.exists(LOCATIONS["common-wheels"]):
return True
Expand All @@ -72,6 +72,7 @@ def should_update_common_wheels():
# -----------------------------------------------------------------------------
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "pypy3"])
def test(session):
# type: (nox.Session) -> None
# Get the common wheels.
if should_update_common_wheels():
run_with_protected_pip(
Expand All @@ -88,7 +89,8 @@ def test(session):
session.log(msg)

# Build source distribution
sdist_dir = os.path.join(session.virtualenv.location, "sdist")
# https://github.com/theacodes/nox/pull/377
sdist_dir = os.path.join(session.virtualenv.location, "sdist") # type: ignore
if os.path.exists(sdist_dir):
shutil.rmtree(sdist_dir, ignore_errors=True)
session.run(
Expand Down Expand Up @@ -117,10 +119,12 @@ def test(session):

@nox.session
def docs(session):
# type: (nox.Session) -> None
session.install("-e", ".")
session.install("-r", REQUIREMENTS["docs"])

def get_sphinx_build_command(kind):
# type: (str) -> List[str]
# Having the conf.py in the docs/html is weird but needed because we
# can not use a different configuration directory vs source directory
# on RTD currently. So, we'll pass "-c docs/html" here.
Expand All @@ -141,6 +145,7 @@ def get_sphinx_build_command(kind):

@nox.session
def lint(session):
# type: (nox.Session) -> None
session.install("pre-commit")

if session.posargs:
Expand All @@ -154,13 +159,15 @@ def lint(session):

@nox.session
def vendoring(session):
# type: (nox.Session) -> None
session.install("vendoring>=0.3.0")

if "--upgrade" not in session.posargs:
session.run("vendoring", "sync", ".", "-v")
return

def pinned_requirements(path):
# type: (Path) -> Iterator[Tuple[str, str]]
for line in path.read_text().splitlines():
one, two = line.split("==", 1)
name = one.strip()
Expand Down Expand Up @@ -208,6 +215,7 @@ def pinned_requirements(path):
# -----------------------------------------------------------------------------
@nox.session(name="prepare-release")
def prepare_release(session):
# type: (nox.Session) -> None
version = release.get_version_from_arguments(session)
if not version:
session.error("Usage: nox -s prepare-release -- <version>")
Expand Down Expand Up @@ -243,6 +251,7 @@ def prepare_release(session):

@nox.session(name="build-release")
def build_release(session):
# type: (nox.Session) -> None
version = release.get_version_from_arguments(session)
if not version:
session.error("Usage: nox -s build-release -- YY.N[.P]")
Expand Down Expand Up @@ -274,6 +283,7 @@ def build_release(session):


def build_dists(session):
# type: (nox.Session) -> List[str]
"""Return dists with valid metadata."""
session.log(
"# Check if there's any Git-untracked files before building the wheel",
Expand Down Expand Up @@ -302,6 +312,7 @@ def build_dists(session):

@nox.session(name="upload-release")
def upload_release(session):
# type: (nox.Session) -> None
version = release.get_version_from_arguments(session)
if not version:
session.error("Usage: nox -s upload-release -- YY.N[.P]")
Expand All @@ -320,7 +331,7 @@ def upload_release(session):
f"Remove dist/ and run 'nox -s build-release -- {version}'"
)
# Sanity check: Make sure the files are correctly named.
distfile_names = map(os.path.basename, distribution_files)
distfile_names = (os.path.basename(fn) for fn in distribution_files)
expected_distribution_files = [
f"pip-{version}-py3-none-any.whl",
f"pip-{version}.tar.gz",
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ per-file-ignores =
tests/*: B011

[mypy]
follow_imports = silent
ignore_missing_imports = True
disallow_untyped_defs = True
disallow_any_generics = True
Expand Down
8 changes: 5 additions & 3 deletions tools/automation/release/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def get_version_from_arguments(session: Session) -> Optional[str]:
# We delegate to a script here, so that it can depend on packaging.
session.install("packaging")
cmd = [
os.path.join(session.bin, "python"),
# https://github.com/theacodes/nox/pull/378
os.path.join(session.bin, "python"), # type: ignore
"tools/automation/release/check_version.py",
version
]
Expand Down Expand Up @@ -153,11 +154,12 @@ def workdir(
"""Temporarily chdir when entering CM and chdir back on exit."""
orig_dir = pathlib.Path.cwd()

nox_session.chdir(dir_path)
# https://github.com/theacodes/nox/pull/376
nox_session.chdir(dir_path) # type: ignore
try:
yield dir_path
finally:
nox_session.chdir(orig_dir)
nox_session.chdir(orig_dir) # type: ignore


@contextlib.contextmanager
Expand Down

0 comments on commit b588c58

Please sign in to comment.