Skip to content

gh-139489: Add xml.utils.is_valid_name()#139768

Open
serhiy-storchaka wants to merge 8 commits intopython:mainfrom
serhiy-storchaka:xml-sax-utils-is_valid_name
Open

gh-139489: Add xml.utils.is_valid_name()#139768
serhiy-storchaka wants to merge 8 commits intopython:mainfrom
serhiy-storchaka:xml-sax-utils-is_valid_name

Conversation

@serhiy-storchaka
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka commented Oct 8, 2025

It allows to check whether a string can be used as an element or attribute name in XML.


📚 Documentation preview 📚: https://cpython-previews--139768.org.readthedocs.build/

It allows to check whether a string can be used as an element or attribute
name in XML.
Comment thread Doc/whatsnew/3.15.rst Outdated
@serhiy-storchaka serhiy-storchaka changed the title gh-139489: Add xml.sax.utils.is_valid_name() gh-139489: Add xml.sax.saxutils.is_valid_name() Oct 8, 2025
@vstinner
Copy link
Copy Markdown
Member

vstinner commented Oct 8, 2025

Why not adding the function in the xml package level, for example in a xml.utils module?

The function doesn't seem to be specific to the sax implementation. Is it?

@serhiy-storchaka
Copy link
Copy Markdown
Member Author

There is no an xml.utils module.

@vstinner
Copy link
Copy Markdown
Member

vstinner commented Oct 8, 2025

There is no an xml.utils module.

I'm suggesting to create such xml.utils module. Ok maybe put the new function directly in the xml top-level package?

@serhiy-storchaka
Copy link
Copy Markdown
Member Author

This is an interesting idea.

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 30, 2026
@vstinner
Copy link
Copy Markdown
Member

See also Place for XML helper functions discussion.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Apr 30, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32509194 | 📁 Comparing 7e96492 against main (b413bc7)

  🔍 Preview build  

33 files changed · ± 33 modified

± Modified

@serhiy-storchaka serhiy-storchaka removed the stale Stale PR or inactive for long period of time. label May 1, 2026
Comment thread Doc/whatsnew/3.15.rst Outdated
Comment thread Lib/xml/utils.py Outdated
Co-authored-by: Victor Stinner <vstinner@python.org>
@picnixz picnixz changed the title gh-139489: Add xml.sax.saxutils.is_valid_name() gh-139489: Add xml.utils.is_valid_name() May 2, 2026
Comment thread Doc/library/xml.rst
``False`` otherwise.

Almost all characters are permitted in names, except control characters and
those which either are or reasonably could be used as delimiters.
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.

Maybe add a link to the specs if any?

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.

Link found in the implementation: https://www.w3.org/TR/xml/#NT-Name.

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.

I think it would be good to add it in the online docs as well. It'll be more accessible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I am going to add several more functions. Some functions will need several links, and some links will be repeated for different functions. If we want to add links, it is better to add root links to the XML specifications at the top of the module documentation.

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.

That's also ok. As long as the online docs contain some links to some specifications, it's ok.

Comment thread Lib/xml/utils.py
@@ -0,0 +1,25 @@
lazy import re
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.

Are we worried about the stability of xml.utils? Maybe we could have xml._utils first and then promote it to a public module later?

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.

I'm also in favor of making this sub-module private (rename to xml._utils). There is a risk that developers start depending on xml.utils.is_valid_name name and it would break projects if the function is moved later (ex: to xml/__init__.py).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

xml and xml.utils were two initial suggestions, and they got the same amount of voices. If there is a chance of xml.utils been documented officially, it is better to keep this name from beginning. It is also more pickle-friendly.

Comment thread Lib/xml/utils.py
@@ -0,0 +1,25 @@
lazy import re


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.

I'd suggest adding an __all__

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.

It's needed. Currently, the re module is exposed as a xml attribute:

$ ./python
>>> import xml
>>> xml.re
<module 're' from '/home/vstinner/python/main/Lib/re/__init__.py'>

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants