Skip to content

refactor(pptx): use python-pptx shape types instead of getattr/hasattr - #4712

Merged
PeterStaar-IBM merged 1 commit into
docling-project:mainfrom
DanielNg0729:refactor/pptx-attribute-probing
Oct 9, 2026
Merged

PeterStaar-IBM merged 1 commit into
docling-project:mainfrom
DanielNg0729:refactor/pptx-attribute-probing

Conversation

@DanielNg0729

Copy link
Copy Markdown
Member

Summary

  • _get_shape_position read the position with getattr(shape, attr). It now types shape as BaseShape and attr as Literal["top", "left"], and reads shape.top or shape.left directly. _iter_shapes_by_position now uses BaseShape instead of object.
  • The AttributeError catch in _get_shape_position stays, with a comment. python-pptx wraps an element without a registered class, such as a p:contentPart inside a group, in a plain BaseShape. Its top and left getters then raise AttributeError.
  • handle_shapes checked hasattr(shape, "text"). In python-pptx, only autoshape.Shape and its placeholder subclasses define text, so the check is now isinstance(shape, Shape).

The behaviour does not change, so I did not add a test.

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

_get_shape_position read top/left with getattr(shape, attr). It now
types the shape as BaseShape and reads shape.top or shape.left
directly. The AttributeError catch stays, with a comment: python-pptx
wraps an element without a registered class, such as a p:contentPart
inside a group, in a plain BaseShape whose getters raise it.

The text check used hasattr(shape, "text"). Only autoshape.Shape and
its placeholder subclasses define text, so it is now an isinstance
check.

No output change.

Signed-off-by: Daniel Nguyen <danielnguyenh07@gmail.com>
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @DanielNg0729, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) pptx issue related to pptx backend labels Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit a53b2066c1. The analysis read the diff only. It did not run code from this pull request.

This PR refactors the PPTX backend to replace getattr/hasattr attribute probing with explicit typed access: _get_shape_position now takes BaseShape and a Literal["top","left"] parameter, and the hasattr(shape, "text") guard becomes isinstance(shape, Shape). No behaviour change is intended.

Topic labels added: pptx.

CI safety: no concern found. A maintainer must still approve CI.

1 related pull request(s) in the same code
PR State Signal Assessment
#4443 open changes the same code related: PR 4443 adds run-level hyperlink and character formatting to the same file and overlaps 3 hunks, but it is a feature addition, not the same refactor. The two PRs cannot both merge without rebasing.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI first review (advisory): no blocking issue found

This review covers the full diff at a53b2066c1. It read the code only. It did not run tests or code from this pull request.

The PR replaces getattr(shape, attr) in _get_shape_position with direct shape.top / shape.left access. It types the shape helpers with BaseShape and Literal["top", "left"], and it replaces hasattr(shape, "text") in handle_shapes with isinstance(shape, Shape). I found no wrong behavior. from __future__ import annotations keeps BaseShape out of runtime evaluation, so the guarded pptx import stays safe. Shape is used at runtime only after the constructor confirms that pptx is available. Limits: I did not run the tests or ty. I could not confirm from the workspace that text exists only on autoshape.Shape and its placeholder subclasses, because the python-pptx source is not installed here. The main remaining risk is a merge conflict with open PR #4443, which changes the same hunks.

A maintainer makes the merge decision. This review never approves the pull request.

Comment thread docling/backend/mspowerpoint_backend.py
@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Oct 9, 2026

@PeterStaar-IBM PeterStaar-IBM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm!

@PeterStaar-IBM
PeterStaar-IBM merged commit 8f2aca0 into docling-project:main Oct 9, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) pptx issue related to pptx backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants