Skip to content

Commit 0594303

Browse files
authored
Merge pull request #1372 from newrelic/fix-linter-pyteststyle
Enable linter flake8-pytest-style
2 parents dfd95ab + 7e6dda8 commit 0594303

File tree

79 files changed

+268
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+268
-265
lines changed

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ select = [
5959
"G", # flake8-logging-format
6060
"INP", # flake8-no-pep420
6161
"PYI", # flake8-pyi
62-
# "PT", # flake8-pytest-style
62+
"PT", # flake8-pytest-style
6363
"Q", # flake8-quotes
6464
"RSE", # flake8-raise
6565
# "RET", # flake8-return
@@ -101,13 +101,13 @@ select = [
101101

102102
ignore = [
103103
# Temporarily disabled rules
104-
"RUF100", # unused-noqa (TODO: remove this once all linters are enabled)
105104
"PERF203", # try-except-in-loop (most of these are unavoidable)
106105
"S110", # try-except-pass (Bandit wants us to log the exception, which is usually pointless. Spot check these later)
107106
"PLW0603", # global-statement (this is currently used extensively)
108107
"PLW2901", # redefined-loop-name (frequently used, not generally an issue)
109108
"PLR", # Pylint Recommendations (too many to fix all at once)
110109
"E722", # bare-except (too many to fix all at once)
110+
"PT012", # pytest-raises-with-multiple-statements (too many to fix all at once)
111111
# Permanently disabled rules
112112
"UP006", # non-pep585-annotation (not compatible with Python 3.7 or 3.8)
113113
"D203", # incorrect-blank-line-before-class
@@ -144,6 +144,9 @@ ignore = [
144144
"PLR2004", # magic-value-comparison (comparing to constant values)
145145
"ASYNC251", # blocking-sleep-in-async-function (acceptable in tests)
146146
"B904", # raise-without-from-inside-except (not necessary in tests)
147+
"PT006", # pytest-parametrize-names-wrong-type (style choice)
148+
"PT007", # pytest-parametrize-values-wrong-type (style choice)
149+
"PT011", # pytest-raises-too-broad (we frequently test for generic errors)
147150
]
148151
"newrelic/admin/*" = [
149152
# Disabled rules in admin scripts

tests/agent_features/test_async_generator_trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ async def _test():
273273
await gen.asend(None)
274274

275275
assert await gen.asend("foobar") == "foobar"
276-
assert _received and _received[0] == "foobar"
276+
assert _received[0] == "foobar"
277277

278278
# finish consumption of the coroutine if necessary
279279
async for _ in gen:

tests/agent_features/test_configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
SKIP_IF_NOT_PY311 = pytest.mark.skipif(sys.version_info < (3, 11), reason="TOML not in the standard library.")
4848

4949

50-
@pytest.fixture(scope="function")
50+
@pytest.fixture
5151
def collector_available_fixture():
5252
# Disable fixture that requires real application to exist for this file
5353
pass

tests/agent_features/test_logs_in_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def test_newrelic_logger_error_inside_transaction_with_stack_trace(log_buffer_wi
241241
assert filename.endswith("/test_logs_in_context.py")
242242
assert isinstance(line_number, int)
243243
assert isinstance(stack_trace, str)
244-
assert stack_trace and stack_trace == expected_stack_trace
244+
assert stack_trace == expected_stack_trace
245245

246246
expected = {
247247
"entity.name": "Python Agent Test (agent_features)",
@@ -329,7 +329,7 @@ def test_newrelic_logger_error_outside_transaction_with_stack_trace(log_buffer_w
329329
assert filename.endswith("/test_logs_in_context.py")
330330
assert isinstance(line_number, int)
331331
assert isinstance(stack_trace, str)
332-
assert stack_trace and stack_trace == expected_stack_trace
332+
assert stack_trace == expected_stack_trace
333333

334334
expected = {
335335
"entity.name": "Python Agent Test (agent_features)",

tests/agent_features/test_metric_normalization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def core_app(collector_agent_registration):
4040
return app._agent.application(app.name)
4141

4242

43-
@pytest.fixture(scope="function")
43+
@pytest.fixture
4444
def rules_engine_fixture(core_app):
4545
rules_engine = core_app._rules_engine
4646
previous_rules = rules_engine["metric"]

tests/agent_features/test_serverless_mode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from newrelic.core.config import global_settings
2929

3030

31-
@pytest.fixture(scope="function")
31+
@pytest.fixture
3232
def serverless_application(request):
3333
settings = global_settings()
3434
orig = settings.serverless_mode.enabled

tests/agent_features/test_web_transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_base_web_transaction(use_bytes):
141141
transaction.process_response(200, response_headers)
142142

143143

144-
@pytest.fixture()
144+
@pytest.fixture
145145
def validate_no_garbage():
146146
yield
147147

tests/agent_streaming/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def wait(self, *args, **kwargs):
6868
return super().wait(*args, **kwargs)
6969

7070

71-
@pytest.fixture(scope="function")
71+
@pytest.fixture
7272
def buffer_empty_event(monkeypatch):
7373
event = threading.Event()
7474

@@ -85,23 +85,23 @@ def batching(request):
8585
return request.param
8686

8787

88-
@pytest.fixture(scope="function")
88+
@pytest.fixture
8989
def spans_received():
9090
from _test_handler import SPANS_RECEIVED
9191

9292
SPANS_RECEIVED.clear()
9393
return SPANS_RECEIVED
9494

9595

96-
@pytest.fixture(scope="function")
96+
@pytest.fixture
9797
def span_batches_received():
9898
from _test_handler import SPAN_BATCHES_RECEIVED
9999

100100
SPAN_BATCHES_RECEIVED.clear()
101101
return SPAN_BATCHES_RECEIVED
102102

103103

104-
@pytest.fixture(scope="function")
104+
@pytest.fixture
105105
def spans_processed_event():
106106
from _test_handler import SPANS_PROCESSED_EVENT
107107

tests/agent_streaming/test_infinite_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
CONDITION_CLS = type(threading.Condition())
3232

3333

34-
@pytest.fixture()
34+
@pytest.fixture
3535
def app():
3636
app = Application("Python Agent Test (Infinite Tracing)")
3737
yield app

tests/agent_streaming/test_stream_buffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def stop_iteration_condition(*args, **kwargs):
2929
return StopIterationOnWait(*args, **kwargs)
3030

3131

32-
@pytest.fixture(scope="function")
32+
@pytest.fixture
3333
def stop_iteration_on_wait(monkeypatch):
3434
monkeypatch.setattr(StreamBuffer, "condition", stop_iteration_condition)
3535

tests/agent_unittests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class FakeProtos:
4444
sys.modules["newrelic.core.infinite_tracing_pb2"] = FakeProtos
4545

4646

47-
@pytest.fixture(scope="function")
47+
@pytest.fixture
4848
def global_settings(request, monkeypatch):
4949
ini_contents = request.getfixturevalue("ini")
5050

tests/agent_unittests/test_encoding_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def test_camel_case(input_, expected, upper):
4242
@pytest.mark.parametrize(
4343
"input_,expected",
4444
[
45-
("", ""),
4645
("", ""),
4746
("my_string", "my_string"),
4847
("myString", "my_string"),

tests/agent_unittests/test_package_version_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@
4141
SKIP_IF_NOT_PY310_PLUS = pytest.mark.skipif(not IS_PY310_PLUS, reason="These features were added in 3.10+")
4242

4343

44-
@pytest.fixture(scope="function", autouse=True)
44+
@pytest.fixture(autouse=True)
4545
def patched_pytest_module(monkeypatch):
4646
for attr in VERSION_ATTRS:
4747
if hasattr(pytest, attr):
4848
monkeypatch.delattr(pytest, attr)
4949

50-
yield pytest
50+
return pytest
5151

5252

53-
@pytest.fixture(scope="function", autouse=True)
53+
@pytest.fixture(autouse=True)
5454
def cleared_package_version_cache():
5555
"""Ensure cache is empty before every test to exercise code paths."""
5656
_get_package_version.cache_clear()
@@ -163,7 +163,8 @@ def version(self):
163163
monkeypatch.setattr(pytest, "version", FakeModule.version, raising=False)
164164

165165
version = get_package_version("pytest")
166-
assert version not in NULL_VERSIONS and isinstance(version, str), version
166+
assert version not in NULL_VERSIONS, version
167+
assert isinstance(version, str), version
167168

168169

169170
# This test checks to see if the version is a property of the class
@@ -181,4 +182,5 @@ def version(self):
181182
monkeypatch.setattr(pytest, "version_tuple", (1, 2, 3), raising=False)
182183

183184
version = get_package_version("pytest")
184-
assert version not in NULL_VERSIONS and isinstance(version, str), version
185+
assert version not in NULL_VERSIONS, version
186+
assert isinstance(version, str), version

tests/agent_unittests/test_sampler_metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def cpu_data_source():
4646
@pytest.fixture
4747
def memory_data_source():
4848
sampler = memory_usage_data_source(settings=())["factory"](environ=())
49-
yield sampler
49+
return sampler
5050

5151

5252
PID = os.getpid()

tests/agent_unittests/test_trace_cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DummyTrace:
2525
pass
2626

2727

28-
@pytest.fixture(scope="function")
28+
@pytest.fixture
2929
def trace_cache():
3030
return TraceCache()
3131

@@ -54,7 +54,7 @@ def test_trace_cache_methods(trace_cache):
5454
assert len(list(trace_cache.values())) == 1
5555

5656

57-
@pytest.fixture(scope="function")
57+
@pytest.fixture
5858
def iterate_trace_cache(trace_cache):
5959
def _iterate_trace_cache(shutdown):
6060
while True:
@@ -70,7 +70,7 @@ def _iterate_trace_cache(shutdown):
7070
return _iterate_trace_cache
7171

7272

73-
@pytest.fixture(scope="function")
73+
@pytest.fixture
7474
def change_weakref_dict_size(trace_cache):
7575
def _change_weakref_dict_size(shutdown, obj_refs):
7676
"""

tests/agent_unittests/test_wrappers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from newrelic.common.object_wrapper import function_wrapper
1818

1919

20-
@pytest.fixture(scope="function")
20+
@pytest.fixture
2121
def wrapper():
2222
@function_wrapper
2323
def _wrapper(wrapped, instance, args, kwargs):
@@ -26,7 +26,7 @@ def _wrapper(wrapped, instance, args, kwargs):
2626
return _wrapper
2727

2828

29-
@pytest.fixture(scope="function")
29+
@pytest.fixture
3030
def wrapped_function(wrapper):
3131
@wrapper
3232
def wrapped():

tests/application_celery/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def celery_worker_parameters():
4242

4343
@pytest.fixture(scope="session", autouse=True)
4444
def celery_worker_available(celery_session_worker):
45-
yield celery_session_worker
45+
return celery_session_worker

tests/cross_agent/fixtures/ecs_container_id/ecs_mock_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ def bad_response_get(self):
117117
self.wfile.write(response)
118118

119119

120-
@pytest.fixture(scope="function")
120+
@pytest.fixture
121121
def mock_server():
122122
with MockExternalHTTPServer(handler=simple_get) as mock_server:
123123
yield mock_server
124124

125125

126-
@pytest.fixture(scope="function")
126+
@pytest.fixture
127127
def bad_response_mock_server():
128128
with MockExternalHTTPServer(handler=bad_response_get) as bad_response_mock_server:
129129
yield bad_response_mock_server

tests/cross_agent/fixtures/rum_loader_insertion_location/no_end_header.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
22
<!--
33
Copyright 2010 New Relic, Inc.
44
@@ -15,8 +15,10 @@
1515
limitations under the License.
1616
-->
1717

18-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
19-
<head>EXPECTED_RUM_LOADER_LOCATION<body>
20-
Cribbed from the Java agent
21-
</body>
22-
</html>
18+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
19+
<head>EXPECTED_RUM_LOADER_LOCATION
20+
<body>
21+
Cribbed from the Java agent
22+
</body>
23+
<!-- Missing end header tag -->
24+
</html>

tests/datastore_cassandradriver/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
)
4141

4242

43-
@pytest.fixture(scope="function", params=["Libev", "AsyncCore", "Twisted"])
43+
@pytest.fixture(params=["Libev", "AsyncCore", "Twisted"])
4444
def connection_class(request):
4545
# Configure tests to run against a specific async reactor.
4646
reactor_name = request.param
@@ -62,7 +62,7 @@ def connection_class(request):
6262
return Connection
6363

6464

65-
@pytest.fixture(scope="function")
65+
@pytest.fixture
6666
def cluster_options(connection_class):
6767
from cassandra.cluster import ExecutionProfile
6868
from cassandra.policies import RoundRobinPolicy
@@ -77,12 +77,12 @@ def cluster_options(connection_class):
7777
"connection_class": connection_class,
7878
"protocol_version": 4,
7979
}
80-
yield cluster_options
80+
return cluster_options
8181

8282

83-
@pytest.fixture(scope="function")
83+
@pytest.fixture
8484
def cluster(cluster_options):
8585
from cassandra.cluster import Cluster
8686

8787
cluster = Cluster(**cluster_options)
88-
yield cluster
88+
return cluster

tests/datastore_cassandradriver/test_cassandra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
REPLICATION_STRATEGY = "{ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"
3030

3131

32-
@pytest.fixture(scope="function")
32+
@pytest.fixture
3333
def exercise(cluster):
3434
def _exercise(is_async=False):
3535
with cluster.connect() as session:

tests/datastore_cassandradriver/test_cqlengine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ABCModel(Model):
4141
b = columns.Double()
4242

4343

44-
@pytest.fixture(scope="function")
44+
@pytest.fixture
4545
def exercise(cluster_options):
4646
# Silence warning from cqlengine when creating tables
4747
os.environ["CQLENG_ALLOW_SCHEMA_MANAGEMENT"] = "true"

tests/datastore_elasticsearch/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
RUN_IF_V7_OR_BELOW = pytest.mark.skipif(not IS_V7_OR_BELOW, reason="Unsupported for elasticsearch<=7")
4646

4747

48-
@pytest.fixture(scope="function")
48+
@pytest.fixture
4949
def client():
5050
from elasticsearch import Elasticsearch
5151

@@ -54,7 +54,7 @@ def client():
5454
_client.close()
5555

5656

57-
@pytest.fixture(scope="function")
57+
@pytest.fixture
5858
def async_client(loop):
5959
from elasticsearch import AsyncElasticsearch
6060

0 commit comments

Comments
 (0)