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

stdlib/xml: fix return types for toxml/toprettyxml methods #10061

Merged
merged 8 commits into from
Apr 22, 2023

Conversation

wesleywright
Copy link
Contributor

Fixes #10060.

This commit adds overloads for xml.dom.minidom.Node.toxml and xml.dom.minidom.Node.toprettyxml to return str when encoding=None and bytes otherwise. I also added return types when evaluating in Python < 3.9, which the stubs previously lacked; AFAICT the same return types should be valid there.

@github-actions

This comment has been minimized.

@wesleywright
Copy link
Contributor Author

After fixing the dangling implementation lines, I realized that my attempt at the stubs was too naïve due to how encoding can be passed to toprettyxml() as either a kwarg or a positional. I updated the stubs to account for that.

The "correct" way to stub this wasn't immediately obvious to me, so I wrote a small test case (test_cases/check_xml.py) to validate my stubs. I'm happy to delete those from the PR if the stubs are deemed to simple to be worth adding to test cases here.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks! This looks good to me. We hit this issue quite often in typeshed (where we need three overloads even though it feels like we should be able to get away with two), but the test cases make it easier to verify that type checkers are making the correct inference here.

Just one question about some of the test cases -- looks to me like there might be some minor duplication right now?

Comment on lines 23 to 28
assert_type(document.toxml(), str)
assert_type(document.toxml(encoding=None), str)
assert_type(document.toxml(encoding="UTF8"), bytes)
if sys.version_info >= (3, 9):
assert_type(document.toxml(standalone=True), str)
assert_type(document.toxml(encoding="UTF8", standalone=True), bytes)
Copy link
Member

Choose a reason for hiding this comment

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

Aren't these cases already covered by the cases on lines 9-16?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are all meant to test toprettyxml; apparently I goofed that up at some point (probably when adding the version checks for 3.9). Will swap them to the correct method.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

thanks!

@AlexWaygood AlexWaygood merged commit cedf3b5 into python:main Apr 22, 2023
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.

Incorrect return types for xml.dom.minidom.Node.{toxml,toprettyxml}
4 participants