Skip to content

Commit

Permalink
Make ProcessEnv.bin always return a str (#378)
Browse files Browse the repository at this point in the history
The property never returns None. This simplifies user code containing
types that already knows a bin directory must exist. It avoids the need
to pepper calling code with:

    assert session.bin is not None

Or

    # type: ignore

For example, in pip:
https://github.com/pypa/pip/blob/062f0e54d99f58e53be36be5a45adad89e2429fb/tools/automation/release/__init__.py#L29

A noxfile that tries to access a bin directory that doesn't exist will
now raise an exception.
  • Loading branch information
jdufresne authored Feb 8, 2021
1 parent 156765c commit 1025682
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 4 additions & 2 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ def bin_paths(self) -> Optional[List[str]]:
return self.virtualenv.bin_paths

@property
def bin(self) -> Optional[str]:
def bin(self) -> str:
"""The first bin directory for the virtualenv."""
paths = self.bin_paths
return paths[0] if paths is not None else None
if paths is None:
raise ValueError("The environment does not have a bin directory.")
return paths[0]

def create_tmp(self) -> str:
"""Create, and return, a temporary directory."""
Expand Down
6 changes: 4 additions & 2 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ def bin_paths(self) -> Optional[List[str]]:
return self._bin_paths

@property
def bin(self) -> Optional[str]:
def bin(self) -> str:
"""The first bin directory for the virtualenv."""
paths = self.bin_paths
return paths[0] if paths is not None else None
if paths is None:
raise ValueError("The environment does not have a bin directory.")
return paths[0]

def create(self) -> bool:
raise NotImplementedError("ProcessEnv.create should be overwritten in subclass")
Expand Down
5 changes: 4 additions & 1 deletion tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ def test_no_bin_paths(self):
session, runner = self.make_session_and_runner()

runner.venv.bin_paths = None
assert session.bin is None
with pytest.raises(
ValueError, match=r"^The environment does not have a bin directory\.$"
):
session.bin
assert session.bin_paths is None

def test_virtualenv_as_none(self):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,17 @@ def mock_sysfind(arg):
def test_process_env_constructor():
penv = nox.virtualenv.ProcessEnv()
assert not penv.bin_paths
with pytest.raises(
ValueError, match=r"^The environment does not have a bin directory\.$"
):
penv.bin

penv = nox.virtualenv.ProcessEnv(env={"SIGIL": "123"})
assert penv.env["SIGIL"] == "123"

penv = nox.virtualenv.ProcessEnv(bin_paths=["/bin"])
assert penv.bin == "/bin"


def test_process_env_create():
penv = nox.virtualenv.ProcessEnv()
Expand Down

0 comments on commit 1025682

Please sign in to comment.