Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-46307: Add string.Template.get_identifiers() method#30493
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
319c8d7
bpo-46307: Add string.Template.get_identifiers() method
benkehoe b14f8bc
Update Misc/NEWS.d/next/Library/2022-01-10-07-51-43.bpo-46307.SKvOIY.rst
benkehoe e80bb26
bpo-46307: Add string.Template.is_valid method
benkehoe f40258e
bpo-46307: Update documentation
benkehoe 2c7e74d
bpo-46307: Update documentation
benkehoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -475,6 +475,57 @@ class PieDelims(Template): | ||
| self.assertEqual(s.substitute(dict(who='tim', what='ham')), | ||
| 'tim likes to eat a bag of ham worth $100') | ||
| def test_is_valid(self): | ||
| eq = self.assertEqual | ||
| s = Template('$who likes to eat a bag of ${what} worth $$100') | ||
| self.assertTrue(s.is_valid()) | ||
| s = Template('$who likes to eat a bag of ${what} worth $100') | ||
| self.assertFalse(s.is_valid()) | ||
| # if the pattern has an unrecognized capture group, | ||
| # it should raise ValueError like substitute and safe_substitute do | ||
| class BadPattern(Template): | ||
| pattern = r""" | ||
| (?P<badname>.*) | | ||
| (?P<escaped>@{2}) | | ||
| @(?P<named>[_a-z][._a-z0-9]*) | | ||
| @{(?P<braced>[_a-z][._a-z0-9]*)} | | ||
| (?P<invalid>@) | | ||
| """ | ||
| s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what') | ||
| self.assertRaises(ValueError, s.is_valid) | ||
warsaw marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_get_identifiers(self): | ||
| eq = self.assertEqual | ||
| raises = self.assertRaises | ||
| s = Template('$who likes to eat a bag of ${what} worth $$100') | ||
| ids = s.get_identifiers() | ||
| eq(ids, ['who', 'what']) | ||
| # repeated identifiers only included once | ||
| s = Template('$who likes to eat a bag of ${what} worth $$100; ${who} likes to eat a bag of $what worth $$100') | ||
| ids = s.get_identifiers() | ||
| eq(ids, ['who', 'what']) | ||
| # invalid identifiers are ignored | ||
| s = Template('$who likes to eat a bag of ${what} worth $100') | ||
| ids = s.get_identifiers() | ||
| eq(ids, ['who', 'what']) | ||
| # if the pattern has an unrecognized capture group, | ||
| # it should raise ValueError like substitute and safe_substitute do | ||
| class BadPattern(Template): | ||
| pattern = r""" | ||
| (?P<badname>.*) | | ||
| (?P<escaped>@{2}) | | ||
| @(?P<named>[_a-z][._a-z0-9]*) | | ||
| @{(?P<braced>[_a-z][._a-z0-9]*)} | | ||
| (?P<invalid>@) | | ||
| """ | ||
| s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what') | ||
| self.assertRaises(ValueError, s.get_identifiers) | ||
warsaw marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| if __name__ == '__main__': | ||
| unittest.main() | ||
1 change: 1 addition & 0 deletions
1 Misc/NEWS.d/next/Library/2022-01-10-07-51-43.bpo-46307.SKvOIY.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add :meth:`string.Template.is_valid` and :meth:`string.Template.get_identifiers` methods. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.