From b02766e201004a7565e1d0266fef9b4a962c6be2 Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Tue, 10 Nov 2026 14:33:20 -0500 Subject: [PATCH] PYTHON-6241 Enable ruff PERF rules --- bson/son.py | 2 +- pymongo/asynchronous/database.py | 2 +- pymongo/mongo_client_shared.py | 2 +- pymongo/synchronous/database.py | 2 +- pyproject.toml | 1 - test/asynchronous/test_collection.py | 4 ++-- test/asynchronous/test_database.py | 2 +- test/asynchronous/test_discovery_and_monitoring.py | 4 ++-- test/asynchronous/test_read_preferences.py | 4 ++-- test/asynchronous/test_sdam_monitoring_spec.py | 2 +- test/test_collection.py | 4 ++-- test/test_database.py | 2 +- test/test_discovery_and_monitoring.py | 4 ++-- test/test_read_preferences.py | 4 ++-- test/test_sdam_monitoring_spec.py | 2 +- test/test_typing.py | 4 +--- 16 files changed, 21 insertions(+), 24 deletions(-) diff --git a/bson/son.py b/bson/son.py index 33d67b4dc6..1f09bed1ae 100644 --- a/bson/son.py +++ b/bson/son.py @@ -116,7 +116,7 @@ def itervalues(self) -> Iterator[_Value]: DeprecationWarning, stacklevel=2, ) - for _, v in self.items(): + for _, v in self.items(): # noqa: PERF102 yield v def values(self) -> list[_Value]: # type: ignore[override] diff --git a/pymongo/asynchronous/database.py b/pymongo/asynchronous/database.py index fce17a52bb..2b732ef159 100644 --- a/pymongo/asynchronous/database.py +++ b/pymongo/asynchronous/database.py @@ -1412,7 +1412,7 @@ async def validate_collection( raise CollectionInvalid(f"{name} invalid: {info}") # Sharded results elif "raw" in result: - for _, res in result["raw"].items(): + for res in result["raw"].values(): if "result" in res: info = res["result"] if info.find("exception") != -1 or info.find("corrupt") != -1: diff --git a/pymongo/mongo_client_shared.py b/pymongo/mongo_client_shared.py index 55b4f655f4..a46eb57928 100644 --- a/pymongo/mongo_client_shared.py +++ b/pymongo/mongo_client_shared.py @@ -101,7 +101,7 @@ def _after_fork_child(clients: Mapping[Any, Any]) -> None: _release_locks() # Perform cleanup in clients (i.e. get rid of topology) - for _, client in clients.items(): + for client in clients.values(): client._after_fork() diff --git a/pymongo/synchronous/database.py b/pymongo/synchronous/database.py index cbf9d4129e..1346371ab4 100644 --- a/pymongo/synchronous/database.py +++ b/pymongo/synchronous/database.py @@ -1409,7 +1409,7 @@ def validate_collection( raise CollectionInvalid(f"{name} invalid: {info}") # Sharded results elif "raw" in result: - for _, res in result["raw"].items(): + for res in result["raw"].values(): if "result" in res: info = res["result"] if info.find("exception") != -1 or info.find("corrupt") != -1: diff --git a/pyproject.toml b/pyproject.toml index 3ccb19d8a0..2f9de571dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -249,7 +249,6 @@ ignore = [ # These are all new defaults added in ruff 0.16.0. Individual tickets linked from PYTHON-5964 track enabling each one. "FURB", # refurb "LOG", # flake8-logging - "PERF", # Perflint "PYI", # flake8-pyi "TC", # flake8-type-checking "TRY", # tryceratops diff --git a/test/asynchronous/test_collection.py b/test/asynchronous/test_collection.py index d63952cbe2..0a0fa823db 100644 --- a/test/asynchronous/test_collection.py +++ b/test/asynchronous/test_collection.py @@ -504,7 +504,7 @@ async def test_index_2dsphere(self): await db.coll.drop_indexes() self.assertEqual("geo_2dsphere", await db.coll.create_index([("geo", GEOSPHERE)])) - for dummy, info in (await db.coll.index_information()).items(): + for info in (await db.coll.index_information()).values(): field, idx_type = info["key"][0] if field == "geo" and idx_type == "2dsphere": break @@ -523,7 +523,7 @@ async def test_index_hashed(self): await db.coll.drop_indexes() self.assertEqual("a_hashed", await db.coll.create_index([("a", HASHED)])) - for dummy, info in (await db.coll.index_information()).items(): + for info in (await db.coll.index_information()).values(): field, idx_type = info["key"][0] if field == "a" and idx_type == "hashed": break diff --git a/test/asynchronous/test_database.py b/test/asynchronous/test_database.py index ae3b19073e..1a67ddbcb1 100644 --- a/test/asynchronous/test_database.py +++ b/test/asynchronous/test_database.py @@ -487,7 +487,7 @@ async def test_id_ordering(self): ) cursor = db.coll.find() async for x in cursor: - for k, _v in x.items(): + for k in x.keys(): self.assertEqual(k, "_id") break diff --git a/test/asynchronous/test_discovery_and_monitoring.py b/test/asynchronous/test_discovery_and_monitoring.py index ca44bfbac2..3d4c0b8e1b 100644 --- a/test/asynchronous/test_discovery_and_monitoring.py +++ b/test/asynchronous/test_discovery_and_monitoring.py @@ -561,7 +561,7 @@ async def test_rtt_connection_is_enabled_stream(self): await client.admin.command("ping") def predicate(): - for _, server in client._topology._servers.items(): + for server in client._topology._servers.values(): monitor = server._monitor if not monitor._stream: return False @@ -603,7 +603,7 @@ async def test_rtt_connection_is_disabled_auto(self): async def assert_rtt_connection_is_disabled(self, client): await client.admin.command("ping") - for _, server in client._topology._servers.items(): + for server in client._topology._servers.values(): monitor = server._monitor self.assertFalse(monitor._stream) if _IS_SYNC: diff --git a/test/asynchronous/test_read_preferences.py b/test/asynchronous/test_read_preferences.py index a193b9dedd..5ff9de30f7 100644 --- a/test/asynchronous/test_read_preferences.py +++ b/test/asynchronous/test_read_preferences.py @@ -578,7 +578,7 @@ def test_read_preference_hedge_deprecated(self): "secondaryPreferred": SecondaryPreferred, "nearest": Nearest, } - for _, cls in cases.items(): + for cls in cases.values(): with self.assertRaises(DeprecationWarning): cls(hedge={"enabled": True}) @@ -593,7 +593,7 @@ async def test_send_hedge(self): listener = OvertCommandListener() client = await self.async_rs_client(event_listeners=[listener]) await client.admin.command("ping") - for _mode, cls in cases.items(): + for cls in cases.values(): with _ignore_deprecations(): pref = cls(hedge={"enabled": True}) coll = client.test.get_collection("coll", read_preference=pref) diff --git a/test/asynchronous/test_sdam_monitoring_spec.py b/test/asynchronous/test_sdam_monitoring_spec.py index 692625e326..33e81d672c 100644 --- a/test/asynchronous/test_sdam_monitoring_spec.py +++ b/test/asynchronous/test_sdam_monitoring_spec.py @@ -67,7 +67,7 @@ def compare_topology_descriptions(expected, actual): if len(expected) != len(actual): return False for exp_server in expected: - for _address, actual_server in actual.items(): + for actual_server in actual.values(): if compare_server_descriptions(exp_server, actual_server): break else: diff --git a/test/test_collection.py b/test/test_collection.py index 58589f5916..0732605dd7 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -494,7 +494,7 @@ def test_index_2dsphere(self): db.coll.drop_indexes() self.assertEqual("geo_2dsphere", db.coll.create_index([("geo", GEOSPHERE)])) - for dummy, info in (db.coll.index_information()).items(): + for info in (db.coll.index_information()).values(): field, idx_type = info["key"][0] if field == "geo" and idx_type == "2dsphere": break @@ -513,7 +513,7 @@ def test_index_hashed(self): db.coll.drop_indexes() self.assertEqual("a_hashed", db.coll.create_index([("a", HASHED)])) - for dummy, info in (db.coll.index_information()).items(): + for info in (db.coll.index_information()).values(): field, idx_type = info["key"][0] if field == "a" and idx_type == "hashed": break diff --git a/test/test_database.py b/test/test_database.py index 849cf41cb6..bced1879ba 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -482,7 +482,7 @@ def test_id_ordering(self): ) cursor = db.coll.find() for x in cursor: - for k, _v in x.items(): + for k in x.keys(): self.assertEqual(k, "_id") break diff --git a/test/test_discovery_and_monitoring.py b/test/test_discovery_and_monitoring.py index c310d447e1..934d525e29 100644 --- a/test/test_discovery_and_monitoring.py +++ b/test/test_discovery_and_monitoring.py @@ -558,7 +558,7 @@ def test_rtt_connection_is_enabled_stream(self): client.admin.command("ping") def predicate(): - for _, server in client._topology._servers.items(): + for server in client._topology._servers.values(): monitor = server._monitor if not monitor._stream: return False @@ -600,7 +600,7 @@ def test_rtt_connection_is_disabled_auto(self): def assert_rtt_connection_is_disabled(self, client): client.admin.command("ping") - for _, server in client._topology._servers.items(): + for server in client._topology._servers.values(): monitor = server._monitor self.assertFalse(monitor._stream) if _IS_SYNC: diff --git a/test/test_read_preferences.py b/test/test_read_preferences.py index a7afab90fc..fdd1bd9f8b 100644 --- a/test/test_read_preferences.py +++ b/test/test_read_preferences.py @@ -558,7 +558,7 @@ def test_read_preference_hedge_deprecated(self): "secondaryPreferred": SecondaryPreferred, "nearest": Nearest, } - for _, cls in cases.items(): + for cls in cases.values(): with self.assertRaises(DeprecationWarning): cls(hedge={"enabled": True}) @@ -573,7 +573,7 @@ def test_send_hedge(self): listener = OvertCommandListener() client = self.rs_client(event_listeners=[listener]) client.admin.command("ping") - for _mode, cls in cases.items(): + for cls in cases.values(): with _ignore_deprecations(): pref = cls(hedge={"enabled": True}) coll = client.test.get_collection("coll", read_preference=pref) diff --git a/test/test_sdam_monitoring_spec.py b/test/test_sdam_monitoring_spec.py index 6e63f94ea3..95ce620139 100644 --- a/test/test_sdam_monitoring_spec.py +++ b/test/test_sdam_monitoring_spec.py @@ -67,7 +67,7 @@ def compare_topology_descriptions(expected, actual): if len(expected) != len(actual): return False for exp_server in expected: - for _address, actual_server in actual.items(): + for actual_server in actual.values(): if compare_server_descriptions(exp_server, actual_server): break else: diff --git a/test/test_typing.py b/test/test_typing.py index dc6d2c8b14..78523a0f4a 100644 --- a/test/test_typing.py +++ b/test/test_typing.py @@ -174,13 +174,11 @@ def test_bulk_write(self) -> None: coll: Collection[Movie] = self.coll requests: list[InsertOne[Movie]] = [InsertOne(Movie(name="American Graffiti", year=1973))] self.assertTrue(coll.bulk_write(requests).acknowledged) - new_requests: list[InsertOne[Movie] | ReplaceOne[Movie]] = [] input_list: list[InsertOne[Movie] | ReplaceOne[Movie]] = [ InsertOne(Movie(name="American Graffiti", year=1973)), ReplaceOne({}, Movie(name="American Graffiti", year=1973)), ] - for i in input_list: - new_requests.append(i) + new_requests: list[InsertOne[Movie] | ReplaceOne[Movie]] = list.copy(input_list) self.assertTrue(coll.bulk_write(new_requests).acknowledged) # Because ReplaceOne is not generic, type checking is not enforced for ReplaceOne in the first example.