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

Pylance shows a tuple of excepcions arent valid exception #1216

Closed
Durgeoble opened this issue Apr 30, 2021 · 10 comments
Closed

Pylance shows a tuple of excepcions arent valid exception #1216

Durgeoble opened this issue Apr 30, 2021 · 10 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@Durgeoble
Copy link

detected in the import from ftplib module all_errors.
Original code from ftplib.
# All exceptions (hopefully) that may be raised here and that aren't
# (always) programming errors on our side
all_errors = (Error, OSError, EOFError)

image

@erictraut
Copy link
Contributor

Can you please provide a self-contained, minimal piece of code that reproduces the behavior? I'm not able to repro the problem from the screen shot above. Here's what I tried, and it seems to work fine:

all_errors = (OSError, EOFError)

def func():
    try:
        ...
    except all_errors as e:
        ...

I didn't include Error in the tuple because that isn't a standard built-in type, and I don't know how you have defined it.

@Durgeoble
Copy link
Author

Durgeoble commented Apr 30, 2021

sure
language python
file ftplib # is a library but i put the relevant code

# Exception raised when an error or invalid response is received
class Error(Exception): pass
class error_reply(Error): pass          # unexpected [123]xx reply
class error_temp(Error): pass           # 4xx errors
class error_perm(Error): pass           # 5xx errors
class error_proto(Error): pass          # response does not begin with [1-5]
# All exceptions (hopefully) that may be raised here and that aren't
# (always) programming errors on our side
all_errors = (Error, OSError, EOFError)

file app.py

import ftplib  # the library from comes the code above
def dummyfuntion():
    try:
        print('do something')
    except ftplib.all_errors as e:
        raise

@jakebailey
Copy link
Member

ftplib is a standard library module, and the tuple in question is defined here: https://github.com/python/typeshed/blob/b0c6598fbaccf92f15a8bc9c47934fd6276eaf78/stdlib/ftplib.pyi#L25

@erictraut
Copy link
Contributor

OK, thanks for the additional details.

This appears to be a bug in the ftplib.pyi type stub. It defines all_errors as follows:

all_errors = Tuple[Type[Exception], ...]

In other words, it's defining all_errors as a type alias.

Based on the documentation, all_errors is not supposed to be a type alias; it's supposed to be a variable with the type Tuple[Type[Exception, ...]]. It should therefore be defined this way:

all_errors: Tuple[Type[Exception], ...]

I can submit a PR to the typeshed repo with this fix.

@jakebailey
Copy link
Member

Wow, I completely missed that when I was looking at the stub.

@Durgeoble
Copy link
Author

wow, so its a bug in the python library itself

@erictraut
Copy link
Contributor

Here's the typeshed PR: python/typeshed#5271

@erictraut
Copy link
Contributor

@Durgeoble, it's not a bug in the python library, but it is a bug in the typeshed stubs that describe the types used in the python library.

@erictraut
Copy link
Contributor

Thanks to very quick turnaround from the maintainers of typeshed, the PR was already accepted and merged. I've pulled the latest typeshed stubs into the pyright repo, so the fix will be incorporated into an upcoming release of pylance.

Thanks again for the bug report.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Apr 30, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.5.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202150-5-may-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants