Skip to content

Commit 7d2b54f

Browse files
authored
Fix bug in scratch language detection (#1697)
* Fix bug in scratch language detection * Fix ruff check * Rewrite the expression nicer
1 parent e53c99f commit 7d2b54f

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

backend/coreapp/serializers.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,32 +259,29 @@ class Meta:
259259
"platform",
260260
]
261261

262-
def get_language(self, scratch: Scratch) -> Optional[str]:
262+
def get_language(self, scratch: Scratch) -> str:
263263
"""
264264
Strategy for extracting a scratch's language:
265265
- If the scratch's compiler has a LanguageFlagSet in its flags, attempt to match a language flag against that
266266
- Otherwise, fallback to the compiler's default language
267267
"""
268268
compiler = compilers.from_id(scratch.compiler)
269269
language_flag_set = next(
270-
iter([i for i in compiler.flags if isinstance(i, LanguageFlagSet)]),
270+
(i for i in compiler.flags if isinstance(i, LanguageFlagSet)),
271271
None,
272272
)
273273

274274
if language_flag_set:
275-
language = next(
276-
iter(
277-
[
278-
language
279-
for (flag, language) in language_flag_set.flags.items()
280-
if flag in scratch.compiler_flags
281-
]
282-
),
283-
None,
284-
)
285-
286-
if language:
287-
return language.value
275+
matches = [
276+
(flag, language)
277+
for flag, language in language_flag_set.flags.items()
278+
if flag in scratch.compiler_flags
279+
]
280+
281+
if matches:
282+
# taking the longest avoids detecting C++ as C
283+
longest_match = max(matches, key=lambda m: len(m[0]))
284+
return longest_match[1].value
288285

289286
# If we're here, either the compiler doesn't have a LanguageFlagSet, or the scratch doesn't
290287
# have a flag within it.

0 commit comments

Comments
 (0)