Visitar URL original
gh-93607: document `root` attribute of `iterparse` by Prometheus3375 · Pull Request #99410 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20221223105609/https://github.com/python/cpython/pull/99410
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-93607: document root attribute of iterparse #99410

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Prometheus3375
Copy link

@Prometheus3375 Prometheus3375 commented Nov 12, 2022 •

@Prometheus3375 Prometheus3375 force-pushed the Prometheus3375-xml-iterparse-docs branch from ff6423a to 7a568ff Compare Nov 22, 2022
Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
@Prometheus3375 Prometheus3375 requested a review from slateny Nov 28, 2022
Copy link
Contributor

@slateny slateny left a comment

I'm no expert on this, but looks right and testing shows the same:

>>> f = io.StringIO('<data><to></to></data>')
>>> iter1 = iterparse(f)
>>> iter1.root
>>> [_ for _ in iter1]
[('end', <Element 'to' at 0x000001EF004B75B0>), ('end', <Element 'data' at 0x000001EF004B7560>)]
>>> iter1.root
<Element 'data' at 0x000001EF004B7560>

@Prometheus3375
Copy link
Author

Prometheus3375 commented Nov 29, 2022

It can be note that root attribute is always present, it is just None until the entire source is processed. In other words, iterparse_object.root is not None is a correct way to check that file is fully read while hasattr(iterparse_object, 'root') is not (if someone uses it for such checks). Thus, I am suggesting to clarify this point and also move the whole note about root a bit higher, when return object is discussed.

   Returns an :term:`iterator` providing ``(event, elem)`` pairs;
   it is also populated with a ``root`` attribute being set to ``None``.
   Once *source* is fully read, this attribute references the root element of the 
   resulting XML tree.

or

   Returns an :term:`iterator` with a ``root`` attribute providing ``(event, elem)`` pairs.
   ``root`` attribute is set to ``None``, but once *source* is fully read, 
   it references the root element of the resulting XML tree.

Both variants have weird wording in my opinion, but I couldn't think of a more acceptable one; suggestions are welcome. Maybe such clarification is not necessary at all.

@slateny
Copy link
Contributor

slateny commented Nov 30, 2022

Perhaps

   Returns an :term:`iterator` providing ``(event, elem)`` pairs;
   it has a ``root`` attribute that references the root element of the 
   resulting XML tree once *source* is fully read.

I think it may be fine to omit the mention of None and make it implicit here, but I wonder if it could also be misleading.

@Prometheus3375
Copy link
Author

Prometheus3375 commented Nov 30, 2022

I think your variant is acceptable.

Committed variant

Once source is fully read, <...> object is populated with a root attribute

states (imho) that attribute is added only at the end of parsing. This not correct.

Variant

it has a root attribute that references the root element of the resulting XML tree once source is fully read

does not hold information when attribute is added, but still notes when this attribute has a valid reference. Therefore, it is fine.

@Prometheus3375 Prometheus3375 requested a review from slateny Dec 2, 2022
slateny
slateny approved these changes Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants