Skip to content

Commit

Permalink
Fix Language.name if subclass doesn't have __doc__.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkaklarck committed Aug 16, 2022
1 parent a1c6fce commit 7e61ad3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/robot/conf/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _get_available_languages(self):
for lang in Language.__subclasses__():
available[normalize(lang.__name__)] = lang
if lang.__doc__:
available[normalize(lang.__doc__)] = lang
available[normalize(lang.__doc__.splitlines()[0])] = lang
return available

def _import_languages(self, lang):
Expand Down Expand Up @@ -141,10 +141,11 @@ def from_name(cls, name):
Raises `ValueError` if no matching langauge is found.
"""
normalized = normalize(name, ignore='-')
for subcls in cls.__subclasses__():
if normalized in (normalize(subcls.__name__),
normalize(getdoc(subcls))):
return subcls()
for lang in cls.__subclasses__():
if normalized == normalize(lang.__name__):
return lang()
if lang.__doc__ and normalized == normalize(lang.__doc__.splitlines()[0]):
return lang()
raise ValueError(f"No language with name '{name}' found.")

@property
Expand All @@ -166,7 +167,7 @@ def name(self):
Got from the first line of the class docstring.
"""
return getdoc(self).splitlines()[0]
return self.__doc__.splitlines()[0] if self.__doc__ else ''

@property
def settings(self):
Expand Down
6 changes: 6 additions & 0 deletions utest/api/test_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class X(Language):
"""
assert_equal(X().name, 'Language Name')

def test_name_without_docstring(self):
class X(Language):
pass
X.__doc__ = None
assert_equal(X().name, '')


class TestFromName(unittest.TestCase):

Expand Down

0 comments on commit 7e61ad3

Please sign in to comment.