I was working on a function to handle a 429 error from the NCBI API, specifically a recursive retry function. The code ran smoothly, but I ended up getting `None` values instead of the expected API data. The logs indicated that retries were happening successfully, which made it frustrating. Here's the simplified code snippet I used:
```python
def fetch_data_with_retry(retries=10):
try:
return api_client.get_data()
except RateLimitError:
if retries > 0:
print(f"Rate limit hit. Retrying... {retries} left")
time.sleep(1)
fetch_data_with_retry(retries - 1)
else:
print("Max retries exceeded.")
raise
```
I eventually figured out what was wrong, but if you were to review this code, would you catch the issue right away?
8 Answers
You're missing a return statement in the recursive call on line 9. It's an easy mistake to make, but it ensures that no value gets passed back up the call stack. Just keeping that in mind can prevent a lot of headaches!
Well, it seems like everyone is in agreement — not returning a value during recursion is a common pitfall. Utilizing tools like mypy can also help catch these types of mistakes early on.
I might not spot the problem immediately, but the tools you use can make a big difference. If you're using PyCharm or a similar IDE, it might help highlight any issues you might overlook.
To be honest, I caught it right away just by reading through the code. Using static type checking by annotating return types would definitely help catch such errors before they cause issues.
The problem probably wouldn’t be obvious during a code review, but once I saw you were getting `None`, it would be clear what the issue was. This definitely points to an important lesson about testing your code thoroughly before releasing it!
It seems like this discussion highlights a key point: having sufficient test coverage could catch these bugs right away. It’s all about taking that extra time in testing your code! If reviewed properly, things like the `except...` part might not get the attention they deserve.
Totally! The lack of a return statement in recursion is a trap many fall into. Just need to be more careful usually!
If I were reviewing this, I’d definitely suggest using a library like Tenacity instead of writing your own retry logic. It handles such cases more gracefully. Also, when reviewing, my thinking often depends on how much I trust the developer's expertise. That's why I think using type checkers for Python code has become essential.
All true! There's no need to reinvent the wheel when these libraries exist.

Exactly! It's good to recognize that just because it seems like it works, doesn't mean it actually does.