Skip to content

support full adcode_name#133

Open
xiayouran wants to merge 1 commit intoDQinYuan:masterfrom
xiayouran:full_adcode_name
Open

support full adcode_name#133
xiayouran wants to merge 1 commit intoDQinYuan:masterfrom
xiayouran:full_adcode_name

Conversation

@xiayouran
Copy link

@xiayouran xiayouran commented Feb 25, 2026

支持根据adcode获取省市区/县全称

Summary by CodeRabbit

  • New Features
    • Added functionality to retrieve the complete location name (province, city, county) for administrative codes with customizable formatting options.

@coderabbitai
Copy link

coderabbitai bot commented Feb 25, 2026

📝 Walkthrough

Walkthrough

A new public function full_adcode_name() was added to resolve complete hierarchical names from administrative codes, and update_res_by_adcode() was enhanced with early returns and expanded logic to handle partial adcodes for province and city resolution.

Changes

Cohort / File(s) Summary
Address Code Resolution Enhancement
cpca/__init__.py
Added full_adcode_name() function to compute hierarchical names from adcodes. Enhanced update_res_by_adcode() with early returns for codes ending in "0000" and "00", enabling partial code resolution for province/city hierarchies.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop through codes so fine and true,
Province, city, county—what a view!
With early returns we bound with glee,
Hierarchies now flow so free!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'support full adcode_name' clearly and specifically describes the main feature addition: a new function to get the full hierarchical name for an adcode.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
cpca/__init__.py (1)

245-248: ⚠️ Potential issue | 🔴 Critical

update_res_by_adcode crashes when adcode is None

In _extract_addrs (line 208), update_res_by_adcode(res, adcode) is called inside the elif new_entry_when_not_belong: branch where adcode is still None if no address match has been found yet (initialized to None at line 185, only set inside if cur_addr:). adcode.endswith("0000") raises AttributeError: 'NoneType' object has no attribute 'endswith' on that path.

This is pre-existing, but since the function is actively being changed here, it's a good opportunity to add a guard:

🛡️ Proposed fix
 def update_res_by_adcode(res: dict, adcode: str):
+    if not adcode:
+        return
     if adcode.endswith("0000"):
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cpca/__init__.py` around lines 245 - 248, The function update_res_by_adcode
currently assumes adcode is a string and calls adcode.endswith("0000"), which
crashes when adcode is None; add an early guard in update_res_by_adcode to
return immediately if adcode is falsy or not a string (e.g., if not adcode or
not isinstance(adcode, str): return) so callers like _extract_addrs can call
update_res_by_adcode(res, adcode) safely when adcode is still None; keep the
existing logic for string adcodes (checking endswith and setting
res[_PROVINCE]).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cpca/__init__.py`:
- Around line 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).

---

Outside diff comments:
In `@cpca/__init__.py`:
- Around line 245-248: The function update_res_by_adcode currently assumes
adcode is a string and calls adcode.endswith("0000"), which crashes when adcode
is None; add an early guard in update_res_by_adcode to return immediately if
adcode is falsy or not a string (e.g., if not adcode or not isinstance(adcode,
str): return) so callers like _extract_addrs can call update_res_by_adcode(res,
adcode) safely when adcode is still None; keep the existing logic for string
adcodes (checking endswith and setting res[_PROVINCE]).

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9bdfd6e and 9b3e77d.

📒 Files selected for processing (1)
  • cpca/__init__.py

Comment on lines +238 to +242
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)
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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant