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

Quick Fix Add "Optional" to Type Annotation doesn't work #1873

Closed
Molkree opened this issue Sep 24, 2021 · 7 comments
Closed

Quick Fix Add "Optional" to Type Annotation doesn't work #1873

Molkree opened this issue Sep 24, 2021 · 7 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

@Molkree
Copy link

Molkree commented Sep 24, 2021

Environment data

  • Language Server version: v2021.9.3
  • OS and version: Win10, 19043.1237
  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.7

Expected behaviour

Quick Fix wraps function parameter type in Optional.

Actual behaviour

This is with strict type checking. None is underlined with this error:

Expression of type "None" cannot be assigned to parameter of type "int"
  Type "None" cannot be assigned to type "int" Pylance(reportGeneralTypeIssues)

When trying to apply Quick Fix nothing happens.

Code Snippet / Additional information

def foo(arg: int = None):
    print(arg)
@jakebailey
Copy link
Member

I debugged this, and this one's just a bug where we seemingly have forgotten to include the name of the file to edit on the quick fix.

The fix is simple, but will take a little more than a line thanks to some plumbing to get a URI to the right place.

@yvvt0379
Copy link

But what about an argunent that starts life as None and stays not-None once assigned not-None object?

@erictraut
Copy link
Contributor

I'm not sure what you mean. Can you provide a concrete example? I presume you're talking about a parameter, not an argument? (An argument is a value passed to a function, whereas a parameter is the name of an input used within a function.)

@yvvt0379
Copy link

yvvt0379 commented Sep 25, 2021

Sorry to make you confused. Here's an example:

def func(lst: list[int], start: int = 0, end: int = None):
    if end is None:
        end = len(lst)
    # end shouldn't be None here
    ...

In this case, we don't want end to be None. What I worry about is that if we declare end as Optional[int], the code may not be robust enough.

@jakebailey
Copy link
Member

jakebailey commented Sep 27, 2021

I don't really know what the above has to do with the code action being broken; if you don't want to apply the code action, you don't have to. But, in the case above, if you type end as Optional[int], the code that assigns over end with an int if it's None will mean further uses are narrowed to int anyway. If that's not how it's behaving now, that sounds like a bug to me.

@judej judej added bug Something isn't working needs investigation Could be an issue - needs investigation labels Sep 27, 2021
@github-actions github-actions bot removed the triage label Sep 27, 2021
@yvvt0379
Copy link

Ok, thank u, I understand.

@jakebailey jakebailey added fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Sep 28, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.9.4, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202194-29-september-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

5 participants