Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cpca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ def adcode_name(part_adcode: str):
return None if addr is None else addr.name


def full_adcode_name(part_adcode: str, sep: str = "") -> str:
res = {}
update_res_by_adcode(res, part_adcode)
parts = [res.get(_PROVINCE), res.get(_CITY), res.get(_COUNTY)]
return sep.join(p for p in parts if p)
Comment on lines +238 to +242
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

full_adcode_name propagates None crash and lacks a docstring

full_adcode_name(None) (or any falsy part_adcode) will propagate the same AttributeError to callers, since update_res_by_adcode has no None guard. The fix proposed above covers this as well.

Additionally, unlike the sibling adcode_name, this new public function has no docstring. Consider adding one to document the expected format of part_adcode (6-digit or 12-digit adcode) and the return value.

📝 Suggested docstring
 def full_adcode_name(part_adcode: str, sep: str = "") -> str:
+    """根据 adcode 返回省市区的完整层级名称。
+
+    Args:
+        part_adcode: 6 位或 12 位 adcode 字符串(如 "110105" 或 "110105000000")。
+        sep: 省市区名称之间的分隔符,默认为空字符串。
+
+    Returns:
+        由省、市、区名称按层级拼接的字符串;若 adcode 不存在则返回空字符串。
+    """
     res = {}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def full_adcode_name(part_adcode: str, sep: str = "") -> str:
res = {}
update_res_by_adcode(res, part_adcode)
parts = [res.get(_PROVINCE), res.get(_CITY), res.get(_COUNTY)]
return sep.join(p for p in parts if p)
def full_adcode_name(part_adcode: str, sep: str = "") -> str:
"""根据 adcode 返回省市区的完整层级名称
Args:
part_adcode: 6 位或 12 adcode 字符串 "110105" "110105000000")。
sep: 省市区名称之间的分隔符默认为空字符串
Returns:
由省区名称按层级拼接的字符串 adcode 不存在则返回空字符串
"""
res = {}
update_res_by_adcode(res, part_adcode)
parts = [res.get(_PROVINCE), res.get(_CITY), res.get(_COUNTY)]
return sep.join(p for p in parts if p)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cpca/__init__.py` around lines 238 - 242, full_adcode_name currently calls
update_res_by_adcode without guarding against a falsy part_adcode, so calling
full_adcode_name(None) will raise an AttributeError; add an explicit guard at
the start of full_adcode_name to handle falsy inputs (e.g., return "" or None
per module convention) and ensure update_res_by_adcode is only called with a
valid adcode string, and add a concise docstring to full_adcode_name (similar to
adcode_name) describing the expected part_adcode format (6- or 12-digit adcode),
the separator param, and the return value (combined province/city/county name or
empty string for invalid input).



def update_res_by_adcode(res: dict, adcode: str):
if adcode.endswith("0000"):
res[_PROVINCE] = adcode_name(adcode[:2])
Expand Down