Pylance-release: Unexpected type warning appeared after recent VS Code update

Created on 17 Apr 2021  路  3Comments  路  Source: microsoft/pylance-release

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.)_

    bug fixed in next version

    All 3 comments

    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.

    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!_

    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

    Was this page helpful?
    0 / 5 - 0 ratings