Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unreachable code after suppress() #9199

Closed
Dreamsorcerer opened this issue Jul 24, 2020 · 5 comments
Closed

unreachable code after suppress() #9199

Dreamsorcerer opened this issue Jul 24, 2020 · 5 comments

Comments

@Dreamsorcerer
Copy link
Contributor

Mypy reports this as unreachable code on the last line:

with suppress(KeyError):
    return _get_session()[name]

weights = AB_TESTS.get(name)

If the indexing causes a KeyError, this code will be reached. _get_session() is Any, but I also tried casting it to a Dict. So, it looks like Mypy assumes this will always succeed, despite us explicitly catching the error.

@ilevkivskyi
Copy link
Member

IIRC there is a way to tell mypy that a context manager can swallow an exception by annotating its __exit__() method in a special way, so this may be fixed by updating suppress.__exit__() annotation in typeshed. I however don't remember details on this.

@Dreamsorcerer
Copy link
Contributor Author

It appears that's already been done (by return bool): https://github.com/python/typeshed/pull/3179/files

I think the problem is that Mypy fails to recognise that if an exception is thrown then the return won't be executed, and therefore there are 2 branches of code here.

@Dreamsorcerer
Copy link
Contributor Author

Actually, it looks like the exact same issue that introduced this: #7214
So, unsure why this isn't working.

@gvanrossum
Copy link
Member

Perhaps you have some configuration flag that affects this? And (just to be sure) what mypy version are you using? I can't repro your issue, but it looks like you didn't show all the code of a self-contained example.

@Dreamsorcerer
Copy link
Contributor Author

Hmm, running it again, the error has now disappeared...
I'll close this unless I see it return again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants