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

Dict .get Type Checking #1915

Closed
ztnel opened this issue Oct 8, 2021 · 3 comments
Closed

Dict .get Type Checking #1915

ztnel opened this issue Oct 8, 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

@ztnel
Copy link

ztnel commented Oct 8, 2021

Hey guys,

I reviewed my code and am pretty confident my typing implementation is correct. I have a dictionary with keys bound to type str | int and values bound to a generic type var _S which is further bound to 3 python object models. I defined a getter for getting an element from the dictionary and typed the return as follows:

def get(self, _id: KeyTypes) -> Optional[_S]:
    return self.registry.get(_id)

However pylance yields the following error on the return line:

Expression of type "_S@Registry | None" cannot be assigned to return type "_S@Registry | None"
  Type "_S@Registry | None" cannot be assigned to type "_S@Registry | None"
    Type "Tower*" cannot be assigned to type "_S@Registry | None"
      Type "Tower*" cannot be assigned to type "_S@Registry"
      Type cannot be assigned to type "None"
    Type "Protocol*" cannot be assigned to type "_S@Registry | None"
      Type "Protocol*" cannot be assigned to type "_S@Registry"
      Type cannot be assigned to type "None"
    Type "User*" cannot be assigned to type "_S@Registry | None"

See the full code snippet at the bottom of the issue. Cheers.

Environment data

  • Language Server version: Pylance language server 2021.10.0 (pyright d5878534)
  • OS and version: MacOS Big Sur 11.2.3 (20D91)
  • Python version (& distribution if applicable, e.g. Anaconda): python3.9

Expected behaviour

Should produce no error

Actual behaviour

yields the following error:
Screen Shot 2021-10-07 at 22 32 55

Logs

XXX

Code Snippet / Additional information

KeyTypes = Union[str, int]
RegistryTypes = Union[Tower, Protocol, User]
_S = TypeVar('_S', bound=RegistryTypes)

class Registry(Generic[_S]):

    def __init__(self) -> None:
        self.registry = {}

    @property
    def registry(self) -> Dict[KeyTypes, _S]:
        return self.__registry

    @registry.setter
    def registry(self, registry: Dict[KeyTypes, _S]) -> None:
        self.__registry = registry
        self.initialized = True

    def get(self, _id: KeyTypes) -> Optional[_S]:
        return self.registry.get(_id)
@github-actions github-actions bot added the triage label Oct 8, 2021
@erictraut
Copy link
Contributor

Thanks for the bug report. I agree this is a bug in the type checker. I'll create a tracking bug in the pyright repo and investigate further when I have a chance.

@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 Oct 8, 2021
@bschnurr
Copy link
Member

bschnurr commented Oct 14, 2021

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