Skip to content

Exception handling not working. #6091

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

Closed
sp1rs opened this issue Dec 20, 2018 · 3 comments
Closed

Exception handling not working. #6091

sp1rs opened this issue Dec 20, 2018 · 3 comments

Comments

@sp1rs
Copy link
Contributor

sp1rs commented Dec 20, 2018

from typing import Union

def post(path: str) -> Union[requests.models.Response, ManualException]
      try:
            return requests.post(path)
      except:  
            raise ManualException 


def call() -> List:
        try:
              resp = post('path')
        except ManualException:
               return []

        return resp.content

On running mypy

Item "ManualException" of "Union[Response, ManualException]" has no attribute "content"

Ideally mypy should not raise any error, because I am handling the case when exception is raised.

@JelleZijlstra
Copy link
Member

Exceptions should not be included in the return type. Your post function should simply return requests.models.Response.

The Python type system doesn't provide a way to specify what exceptions may be raised from a function.

@sp1rs
Copy link
Contributor Author

sp1rs commented Dec 20, 2018

@JelleZijlstra There are lots of places where we raise exception. This is common practice. Any work around? In django codebase, raising exception is a common thing.

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 20, 2018

Mypy can't check that exceptions are correctly caught or annotated, so your best is to document them in a docstring or invent some custom way of annotating them (but that won't be checked by mypy).

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