Skip to content

Commit dea1427

Browse files
committed
Merge branch 'master' into doctest_fail_fast
2 parents 5647e7d + 287ee20 commit dea1427

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ Features added
1919
Bugs fixed
2020
----------
2121

22+
* #13392: Fix argument type for ``jieba.load_userdict()``.
23+
2224
Testing
2325
-------

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ docs = [
9292
"sphinxcontrib-websupport",
9393
]
9494
lint = [
95-
"ruff==0.9.7",
95+
"ruff==0.9.9",
9696
"mypy==1.15.0",
9797
"sphinx-lint>=0.9",
9898
"types-colorama==0.4.15.20240311",
@@ -102,7 +102,7 @@ lint = [
102102
"types-Pygments==2.19.0.20250219",
103103
"types-requests==2.32.0.20241016", # align with requests
104104
"types-urllib3==1.26.25.14",
105-
"pyright==1.1.394",
105+
"pyright==1.1.395",
106106
"pytest>=8.0",
107107
"pypi-attestations==0.0.21",
108108
"betterproto==2.0.0b6",

sphinx/search/zh.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,33 @@
44

55
import re
66
from pathlib import Path
7+
from typing import TYPE_CHECKING
78

89
import snowballstemmer
910

1011
from sphinx.search import SearchLanguage
1112

13+
if TYPE_CHECKING:
14+
from collections.abc import Iterator
15+
1216
try:
1317
import jieba # type: ignore[import-not-found]
14-
15-
JIEBA = True
16-
JIEBA_DEFAULT_DICT = Path(jieba.__file__).parent / jieba.DEFAULT_DICT_NAME
18+
from jieba import cut_for_search
19+
from jieba import load_userdict as jieba_load_userdict
1720
except ImportError:
18-
JIEBA = False
19-
JIEBA_DEFAULT_DICT = Path()
21+
JIEBA_DEFAULT_DICT = ''
22+
23+
def jieba_load_userdict(f: str) -> None:
24+
pass
25+
26+
def cut_for_search(sentence: str, HMM: bool = True) -> Iterator[str]:
27+
yield from ()
28+
29+
else:
30+
JIEBA_DEFAULT_DICT = (
31+
Path(jieba.__file__, '..', jieba.DEFAULT_DICT_NAME).resolve().as_posix()
32+
)
33+
del jieba
2034

2135
english_stopwords = {
2236
'a', 'and', 'are', 'as', 'at',
@@ -231,18 +245,14 @@ def __init__(self, options: dict[str, str]) -> None:
231245
self.latin_terms: set[str] = set()
232246

233247
def init(self, options: dict[str, str]) -> None:
234-
if JIEBA:
235-
dict_path = options.get('dict', JIEBA_DEFAULT_DICT)
236-
if dict_path and Path(dict_path).is_file():
237-
jieba.load_userdict(dict_path)
248+
dict_path = options.get('dict', JIEBA_DEFAULT_DICT)
249+
if dict_path and Path(dict_path).is_file():
250+
jieba_load_userdict(str(dict_path))
238251

239252
self.stemmer = snowballstemmer.stemmer('english')
240253

241254
def split(self, input: str) -> list[str]:
242-
if JIEBA:
243-
chinese: list[str] = list(jieba.cut_for_search(input))
244-
else:
245-
chinese = []
255+
chinese: list[str] = list(cut_for_search(input))
246256

247257
latin1 = [term.strip() for term in self.latin1_letters.findall(input)]
248258
self.latin_terms.update(latin1)

0 commit comments

Comments
 (0)