bpo-32476 : Add concat function for ElementTree find#5251
bpo-32476 : Add concat function for ElementTree find#5251jjolly wants to merge 4 commits intopython:mainfrom
Conversation
|
maybe you could add a |
|
@matrixise How about full documentation on the new feature - including the versionadd? |
|
Fine, you have updated the documentation. Thank you |
| r"\.\.|" | ||
| r"\(\)|" | ||
| r"[/.*:\[\]\(\)@=])|" | ||
| r"[/.,*:\[\]\(\)@=])|" |
There was a problem hiding this comment.
Adding a comma here suggests that you should also add error tests for commas appearing in some unusual places.
There was a problem hiding this comment.
There does not seem to be a precedence for such testing. Is there a similar test for unusually placed '@' or '=' tokens? I could implement a test such as these:
self.assertRaisesRegex(KeyError, ',', e.find, ',/tag[0]')
self.assertRaisesRegex(KeyError, ',', e.find, '.,tag[0]')
self.assertRaisesRegex(KeyError, ',', e.find, './,tag[0]')
self.assertRaisesRegex(KeyError, ',', e.find, '.,/tag[0]')
If I added these tests, wouldn't it be necessary to add similar tests for the other characters in the regex? Do I test for every character in the regex? How far do I go?
There was a problem hiding this comment.
Most of the other characters are part of the general tree structure and thus allowed in more places. All of these places are unlikely to be prepared for handling a ,, so this is a new case. I'd specifically recommend to add tests for things like ,[0], tag[,], tag[(,)] and so on.
I would generally say that covering the parser with more tests would be helpful, but you don't have to do that for this ticket. Since you're at it, though, feel free to add a couple of similar ones for =, : and @. You can do that in a loop, as long as the error message on failures is clear about what incorrect expression was tried.
cb286b0 to
67c0d33
Compare
Lib/xml/etree/ElementPath.py
Outdated
Lib/xml/etree/ElementPath.py
Outdated
|
One more thing: there should also be tests that it's still possible to look for tags and attributes called |
67c0d33 to
8128317
Compare
8128317 to
3d714fa
Compare
I have added a new tag and attributes in order to test |
3d714fa to
75dff0a
Compare
|
...and the spelling changes. Sorry for the multiple pushes. |
75dff0a to
67a1c19
Compare
| try: | ||
| token = next() | ||
| except StopIteration: | ||
| raise SyntaxError("missing closing parathesis for concat") |
There was a problem hiding this comment.
| raise SyntaxError("missing closing parathesis for concat") | |
| raise SyntaxError("missing closing parenthesis for concat") |
| self.assertEqual(e.find('./tag[@class=concat("d")]').attrib['class'], 'd') | ||
| self.assertEqual(e.find('./tag[@class=concat("", \'d\', "")]').attrib['class'], 'd') | ||
| self.assertEqual(e.find('./tag[@value="concat"]').attrib['class'], 'b') | ||
| self.assertEqual(e.find('./tag[@value=concat("concat")]').attrib['class'], 'b') | ||
| self.assertEqual(e.find('./tag[@concat="value"]').attrib['class'], 'd') | ||
| self.assertEqual(e.find('./tag[@concat=concat("value")]').attrib['class'], 'd') | ||
| self.assertEqual(e.find('./concat[@class=concat("x")]').attrib['class'], 'x') |
There was a problem hiding this comment.
Ok, but … none of these tests represents a real use case for concat(), does it? What about concat("prefix-", @attr) or concat("'", ., "'") ? Those seem to be the really interesting cases to me.
I understand that your intention was to allow for escaping, but a) I can't see that being covered by the tests and b) does that really make a use case for concat() already?
My fear is that we're opening up a feature here which users will bump into and be disappointed because it doesn't actually do what they want. It really just serves the tiny use case of concatenating string constants right now. That's … unexpected at best.
|
Could you amend versionchanged, please? Lets try for 3.11. |
|
This PR is stale because it has been open for 30 days with no activity. |
|
Closing since the implementation isn't complete while the solution is not targeted to the specific problem. It seems that we'd add more expectations than solutions here. |
Problem: Using apostrophes and quotes in a search string is impossible. Currently escaping quotes in the search string is not implemented.
Example:
Solution: This patch implements the XPath
concat()function within an attribute search.Result:
https://bugs.python.org/issue32476