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

Unexpected type warning appeared after recent VS Code update #1168

Closed
eddyg opened this issue Apr 17, 2021 · 3 comments
Closed

Unexpected type warning appeared after recent VS Code update #1168

eddyg opened this issue Apr 17, 2021 · 3 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

@eddyg
Copy link

eddyg commented Apr 17, 2021

Environment data

  • Language Server version: 2021.4.1 (pyright 6410a8a2)
  • OS and version: macOS 10.15.7
  • Python version: 3.8.7 (via pyenv)

Expected behaviour

No type warning.

Actual behaviour

Argument of type "dict[str, str]" cannot be assigned to parameter "param" of type "str | Pattern[Unknown] | Dict[str, str | Sequence[str | Pattern[Unknown] | None]]" in function "test1"
  Type "dict[str, str]" cannot be assigned to type "str | Pattern[Unknown] | Dict[str, str | Sequence[str | Pattern[Unknown] | None]]"
    "dict[str, str]" is incompatible with "str"
    "dict[str, str]" is incompatible with "Pattern[Unknown]"
      TypeVar "_VT@dict" is invariant
        Type "str" cannot be assigned to type "str | Sequence[str | Pattern[Unknown] | None]"
          "str" is incompatible with "Sequence[str | Pattern[Unknown] | None]"

Logs

There didn't seem to be anything of interest in the trace output

Code Snippet / Additional information

After a recent VS Code update, a function (like test1) started generating a type warning that wasn't appearing previously. A slight change in types (like test2) makes the warning disappear.

    def test1(self, param: Union[str, Pattern, Dict[str, Union[str, Sequence[Optional[Union[str, Pattern]]]]]]):
        pass

    def test2(self, param: Union[str, Pattern, Dict[str, Union[str, Sequence[Optional[Union[str, int]]]]]]):
        pass

    self.test1({"str": "str", "str": "str"})  # this generates a type warning 
    self.test2({"str": "str", "str": "str"})  # the test2 version does not

(I'm fairly new to Python typing, so I apologize if I'm missing something obvious here.)

@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release.

In the meantime, you can work around the problem by adding a missing type argument for the generic Pattern type. It should take a single type argument, but you haven't specified any. I suspect that you mean for it to be Pattern[str]. If you omit the type argument, it is assumed to be Any, which is probably not what you had intended. If you enable the reportMissingTypeArgument diagnostic rule in pylance, it will tell you when you've omitted type arguments.

@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 17, 2021
@eddyg
Copy link
Author

eddyg commented Apr 17, 2021

Wow, thanks for the quick reply! 🎉

(I've been assuming a generic AnyStr argument for Pattern, which is OK for my use case.)

Not sure if this issue should be closed now, or after the next release? In any case, feel free to close it as appropriate. Thanks again!

@jakebailey
Copy link
Member

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