Skip to content

Commit

Permalink
fixes from ruff --fix, mostly tests (conda#12294)
Browse files Browse the repository at this point in the history
* fixes from ruff --fix, mostly tests
* add news item
  • Loading branch information
dholth authored Feb 1, 2023
1 parent 5a9deaf commit 6c4a226
Show file tree
Hide file tree
Showing 32 changed files with 81 additions and 98 deletions.
4 changes: 0 additions & 4 deletions conda/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
# If it's only used in one module, keep it in that module, preferably near the top.
# This module should contain ONLY stdlib imports.

from itertools import chain
from operator import methodcaller
import sys
from tempfile import mkdtemp

on_win = bool(sys.platform == "win32")
on_mac = bool(sys.platform == "darwin")
Expand Down Expand Up @@ -44,7 +41,6 @@ def encode_arguments(arguments):


from collections.abc import Iterable
from io import StringIO

def isiterable(obj):
return not isinstance(obj, str) and isinstance(obj, Iterable)
Expand Down
21 changes: 21 additions & 0 deletions news/12294-ruff-fixes
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Enhancements

* <news item>

### Bug fixes

* <news item>

### Deprecations

* Remove unused `chain`, `methodcaller`, `mkdtemp`, `StringIO` imports in
`conda.common.compat`; apply other fixes from `ruff --fix .` in the test
suite. (PR #12294)

### Docs

* <news item>

### Other

* <news item>
2 changes: 1 addition & 1 deletion tests/base/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def test_context_parameters_have_descriptions(self):

from pprint import pprint
for name in documented_parameter_names:
description = context.get_descriptions()[name]
context.get_descriptions()[name]
pprint(context.describe_parameter(name))

def test_local_build_root_custom_rc(self):
Expand Down
2 changes: 0 additions & 2 deletions tests/cli/test_cli_install.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

import tempfile
from unittest import TestCase
from unittest.mock import patch
from conda.testing.integration import run_command, Commands

Expand Down
1 change: 0 additions & 1 deletion tests/cli/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import os
from io import StringIO
from unittest import TestCase

import pytest
from pytest import raises
Expand Down
2 changes: 0 additions & 2 deletions tests/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from contextlib import contextmanager
from textwrap import dedent
import pytest

from conda.auxlib.compat import Utf8NamedTemporaryFile

Expand Down Expand Up @@ -89,7 +88,6 @@ def test_invalid_yaml():
""")
try:
with make_temp_condarc(condarc) as rc:
rc_path = rc
run_command(Commands.CONFIG, '--file', rc, '--add', 'channels', 'test')
except ConfigurationLoadError as err:
assert "reason: invalid yaml at line" in err.message, err.message
Expand Down
4 changes: 2 additions & 2 deletions tests/cli/test_main_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_rename_with_dry_run(env_one):
)

(out, err, exit_code), data = list_envs()
result = data.get("envs", [])
data.get("envs", [])

assert locate_prefix_by_name(TEST_ENV_NAME_1)
with pytest.raises(EnvironmentNameNotFound):
Expand All @@ -255,7 +255,7 @@ def test_rename_with_force_and_dry_run(env_one, env_prefix_one):
)

(out, err, exit_code), data = list_envs()
result = data.get("envs", [])
data.get("envs", [])

assert locate_prefix_by_name(TEST_ENV_NAME_1)
with pytest.raises(EnvironmentNameNotFound):
Expand Down
5 changes: 2 additions & 3 deletions tests/common/pkg_formats/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: BSD-3-Clause
"""Test for python distribution information and metadata handling."""

from datetime import datetime
from errno import ENOENT
import os
from os.path import basename, lexists
Expand Down Expand Up @@ -385,7 +384,7 @@ def test_metadata():
meta = PythonDistributionMetadata(fpath)
a = meta.get_dist_requirements()
b = meta.get_python_requirements()
z = meta.get_external_requirements()
meta.get_external_requirements()
c = meta.get_extra_provides()
d = meta.get_dist_provides()
e = meta.get_dist_obsolete()
Expand Down Expand Up @@ -468,7 +467,7 @@ def test_dist_get_paths_no_paths():
temp_path = tempfile.mkdtemp()
dist = PythonEggInfoDistribution(temp_path, "2.7", None)
with pytest.raises(EnvironmentError):
paths = dist.get_paths()
dist.get_paths()


def test_get_dist_requirements():
Expand Down
4 changes: 2 additions & 2 deletions tests/conda_env/specs/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_has_detect_function(self):

def test_dispatches_to_registered_specs(self):
spec1, spec2 = generate_two_specs()
with patched_specs(spec1, spec2) as all_specs:
with patched_specs(spec1, spec2):
actual = specs.detect(name="foo")
self.assertEqual(actual, spec2)

Expand All @@ -56,7 +56,7 @@ def test_passes_kwargs_to_all_specs(self):
def test_raises_exception_if_no_detection(self):
spec1 = generate_two_specs()[0]
spec1.msg = 'msg'
with patched_specs(spec1) as all_specs:
with patched_specs(spec1):
with self.assertRaises(SpecNotFound):
specs.detect(name="foo")

Expand Down
5 changes: 0 additions & 5 deletions tests/conda_env/specs/test_binstar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

import types
import unittest
try:
from io import StringIO
except ImportError:
from StringIO import StringIO
from unittest.mock import patch, MagicMock
from binstar_client import errors

from conda_env.specs import binstar
from conda_env.specs.binstar import BinstarSpec
from conda_env.env import Environment

Expand Down
1 change: 0 additions & 1 deletion tests/conda_env/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import unittest
from logging import Handler, getLogger
from os.path import exists, join
from shutil import rmtree
from unittest import TestCase
from uuid import uuid4

Expand Down
4 changes: 1 addition & 3 deletions tests/core/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

from logging import getLogger
from unittest import TestCase
from unittest.mock import patch

import os
import pytest

from conda.base.constants import DEFAULT_CHANNELS
from conda.base.context import context, Context, conda_tests_ctxt_mgmt_def_pol, non_x86_machines
from conda.base.context import context, conda_tests_ctxt_mgmt_def_pol, non_x86_machines
from conda.common.compat import on_win, on_mac, on_linux
from conda.common.io import env_vars
from conda.core.index import check_allowlist, get_index, get_reduced_index, _supplement_index_with_system
Expand Down
14 changes: 7 additions & 7 deletions tests/core/test_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_solve_msgs_exclude_vp(tmpdir, clear_cuda_version):
with env_var('CONDA_OVERRIDE_CUDA', '10.0'):
with get_solver_cuda(tmpdir, specs) as solver:
with pytest.raises(UnsatisfiableError) as exc:
final_state = solver.solve_final_state()
solver.solve_final_state()

assert "__cuda==10.0" not in str(exc.value).strip()

Expand Down Expand Up @@ -179,7 +179,7 @@ def test_cuda_fail_1(tmpdir, clear_cuda_version):
with env_var('CONDA_OVERRIDE_CUDA', '8.0'):
with get_solver_cuda(tmpdir, specs) as solver:
with pytest.raises(UnsatisfiableError) as exc:
final_state = solver.solve_final_state()
solver.solve_final_state()

if sys.platform == "darwin":
if "ARM_8" in get_cpu_info()["arch"]:
Expand Down Expand Up @@ -211,7 +211,7 @@ def test_cuda_fail_2(tmpdir, clear_cuda_version):
with env_var('CONDA_OVERRIDE_CUDA', ''):
with get_solver_cuda(tmpdir, specs) as solver:
with pytest.raises(UnsatisfiableError) as exc:
final_state = solver.solve_final_state()
solver.solve_final_state()

assert str(exc.value).strip() == dals("""The following specifications were found to be incompatible with your system:
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_cuda_constrain_unsat(tmpdir, clear_cuda_version):
with env_var('CONDA_OVERRIDE_CUDA', '8.0'):
with get_solver_cuda(tmpdir, specs) as solver:
with pytest.raises(UnsatisfiableError) as exc:
final_state = solver.solve_final_state()
solver.solve_final_state()

assert str(exc.value).strip() == dals("""The following specifications were found to be incompatible with your system:
Expand Down Expand Up @@ -287,7 +287,7 @@ def test_cuda_glibc_unsat_depend(tmpdir, clear_cuda_version):
with env_var('CONDA_OVERRIDE_CUDA', '8.0'), env_var('CONDA_OVERRIDE_GLIBC', '2.23'):
with get_solver_cuda(tmpdir, specs) as solver:
with pytest.raises(UnsatisfiableError) as exc:
final_state = solver.solve_final_state()
solver.solve_final_state()

assert str(exc.value).strip() == dals("""The following specifications were found to be incompatible with your system:
Expand All @@ -304,8 +304,8 @@ def test_cuda_glibc_unsat_constrain(tmpdir, clear_cuda_version):

with env_var('CONDA_OVERRIDE_CUDA', '10.0'), env_var('CONDA_OVERRIDE_GLIBC', '2.12'):
with get_solver_cuda(tmpdir, specs) as solver:
with pytest.raises(UnsatisfiableError) as exc:
final_state = solver.solve_final_state()
with pytest.raises(UnsatisfiableError):
solver.solve_final_state()


def test_prune_1(tmpdir):
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_subdir_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ def test_metadata_cache_works(platform=OVERRIDE_PLATFORM):
{"CONDA_PLATFORM": platform}, stack_callback=conda_tests_ctxt_mgmt_def_pol
), patch.object(CondaRepoInterface, "repodata", return_value={}) as fetcher:
sd_a = SubdirData(channel)
precs_a = tuple(sd_a.query("zlib"))
tuple(sd_a.query("zlib"))
assert fetcher.call_count == 1

sd_b = SubdirData(channel)
assert sd_b is sd_a
precs_b = tuple(sd_b.query("zlib"))
tuple(sd_b.query("zlib"))
assert fetcher.call_count == 1


Expand Down
11 changes: 5 additions & 6 deletions tests/gateways/disk/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from conda.gateways.disk.read import read_python_record
import pytest
from tests.data.env_metadata import (
METADATA_VERSION_PATHS, PATH_TEST_ENV_1, PATH_TEST_ENV_2, PATH_TEST_ENV_3, PATH_TEST_ENV_4,
__file__ as env_metadata_file,
)
ENV_METADATA_DIR = dirname(env_metadata_file)
Expand Down Expand Up @@ -190,8 +189,8 @@ def test_cherrypy_py36_osx_whl():
prefix_rec = read_python_record(prefix_path, anchor_file, "3.6")

dumped_rec = json_load(json_dump(prefix_rec.dump()))
files = dumped_rec.pop("files")
paths_data = dumped_rec.pop("paths_data")
dumped_rec.pop("files")
dumped_rec.pop("paths_data")
print(json_dump(dumped_rec))
constrains = dumped_rec.pop("constrains")
depends = dumped_rec.pop("depends")
Expand Down Expand Up @@ -392,8 +391,8 @@ def test_cherrypy_py27_osx_no_binary():
prefix_rec = read_python_record(prefix_path, anchor_file, "2.7")

dumped_rec = json_load(json_dump(prefix_rec.dump()))
files = dumped_rec.pop("files")
paths_data = dumped_rec.pop("paths_data")
dumped_rec.pop("files")
dumped_rec.pop("paths_data")
print(json_dump(dumped_rec))
constrains = dumped_rec.pop("constrains")
depends = dumped_rec.pop("depends")
Expand Down Expand Up @@ -442,7 +441,7 @@ def test_six_py27_osx_no_binary_unmanageable():

dumped_rec = json_load(json_dump(prefix_rec.dump()))
files = dumped_rec.pop("files")
paths_data = dumped_rec.pop("paths_data")
dumped_rec.pop("paths_data")
print(json_dump(dumped_rec))
assert dumped_rec == {
"build": "pypi_0",
Expand Down
4 changes: 2 additions & 2 deletions tests/gateways/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_local_file_adapter_404(self):
session = CondaSession()
test_path = "file:///some/location/doesnt/exist"
r = session.get(test_path)
with pytest.raises(HTTPError) as exc:
with pytest.raises(HTTPError):
r.raise_for_status()
assert r.status_code == 404
assert r.json()["path"] == test_path[len("file://") :]
Expand All @@ -71,7 +71,7 @@ def test_local_file_adapter_200(self):
rm_rf(test_path)


@pytest.mark.skipif(MINIO_EXE is None, reason=f"Minio server not available")
@pytest.mark.skipif(MINIO_EXE is None, reason="Minio server not available")
@pytest.mark.integration
def test_s3_server(minio_s3_server):
import boto3
Expand Down
3 changes: 0 additions & 3 deletions tests/models/test_channel.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from logging import getLogger
from os.path import join
from tempfile import gettempdir
from unittest import TestCase
from unittest.mock import patch

from conda.auxlib.ish import dals
from conda.base.constants import DEFAULT_CHANNELS
Expand Down Expand Up @@ -567,7 +565,6 @@ def test_local_channel(self):
conda_bld_path = join(gettempdir(), 'conda-bld')
mkdir_p(conda_bld_path)
try:
from functools import partial
with env_var('CONDA_CROOT', conda_bld_path, stack_callback=conda_tests_ctxt_mgmt_def_pol):
Channel._reset_state()
channel = Channel('local')
Expand Down
1 change: 0 additions & 1 deletion tests/models/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from conda.gateways.disk.delete import rm_rf
from conda.models.dist import Dist
from logging import getLogger
from unittest import TestCase

import pytest

Expand Down
8 changes: 4 additions & 4 deletions tests/models/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import unittest

from conda.exceptions import InvalidVersionSpec
from conda.models.version import VersionOrder, VersionSpec, normalized_version, ver_eval, treeify
from conda.models.version import VersionOrder, VersionSpec, normalized_version, ver_eval
import pytest


Expand Down Expand Up @@ -70,11 +70,11 @@ def test_version_order(self):
]

# check parser
versions = [(v, VersionOrder(v), l) for v, l in versions]
for s, v, l in versions:
versions = [(v, VersionOrder(v), expected) for v, expected in versions]
for s, v, expected in versions:
assert VersionOrder(v) is v
assert str(v) == s.lower().replace('-', '_')
self.assertEqual(v.version, l)
self.assertEqual(v.version, expected)
self.assertEqual(VersionOrder("0.4.1.rc"), VersionOrder(" 0.4.1.RC "))
self.assertEqual(normalized_version(" 0.4.1.RC "), VersionOrder("0.4.1.rc"))
for ver in ("", "", " ", "3.5&1", "5.5++", "5.5..mw", "!", "a!1.0", "a!b!1.0"):
Expand Down
1 change: 0 additions & 1 deletion tests/plugins/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import pluggy
import pytest
import sys

Expand Down
Loading

0 comments on commit 6c4a226

Please sign in to comment.