expose re._parse.parse_template publicly #105636
Copy link
Copy link
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-regextype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
I would like to access parse_template to be able to eagerly validate that a regex replacement template is valid.
I'm using this in my python-based text editor to do find and replace. Currently I am reaching into sre_parse to get parse_template (which used to be a public-named module but was deprecated in 3.11)
I suspect this could be as simple as exposing parse_template -- my code doesn't actually care about the return value, simply whether it raises an exception or not
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-regextype-featureA feature request or enhancementA feature request or enhancement
Activity
sobolevn commentedon Jun 11, 2023
Refs #105456
AlexWaygood commentedon Jun 11, 2023
Cc. @serhiy-storchaka
raghunandanbhat commentedon Jun 14, 2023
I started working on this and added
parse_template()inre.__init__.pyConsidering the very limited amount of documentation about this function, I would appreciate some help in testing it thoroughly and writing detailed documentation.
serhiy-storchaka commentedon Jun 14, 2023
Is regex replacement template validation only what you need? Or do you need more information from the result?
Over a year ago, I wrote an implementation to compile a regex replacement template. I expected that this could be used to speed up critical code, like
re.compile(), but I didn't see much difference compared to implicitly caching the compiled regex replacement templates. So I excluded it from #91524.asottile commentedon Jun 14, 2023
personally I only care about validation
raghunandanbhat commentedon Jul 1, 2023
Since the
parse_template()raises an error when replacement template is invalid, we could just useparse_template()as it is by exposing it publicly.I think it is redundant to implement a function just to check if a replacement template is valid, instead we could use the existing one as it is.
pythongh-105636: Add re.Pattern.compile_template()
pythongh-105636: Add re.Pattern.compile_template()
serhiy-storchaka commentedon Jun 26, 2025
#135992 it is almost the same as the version written 4 years ago. It adds the
compile_template()method for the pattern object which simply calls private functionre._compile_template(). The type of the result is exposed asre.Template. It has minimal interface:__call__and__class_getitem__.asottile commentedon Jun 26, 2025
ah nice! that would definitely solve my problem
asottile commentedon Jun 17, 2026
unfortunate this won't make it to 3.15 (since the sre_* modules are gone gone now) -- will have to resort to some hacks until this api gets included :(