You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support mypy analysis by adding NoReturn annotation to control flow methods #5361
Currently, the BuildIn keywords that always raise have no return annotation. Due to this, mypy cannot properly determine the flow of the code. Quick adaptation (from openapitools) as an example:
defexample() ->Anycached_parser=PARSER_CACHE.get(self._source, None)
ifcached_parser:
returncached_parsertry:
parser=ResolvingParser(self._source)
ifparser.specificationisNone: # pragma: no coverBuiltIn().fatal_error(
"Source was loaded, but no specification was present after parsing."
)
returnparserexceptResolutionErrorasexception:
BuiltIn().fatal_error(
f"ResolutionError while trying to load openapi spec: {exception}"
)
exceptValidationErrorasexception:
BuiltIn().fatal_error(
f"ValidationError while trying to load openapi spec: {exception}"
)
mypy flags this code as Missing return statement [return]. This is due to mypy not being aware that the BuildIn().fatal_error() calls in the except clauses will always raise.
Adding NoReturn annotations to fail(), fatal_error(), skip() and pass_execution() will allow mypy to properly infer the control flow in (library) code that uses these BuildIn methods.
First off all, I don't think it's a good idea to use BuiltIn.fatal_error() and other such methods in your own library. The reason is that we have FatalError and other such exceptions exposed via the robot.api.exceptions module for exact that purpose.
Due to the above reason, I don't think adding return type annotations just for the proposed usage is worth the effort. That said, adding argument and return value typing to all BuiltIn keywords would be great. That would add useful information to Libdoc outputs and it would also allow removing conversion code from various keywords. Are you interested to look at that?
Even better would be adding type hints to all standard libraries, but doing everything in one go isn't a good idea. It might be a good idea to start from some smaller and simpler library than BuiltIn, though.
Good point on the fatal_error usage, I'll update my side. I wrote that 3-4 years ago, not sure why I chose that approach (most likely me not knowing about FatalError).
I am interested in adding more annotations to Builtin keyboards / public API but under Python 3.9 it might be messy and limited in some areas (as we found with the annotations in api.deco previously). I'm in the middle of a big refactor of OpenApiTools and I decided that release will drop 3.9 already due to the need for typing features I have there.
Perhaps we should take a phased approach with this; add the annotations that can be done cleanly now and leave the bits that need 3.10+ annotation features for the release that drops 3.9 support.
Do you have a prioriry list of BuildIns / Keywords to be annotated?
We certainly don't need to add type hints to all standard libraries in one go, but I'd like to avoid partially annotated libraries. Because both Robot and type checkers support stringified annotations like 'int | float' and 'list[int]', I doubt there are much problems other that some libraries are rather big so there's quite a lot of work. Or do you have some particular issues in mind @robinmackaij?
If we decide that adding type hints to keywords is the way forward, I believe this issue and the associated PR can be closed and a new issue submitted about type hints. If the goal would be adding type hints to all standard libraries in RF 7.3, we could just have one issue listing all libraries. Each library could then get its own PR and the work could easily be shared with different community members. If you @robinmackaij would be interested to lead these efforts, it would be awesome. I can ask about that in the #devel channel if needed as well and would naturally review and merge PRs.
I decided to move to 3.10 as to not be restricted to stringified annotations, but since they support the | operator, I would expect we can cover almost everything; we shouldn't need complex generics etc. for library / keyword annotations.
On the openapitools repo I see the option to create sub-issues...not sure if that's available for all GitHub repos or an opt-in, but I guess that could work well for an issue for the annotations effort with a sub-issue for each library.
I can take the lead in this, for as far as that's needed; it seems like an effort that can be split up easily enough. A message in #devel would be good.
We most likely don't need anything more complicated than unions, but stringified annotations support also parameterized generics.
We also have sub issues here. It's a new addition and I haven't yet looked how it works, but it could be useful in this case. Alternatively we can have a simple task list.
I'll close this issue now. Can you @robinmackaij submit an issue about adding type hints to standard libraries? You could also send the initial message to #devel so we get started.
Activity
pekkaklarck commentedon Mar 11, 2025
First off all, I don't think it's a good idea to use
BuiltIn.fatal_error()and other such methods in your own library. The reason is that we haveFatalErrorand other such exceptions exposed via the robot.api.exceptions module for exact that purpose.Due to the above reason, I don't think adding return type annotations just for the proposed usage is worth the effort. That said, adding argument and return value typing to all BuiltIn keywords would be great. That would add useful information to Libdoc outputs and it would also allow removing conversion code from various keywords. Are you interested to look at that?
Even better would be adding type hints to all standard libraries, but doing everything in one go isn't a good idea. It might be a good idea to start from some smaller and simpler library than BuiltIn, though.
robinmackaij commentedon Mar 12, 2025
pekkaklarck commentedon Mar 12, 2025
We certainly don't need to add type hints to all standard libraries in one go, but I'd like to avoid partially annotated libraries. Because both Robot and type checkers support stringified annotations like
'int | float'and'list[int]', I doubt there are much problems other that some libraries are rather big so there's quite a lot of work. Or do you have some particular issues in mind @robinmackaij?If we decide that adding type hints to keywords is the way forward, I believe this issue and the associated PR can be closed and a new issue submitted about type hints. If the goal would be adding type hints to all standard libraries in RF 7.3, we could just have one issue listing all libraries. Each library could then get its own PR and the work could easily be shared with different community members. If you @robinmackaij would be interested to lead these efforts, it would be awesome. I can ask about that in the #devel channel if needed as well and would naturally review and merge PRs.
robinmackaij commentedon Mar 14, 2025
I decided to move to 3.10 as to not be restricted to stringified annotations, but since they support the
|operator, I would expect we can cover almost everything; we shouldn't need complex generics etc. for library / keyword annotations.On the openapitools repo I see the option to create sub-issues...not sure if that's available for all GitHub repos or an opt-in, but I guess that could work well for an issue for the annotations effort with a sub-issue for each library.
I can take the lead in this, for as far as that's needed; it seems like an effort that can be split up easily enough. A message in #devel would be good.
pekkaklarck commentedon Mar 14, 2025
We most likely don't need anything more complicated than unions, but stringified annotations support also parameterized generics.
We also have sub issues here. It's a new addition and I haven't yet looked how it works, but it could be useful in this case. Alternatively we can have a simple task list.
I'll close this issue now. Can you @robinmackaij submit an issue about adding type hints to standard libraries? You could also send the initial message to #devel so we get started.
pekkaklarck commentedon Jan 13, 2026
This was actually done as part of adding type hints to standard libraries (#5373).