Skip to content

Refactor star-list handling, autoimport parsing and worder naming - #965

Open
Ketankhunti wants to merge 6 commits into
python-rope:masterfrom
Ketankhunti:refactor/assignment-cleanups
Open

Ketankhunti wants to merge 6 commits into
python-rope:masterfrom
Ketankhunti:refactor/assignment-cleanups

Conversation

@Ketankhunti

Copy link
Copy Markdown

Behavior-preserving cleanups in three areas, no functional change.

rope/base/worder.py

  • Rename Worder.dumb_finder to fallback_finder. The old name is
    vague; the field holds a plain finder over the original source used
    when the offset falls inside an ignored region.

rope/contrib/autoimport/parse.py

  • Replace the isinstance if-chain in get_type_ast with a dispatch
    table (_NODE_TYPE_MAP). Same semantics; a new node kind now needs
    one table entry instead of another branch.
  • Extract _names_from_importfrom from get_names_from_file. The
    relative from-import loop is now a standalone generator, testable
    in isolation.

rope/refactor/importutils/module_imports.py

  • Introduce explaining variables (is_str_constant, is_name_ref,
    is_str_assignment) for the repeated isinstance checks in
    _get_all_star_list.
  • Move the nested _resolve_name to module-level
    _resolve_assigned_names (it never used self); updates the three
    call sites.
  • Extract _StarListCollector to hold the pending-assignments stack
    and collected names; _get_all_star_list keeps the traversal logic
    while state lives in the new class.

Verification:

  • black and isort clean on all three files.
  • pytest ropetest/refactor/importutilstest.py — 152 passed.
  • pytest ropetest/contrib/autoimporttest.py — 28 passed.
  • pytest ropetest/codeanalyzetest.py — 180 passed.
  • Full suite pytest -q — 2177 passed, 8 skipped, 5 xfailed.

The old name is vague and unprofessional. The field holds a plain
finder over the original source used when the offset falls inside an
ignored region. Rename both the definition and the single use in
_context_call. No behavior change.
Replace the repeated isinstance if-chain with a small type map
iterated with isinstance. Same behavior, adding a new node kind now
means adding one table entry instead of another branch.
Move the relative from-import loop out of the main child-node loop
into its own generator. get_names_from_file keeps Assign and
def/class handling; import handling is now testable in isolation.
No behavior change.
Name the repeated isinstance checks as is_str_constant,
is_name_ref and is_str_assignment so the star-list branches read
as intent instead of type-test soup. No behavior change.
Move the nested _resolve_name out of ModuleImports._get_all_star_list
as _resolve_assigned_names. It never used self; nesting hid it from
reuse and tests. Update the three call sites. No behavior change.
Move the pending-assignments stack and collected names out of
_get_all_star_list into a small helper class with has_pending,
next_assignment, defer, defer_many and add. The method keeps the
traversal logic; state lives in the new class. No behavior change.

This branch has not been deployed

No deployments
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