Repository navigation
rework public Languages API fixes #4494 - #4496
Conversation
Sorry, sitting in the train in the way to RoboConDE
|
How can I rerun the checks or/and download the artifacts of the failed runs? |
|
ok, I got it. |
pekkaklarck
left a comment
There was a problem hiding this comment.
Looks very good. Apart from smallish style issues, the only thing I think that should be changed is the return value of import_language_module. Or do you have a strong reason for returning a dict of classes instead of a list of instances like earlier?
|
Changes look good now (apart from unused |
|
|
|
I got that but why? It makes our code more complicated. Does it help in some external usage? |
|
Yes, it helps, with that it is possible to use the same mechanism to select the possible language class that needs to be instanciated as if we use langs = Languages.get_available_languages()
for m in lang_modules:
langs.update(Languages.import_language_module(import_language_module) |
|
The point of The point of It would make sense for these two methods to consistently return either classes or instances, though. I don't have strong preference either way. If they'd return classes, then we should enhance |
|
Is there a reason you need to use langs = Languages.get_available_languages()
for m in lang_modules:
langs.update(Languages.import_language_module(m)instead of for m in lang_modules:
Languages.import_language_module(m)
langs = Languages.get_available_languages()? |
|
Yes it is, because I want to be as close as possible to the robotframework implementation and the language argument is either a language name or a language code or module name. So I have to look in available languages if there is name or code otherwise I have to import the language and look there, if I found something, I can add the result of my available languages instead of calling get_available_languages again. |
|
So your use case is something like this: available = Languages.get_available_languages()
for lang in languages:
if lang not in available:
available.update(Languages.import_language_module(lang))How's that better than the following? available = Languages(languages).get_available_languages() |
|
It should also be decided should This makes me think should this method really be public or not. Perhaps it would be better if external users just used |
|
The more I think about this, the less I see real needs to expose I'm not sure is exposing Controlling is English loaded or not is definitely a useful enhancement. |
|
Ok, example: The user defines these Here the code (it's copied from robotcode, but simplified to be runnable) from typing import List
from robot.conf.languages import Languages
from robot.utils.normalizing import normalize
from robot.errors import DataError
languages = ["fi", "pt-BR", "wr-ONG", "de", "CustomLang", "Chinese Traditional"]
available_languages = Languages.get_available_languages()
diagnostics: List[str] = []
found_languages: List[str] = []
for lang in languages:
normalized_lang = normalize(lang, "-")
language = available_languages.get(normalized_lang, None)
if language is None:
try:
available_languages.update(Languages.import_language_module(lang))
language = available_languages.get(normalized_lang, None)
except DataError:
pass
if language is None:
diagnostics.append(f"{lang} is not a valid language definition or language module.")
else:
found_languages.append(lang)
print(diagnostics)
print(found_languages)If I only have: available = Languages(languages).get_available_languages()I get an exceptions after the first wrong language, And I dont know where in the list of languages is the error. |
|
And why I must create an instance of the |
|
Thanks for the example, it's very useful. Based on it seems that just enhancing from typing import List
from robot.api import Languages
languages = ["fi", "pt-BR", "wr-ONG", "de", "CustomLang", "Chinese Traditional"]
available = Languages()
diagnostics: List[str] = []
for lang in languages:
try:
available.add_language(lang)
except ValueError as err:
diagnostics.append(f"{lang} is not a valid language definition or language module.")
print(diagnostics)
print(list(available))The above code already works otherwise, but custom language modules aren't supported. Enhancing |
|
You don't need to create an instance of |
|
Are there some other use case where making English optional and enhancing def add_language(self, name):
"""Add new language.
:param name: Name or code of a language to add, or name or path of
a language file to load.
:raises: :class:`~robot.errors.DataError` if language not found.
Language codes and names are passed to by :meth:`Language.from_name`.
Language files are imported as modules and :class:`Language` subclasses
in them are loaded.
"""
try:
languages = [Language.from_name(name)]
except ValueError as err1:
try:
languages = self._import_languages(name)
except DataError as err2:
raise DataError(f'{err1} {err2}')
for lang in languages:
self._add_language(lang)If you agree on this approach, we can decide who finishes this work. You've already spent lot of time with the PR and it contains good docs and tests and it would be great if you could finalize it. If you don't have time or interest, I can do that as well. It's clear we won't get rc 2 out today, but I hope we'd get it out tomorrow. |
|
Ok, see my comment in the issue ;-) Even if I think (please do not be angry) we could do better ;-) Can I finalize the PR, but only tomorrow evening. If too late for you, ... let's talk tommorow. |
|
If enhancing If you have time, it would be awesome if you could look at #4497. I got an implementation for that ready and can commit if it is considered good enough. |
|
ok, implemented your add_language proposal, but move get_available_languages and import_language_module to the global namespace, so that these functions are not public api, but because in robotcode I use already the not public api it's my turn if something changed. I missed 1 unittest will fix this later when I'm back from the doctor.... |
|
No, I don't want these methods to be in the module scope as public functions either. That makes no sense from Robot's point of view and I don't want to make them public as a convenience for external tools because it adds maintenance burden for us. I've told that multiple times. My position is not going to change unless you have a concrete example where these methods are needed and don't only avoid you writing a line or two more code on RobotCode side. This issue has now delayed RF 6.0 so long that I really need to get it closed. Are you willing to revert your changes related to making these two methods public? If yes, and you can do that pretty soon today, I can still wait for that. Otherwise I can do the following API changes myself:
|
|
After latest changes this looks very good! There are few more things that I think can be enhanced listed below but I can take care of them:
|
|
You were too fast ;-) I just optimized a little bit and merged duplicate codes. And the add_language also allows to add a language instance. Maybe we can merge that too. Do I have to open a new PR for this? |
|
Parser "needs" a ValueError thats why I leave it so. CommandLine Option needs a DataError to get the correct error text. |
- More doc strings. - Change exception used by `Languages.add_language` on error to match exception used when initializing `Languages`. - Make `Language.code/name` propertys accessible also from class. This required implementing new `@classproperty` decorator. This is part of issue #4494 and the work was started in #4496.
|
I don't see the final changes you mentioned anywhere. Were there other new features than |
|
See here d-biehl@fa63db3 Because the PR was closed, it is not shown there. I did not have the time this evening to make a new PR, but maybe you see what I did. |
|
I added support for using |
Example of a better public
LanguagesAPI