Repository navigation
5373 type hints string - #5554
Conversation
…nd notes for discussion)
8ce5dcc to
4533d40
Compare
|
Rebased upstream/master |
…lso work with bytes
pekkaklarck
left a comment
There was a problem hiding this comment.
I only looked at the code changes and they mostly looks good.
Keywords that don't currently work properly with strings and bytes should be listed in #5548 that I'll reopen. I can then go through them myself after this PR has been merged. Their initial typing as part of this PR can be either str or str | bytes depending on the context.
| "[ARABIC]": "".join(chr(c) for c in range(0x0600, 0x0700)), | ||
| "[POLISH]": ascii_lowercase + ascii_uppercase + "ąćęłńóśźżĄĆĘŁŃÓŚŹŻ", | ||
| } | ||
| Error = Union[Literal["strict", "ignore", "replace"], str] |
There was a problem hiding this comment.
I'm not sure is it a good idea to list some of the possible error handlers separate. Using just str as the type would be simpler.
| @keyword(types=None) | ||
| def convert_to_title_case(self, string, exclude=None): | ||
| # TODO(@pekkaklarck): some paramters could be string, some bytes -> risk of incompatibilies | ||
| # TODO(@pekkaklarck): there are some tests indicating that it should only work with str/ not bytes (at least in the past) e.g. Should Be Title Case Does Not Work With ASCII Bytes |
There was a problem hiding this comment.
The current explicit check for bytes is due to the keyword otherwise anyway failing with them. For example, re.split(r"(\s+)", string) fails if string is actually bytes because the pattern is str. It could be fixed by using br"(\s+)" instead with bytes, but there are also other places that needed to be adjusted. If the signature is changed to string: str, then automatic argument conversion will convert bytes to str and the keyword will kind of work. It would return a str instead of bytes, though, which in my opinion would be wrong.
We have several options how to handle this:
- Just use
stras the type. - Use
str | bytesas the type, but explicitly reject bytes like nowadays. It would be a bit odd if the signature would say that bytes are fine but the keyword would fail with them, but at least the behavior would be explicit. - Enhance the keyword so that it actually works with bytes. Looking at the code, it shouldn't be overly complicated as
strandbytesAPIs are mostly the same. We already did this with various BuiltIn and Collections keywords (Enhanced and explicitbytessupport in BuiltIn, String and Collections #5548) so this would be a logic change also in this case.
Considering that options 1. and 2. have some drawbacks, I believe the option 3. is the way to go. This is what I propose we do in practice:
- Use typing
str | bytesas part of this PR. Preserve the currentTypeErrorwhen used withbytes. - Re-open Enhanced and explicit
bytessupport in BuiltIn, String and Collections #5548 with a note thatConvert To Title Caseneeds to be updated as well. If there are other similar keywords that don't actually work with bytes in String, they can be mentioned in that issue as well. - Merge this PR when it's otherwise ready.
- Enhance
Convert To Title Case, and possible other keywords, separately to get Enhanced and explicitbytessupport in BuiltIn, String and Collections #5548 closed again. I can take care of this.
There was a problem hiding this comment.
- Used typing str | bytes as part of this PR. Preserve the current TypeError
- left a note referencing this comment in 5548
| return bytes.decode(encoding, errors) | ||
|
|
||
| def format_string(self, template, /, *positional, **named): | ||
| # TODO(@pekkaklarck): some paramters could be string, some bytes -> risk of incompatibilies |
There was a problem hiding this comment.
This is similar to Convert To Title Case where I think enhancing the keyword so that it actually works with bytes would make sense. In this case that would be harder, though, because there's no bytes.format method. I guess we could to something like this to support bytes, though:
if isinstance(template, bytes):
template = template.decode('latin-1')
return_bytes = True
else:
return_bytes = False
# current implementation
return result if not return_bytes else result.decode('latin-1')I'm not sure is this really worth the effort, but the alternatives have the same problems as with Convert To Title Case. In this case I believe we should do this:
- Use just
stras the typing initially. - Mention this in Enhanced and explicit
bytessupport in BuiltIn, String and Collections #5548 and decide later should bytes support be added or not.
There was a problem hiding this comment.
- use just str
- left a note referencing this comment in 5548
| return count | ||
|
|
||
| def split_to_lines(self, string, start=0, end=None): | ||
| # does not work with bytes: ${{b'hello\nworld'}} -> Length of '["b'hello\\nworld'"]' should be 2 but is 1. |
There was a problem hiding this comment.
You seem to have str as the typing but it should be str | bytes.
There was a problem hiding this comment.
Yes I left a comment there (which I now removed in new commits):
Testing showed:
does not work with bytes (when using `str | bytes`): ${{b'hello\nworld'}} -> Length of '["b'hello\\nworld'"]' should be 2 but is 1.
This seems to be broken.
Is also mentioned in #5548.
There was a problem hiding this comment.
left a note referencing this comment in 5548
| self, | ||
| string: str, | ||
| start: "int | str | None" = 0, | ||
| end: "int | str | None" = None, |
There was a problem hiding this comment.
This is one of the keywords that are affected by #5558. Please use Literal[''] instead of str with start and end.
There was a problem hiding this comment.
Is this the correct link? #5558 is just a link to "Add annotations to String", also there you reference 5558?
Could you help me out? I can not see the explanation for Literal['']
There was a problem hiding this comment.
Update: I think I understand the problem and fixed it for now.
|
|
||
| def fetch_from_right(self, string, marker): | ||
| # TODO(@pekkaklarck): some paramters could be string, some bytes -> risk of incompatibilies | ||
| def fetch_from_right(self, string: str, marker: str) -> str: |
There was a problem hiding this comment.
done. (listed all)
|
|
||
| @keyword(types=None) | ||
| def strip_string(self, string, mode="both", characters=None): | ||
| # TODO(@pekkaklarck): some paramters could be string, some bytes -> risk of incompatibilies |
There was a problem hiding this comment.
done. (listed all)
|
|
||
| @keyword(types=None) | ||
| def should_be_title_case(self, string, msg=None, exclude=None): | ||
| # TODO(@pekkaklarck): some paramters could be string, some bytes -> risk of incompatibilies |
There was a problem hiding this comment.
done. (listed all)
| Error = Union[Literal["strict", "ignore", "replace"], str] | ||
| Flags = Union[ | ||
| Literal["IGNORECASE", "MULTILINE", "VERBOSE", "IGNORECASE | VERBOSE"], str, None | ||
| ] |
There was a problem hiding this comment.
I don't think listing these explicitly makes sense. We cannot anyway enumerate all possible combinations.
| ] | ||
| Markers = Union[ | ||
| Literal["[LOWER]", "[UPPER]", "[LETTERS]", "[NUMBERS]", "[ARABIC]", "[POLISH]"], str | ||
| ] |
There was a problem hiding this comment.
Similarly as above, I don't think listing these explicitly is a good idea. These markers can also be used like [NUMBERS]abcdef which doesn't match any of the literal values. Using str allows that, but I believe all this still makes the signature unnecessarily complicated.
|
At least some of the failures on CI are due to the use of parameterized types like |
done. |
…added Literal[''] for start/end index
|
@pekkaklarck I took time to answer your review notes and finished most of them.
How is it possible to see the errors? Update: FAIL: String.Convert To.Convert To Title Case with excludes I tried to reproduce this error and figured out that e.g. Tests are passed when using What should we do here? I remember , you hat the idea to use Iterable[str] |
|
I'd be surprised if |
I made a mistake, writing it down. Local Tests with python3.8 (focus on parameter fail with |
|
Do all tests pass for you locally with Python 3.8? I can try that myself as well. Have you pushed all changes? |
|
I tested this locally and can confirm that four tests fail with Python 3.8 and 3.9 when using typing
|
|
The above explains why tests fail. There are three ways we can make them pass:
I believe the last option is the best one. I don't remember what I have earlier said about using |
| # @{result} = Split To Lines ${BYTES TEXT IN COLUMNS} | ||
| # Length Should Be ${result} 2 | ||
| # Should be equal ${result}[0] ${BYTES FIRST LINE} | ||
| # Should be equal ${result}[1] ${BYTES SECOND LINE} |
There was a problem hiding this comment.
Splitting bytes to lines should work. Is the typing of the keyword correct?
|
When reviewing this PR, I noticed that our It could now be a good idea to add tests for |
Yes you can do the minor changes. I would at earliest have time tommorow evening. |
|
This is such a good shape that I'll fix the issues myself and merge this. I can then continue with other issues that are related to String. |
|
I changed |

No description provided.