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

expose re._parse.parse_template publicly #105636

Open
asottile opened this issue Jun 10, 2023 · 6 comments
Open

expose re._parse.parse_template publicly #105636

asottile opened this issue Jun 10, 2023 · 6 comments
Labels
topic-regex type-feature A feature request or enhancement

Comments

@asottile
Copy link
Contributor

asottile commented Jun 10, 2023

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

@asottile asottile added the type-feature A feature request or enhancement label Jun 10, 2023
@sobolevn
Copy link
Member

Refs #105456

@AlexWaygood
Copy link
Member

Cc. @serhiy-storchaka

@raghunandanbhat
Copy link
Contributor

I started working on this and added parse_template() in re.__init__.py

def parse_template(source, pattern):
    return _parse.parse_template(source, pattern)

Considering the very limited amount of documentation about this function, I would appreciate some help in testing it thoroughly and writing detailed documentation.

@serhiy-storchaka
Copy link
Member

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
Copy link
Contributor Author

personally I only care about validation

@raghunandanbhat
Copy link
Contributor

Since the parse_template() raises an error when replacement template is invalid, we could just use parse_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-regex type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants