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

Incorrect type reported on hover with implicit optional parameter #2091

Closed
decorator-factory opened this issue Nov 19, 2021 · 6 comments
Closed
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@decorator-factory
Copy link

Code:

def f(x: str = None):
    return x + "foo"

If you put a reveal_type(x) in the middle, it shows str | None correctly.
However, if you hover over x, you see just str.
image
Seems at least confusing to me.

@erictraut
Copy link
Contributor

I agree this looks like a bug. I've created a corresponding issue in the pyright issue tracker.

@erictraut
Copy link
Contributor

This will be fixed in the next release.

@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 Nov 20, 2021
@bschnurr
Copy link
Member

bschnurr commented Dec 2, 2021

This issue has been fixed in version 2021.12.0, which we've just released. You can find the changelog here: CHANGELOG.md

@bschnurr bschnurr closed this as completed Dec 2, 2021
@hadim
Copy link

hadim commented Mar 6, 2022

I don't know whether I should open a new ticket or not but it seems like the Optional implicit type is not assigned anymore with = None. It affects all my source code since 1-2 weeks now (updated VSCode version).

See the below example:

def hello_world(value: str = None):
    print(value)

hello_world()

Screenshot from 2022-03-06 07-50-58

The code also seems valid according to mypy:

$ mypy type_test.py 
Success: no issues found in 1 source file

@erictraut
Copy link
Contributor

@hadim, this was an intentional change. About a year ago, PEP 484 was updated to recommend that type checkers do not use an implicit Optional by default. We decided to give users a bit of time before changing this default, but we did so recently. If you want the old (now deprecated) behavior, you can set strictParameterNoneValue to "false".

If you are actively maintaining your code base, I'd encourage you to start moving away from implicit Optional.

@hadim
Copy link

hadim commented Mar 6, 2022

Thanks @erictraut. It makes sense and I'll switch my codebase to this practice from now on.

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

4 participants