Bug example
Take the following code
top_genres = [{'name': 'Adventure',
'modified_mean_score': 8.857142857142858},
{'name': 'Crime',
'modified_mean_score': 8.733142857142858},
{'name': 'Fantasy',
'modified_mean_score': 8.608695652173912}]
movies = [
{'movie_id': 42511,
'title': 'Movie 1',
'genres': [{'id': 1, 'name': 'Action'},
{'id': 2, 'name': 'Adventure'},
{'id': 28, 'name': 'Drama'},
{'id': 38, 'name': 'Military'},
{'id': 24, 'name': 'Sci-Fi'}]},
{'movie_id': 42512,
'title': 'Movie 2',
'genres': [{'id': 3, 'name': 'Comedy'},
{'id': 4, 'name': 'Fantasy'},
{'id': 28, 'name': 'Crime'},
{'id': 38, 'name': 'War'},
{'id': 24, 'name': 'Science Fiction'}]},
{'movie_id': 42513,
'title': 'Movie 3',
'genres': [{'id': 5, 'name': 'Crime'},
{'id': 6, 'name': 'Horror'},
{'id': 28, 'name': 'Mystery'},
{'id': 38, 'name': 'Historical'},
{'id': 24, 'name': 'Fantasy'}]},
{'movie_id': 42514,
'title': 'Movie 4',
'genres': [{'id': 7, 'name': 'Documentary'},
{'id': 8, 'name': 'Biography'},
{'id': 28, 'name': 'Romance'},
{'id': 38, 'name': 'Crime'},
{'id': 24, 'name': 'Animation'}]}
]
for genre in top_genres:
movie_list = [movie for movie in movies if genre['name'] in [genre['name'] for genre in movie['genres']]]
print(f"{genre['name']}: {len(movie_list)}")
When I run it in a jupyter notebook I get the desired (correct) output:
Adventure: 1
Crime: 3
Fantasy: 2
However, when I run it in Anvil, I get this weird output:
Animation: 1
Animation: 0
Animation: 0
I believe that the genre variable in the inner list comprehension is escaping the context defined by the inner list comprehension.
Changing the variable name in the inner list comprehesion (for example to genre_b) solves the problem and produces the expected output.
for genre in top_genres:
movie_list = [movie for movie in movies if genre['name'] in [genre_b['name'] for genre_b in movie['genres']]]
print(f"{genre['name']}: {len(movie_list)}")
Output
Adventure: 1
Crime: 3
Fantasy: 2
Clone link:
Here is a minimal app that demonstrates the bug