Skip to content

Commit 4d185b8

Browse files
fix: move PLC0415 inline imports to top-level in 11 test/config files (#12295)
Co-authored-by: RayBB <RayBB@users.noreply.github.com>
1 parent bb2d5e1 commit 4d185b8

12 files changed

Lines changed: 29 additions & 53 deletions

File tree

openlibrary/conftest.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
import web
55

6+
from infogami import config
67
from infogami.infobase.tests.pytest_wildcard import Wildcard
78
from infogami.utils import template
89
from infogami.utils.view import render_template as infobase_render_template
@@ -15,6 +16,7 @@
1516
from openlibrary.mocks.mock_memcache import (
1617
mock_memcache, # noqa: F401 side effects may be needed
1718
)
19+
from openlibrary.utils.request_context import RequestContextVars, req_context
1820

1921

2022
@pytest.fixture(autouse=True)
@@ -54,10 +56,6 @@ def setup_db_config():
5456
which is necessary for the context variable infrastructure being added. Without this, tests may fail due to
5557
missing or incorrect database parameters when initializing site.
5658
"""
57-
import web
58-
59-
from infogami import config
60-
6159
# Set web.config.db_parameters for OLConnection
6260
web.config.db_parameters = {}
6361

@@ -89,7 +87,6 @@ def request_context_fixture():
8987
Provides defaults and allows tests to override any subset of fields.
9088
Automatically cleans up after the test.
9189
"""
92-
from openlibrary.utils.request_context import RequestContextVars, req_context
9390

9491
tokens = []
9592

@@ -149,12 +146,12 @@ def render_template(request):
149146

150147
# ol_infobase.init_plugin call is failing when trying to import plugins.openlibrary.code.
151148
# monkeypatch to avoid that.
152-
from openlibrary.plugins import ol_infobase
149+
from openlibrary.plugins import ol_infobase # noqa: PLC0415
153150

154151
init_plugin = ol_infobase.init_plugin
155152
ol_infobase.init_plugin = lambda: None
156153

157-
from openlibrary.plugins.openlibrary import code
154+
from openlibrary.plugins.openlibrary import code # noqa: PLC0415
158155

159156
web.config.db_parameters = {}
160157
code.setup_template_globals()

openlibrary/plugins/admin/tests/test_services.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
Tests for the services module used by the admin interface.
33
"""
44

5+
from .. import services
6+
57

68
def test_loader(serviceconfig):
79
"Make sure services are loaded"
8-
from .. import services
910

10-
services = services.load_all(serviceconfig, "http://nagios.url")
11-
assert len(list(services)) == 2
12-
s = sorted(services)
11+
loaded_services = services.load_all(serviceconfig, "http://nagios.url")
12+
assert len(list(loaded_services)) == 2
13+
s = sorted(loaded_services)
1314
assert s[0] == "ol-web0"
1415
assert s[1] == "ol-web1"
15-
assert services["ol-web0"][0].name == "7071-ol-gunicorn"
16-
assert services["ol-web0"][1].name == "7060-memcached"
17-
assert services["ol-web1"][0].name == "7072-ol-gunicorn"
18-
assert services["ol-web1"][1].name == "7061-memcached"
16+
assert loaded_services["ol-web0"][0].name == "7071-ol-gunicorn"
17+
assert loaded_services["ol-web0"][1].name == "7060-memcached"
18+
assert loaded_services["ol-web1"][0].name == "7072-ol-gunicorn"
19+
assert loaded_services["ol-web1"][1].name == "7061-memcached"

openlibrary/plugins/upstream/tests/test_merge_authors.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import web
22

33
from infogami.infobase import client, common
4+
from openlibrary.plugins.upstream import models
45
from openlibrary.plugins.upstream.merge_authors import (
56
AuthorMergeEngine,
67
AuthorRedirectEngine,
@@ -16,10 +17,8 @@
1617
def setup_module(mod):
1718
# delegate.fakeload()
1819

19-
# models module imports openlibrary.code, which imports ol_infobase and that expects db_parameters.
20+
# Configure test database settings before calling models.setup().
2021
web.config.db_parameters = {"dbn": "sqlite", "db": ":memory:"}
21-
from openlibrary.plugins.upstream import models
22-
2322
models.setup()
2423

2524

openlibrary/plugins/upstream/tests/test_utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from collections.abc import Sequence
2+
from unittest.mock import MagicMock, patch
23

34
import pytest
45
import web
56

7+
from infogami import config
68
from openlibrary.catalog.add_book.tests.conftest import add_languages # noqa: F401
79
from openlibrary.mocks.mock_infobase import MockSite
810

@@ -133,8 +135,6 @@ def test_canonical_url():
133135

134136

135137
def test_get_coverstore_url(monkeypatch):
136-
from infogami import config
137-
138138
monkeypatch.delattr(config, "coverstore_url", raising=False)
139139
assert utils.get_coverstore_url() == "https://covers.openlibrary.org"
140140

@@ -342,8 +342,6 @@ def test_render_cached_macro_evicts_cache_on_error(monkeypatch):
342342
`work_search` returned a Solr error in RawQueryCarousel), the bad result
343343
must be evicted from memcache so subsequent requests get a fresh attempt.
344344
"""
345-
from unittest.mock import MagicMock, patch
346-
347345
# Simulate the rendered macro result indicating failure
348346
error_page = {"do_not_cache": "True", "content": "<div></div>"}
349347

openlibrary/plugins/worksearch/schemes/tests/test_works.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import patch
22

33
import pytest
4+
import web
45

56
from openlibrary.plugins.worksearch.schemes.works import WorkSearchScheme
67

@@ -136,8 +137,6 @@ def test_process_user_query(query, parsed_query):
136137

137138
@pytest.mark.parametrize(("query", "edQuery"), EDITION_KEY_TESTS.items())
138139
def test_q_to_solr_params_edition_key(query, edQuery):
139-
import web
140-
141140
web.ctx.lang = "en"
142141
s = WorkSearchScheme()
143142

openlibrary/plugins/worksearch/tests/test_autocomplete.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import web
55

6-
from openlibrary.plugins.worksearch.autocomplete import autocomplete, works_autocomplete
6+
from openlibrary.plugins.worksearch.autocomplete import autocomplete, series_autocomplete, works_autocomplete
77
from openlibrary.utils.solr import Solr
88

99

@@ -106,8 +106,6 @@ def test_works_autocomplete():
106106

107107

108108
def test_series_autocomplete_olid():
109-
from openlibrary.plugins.worksearch.autocomplete import series_autocomplete
110-
111109
ac = series_autocomplete()
112110
with (
113111
patch("web.input") as mock_web_input,
@@ -124,8 +122,6 @@ def test_series_autocomplete_olid():
124122

125123

126124
def test_series_autocomplete_url():
127-
from openlibrary.plugins.worksearch.autocomplete import series_autocomplete
128-
129125
ac = series_autocomplete()
130126
with (
131127
patch("web.input") as mock_web_input,

openlibrary/tests/core/test_models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
2+
import web
23

34
from openlibrary.core import models
5+
from openlibrary.mocks import mock_infobase
46

57

68
class MockSite:
@@ -151,10 +153,6 @@ def test_resolve_redirect_chain(self, monkeypatch):
151153
work3 = {"key": work3_key, "location": work4_key, "type": type_redir}
152154
work4 = {"key": work4_key, "type": type_work}
153155

154-
import web
155-
156-
from openlibrary.mocks import mock_infobase
157-
158156
site = mock_infobase.MockSite()
159157
site.save(web.storage(work1))
160158
site.save(web.storage(work2))

openlibrary/tests/fastapi/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from fastapi.testclient import TestClient
77

88
from openlibrary.fastapi.auth import AuthenticatedUser, require_authenticated_user
9+
from openlibrary.plugins.worksearch.code import SearchResponse
910

1011

1112
@pytest.fixture
1213
def fastapi_client():
1314
"""Create a test client for the FastAPI app."""
1415
with patch("openlibrary.asgi_app.set_context_from_fastapi", autospec=True):
15-
from openlibrary.asgi_app import create_app
16+
from openlibrary.asgi_app import create_app # noqa: PLC0415
1617

1718
app = create_app()
1819
client = TestClient(app)
@@ -126,7 +127,6 @@ def _default_search_response():
126127

127128
def _default_subjects_response():
128129
"""Default mock response for subjects search."""
129-
from openlibrary.plugins.worksearch.code import SearchResponse
130130

131131
return SearchResponse(
132132
facet_counts=None,

openlibrary/tests/fastapi/test_search.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Basic tests for the FastAPI search endpoint."""
22

33
import json
4+
from urllib.parse import urlencode
45

56
import pytest
67

@@ -28,8 +29,6 @@ def search(client, **params):
2829
This helper provides a cleaner interface for making search requests
2930
by accepting keyword arguments directly instead of building URL strings.
3031
"""
31-
from urllib.parse import urlencode
32-
3332
query_string = urlencode(params, doseq=True)
3433
return client.get(f"/search.json?{query_string}")
3534

openlibrary/tests/fastapi/test_trending.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""Tests for the /trending/{period}.json FastAPI endpoint (internal API)."""
22

3+
from typing import get_args
34
from unittest.mock import patch
45

56
import pytest
67

8+
from openlibrary.fastapi.internal.api import TrendingPeriod
9+
from openlibrary.views.loanstats import SINCE_DAYS
10+
711
MOCK_WORKS = [
812
{"key": "/works/OL1W", "title": "Popular Book 1", "author_name": ["Author A"]},
913
{"key": "/works/OL2W", "title": "Popular Book 2", "author_name": ["Author B"]},
@@ -109,11 +113,6 @@ def test_limit_defaults_to_100(self, fastapi_client, mock_get_trending_books):
109113
assert mock_get_trending_books.call_args.kwargs["limit"] == 100
110114

111115
def test_trending_period_literal_matches_since_days(self):
112-
from typing import get_args
113-
114-
from openlibrary.fastapi.internal.api import TrendingPeriod
115-
from openlibrary.views.loanstats import SINCE_DAYS
116-
117116
literal_keys = set(get_args(TrendingPeriod))
118117
since_days_keys = set(SINCE_DAYS.keys())
119118

0 commit comments

Comments
 (0)