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

Add type inference for dict.keys membership #13372

Merged

Commits on Aug 23, 2022

  1. Add type inference for dict.keys membership

    Also for containership checks on `typing.KeysView.` This is to bring the
    following cases into alignment:
    
        from typing import KeysView
    
        key: str | None
        d: dict[str, int]
        kv: KeysView[str]
    
        if key in d:
            # type of 'key' is inferred to be 'str'
            ...
    
        if key in d.keys():
            # type of 'key' should be inferred to be 'str'
            ...
    
        if key in kv:
            # type of 'key' should be inferred to be 'str'
            ...
    
    Before this change only the first `if` would narrow the type of `key`.
    
    I've just added a test under `test-data/unit/pythoneval.test` as
    `test-data/unit/fixtures/dict.pyi` doesn't include `dict.keys`, and
    adding it requires importing `dict_keys` from `_collections_abc` in
    those stubs, which then requires adding `_collections_abc.pyi` stubs,
    which would have to be complete since e.g.
    `testGenericAliasCollectionsABCReveal` expects most of the types in
    those stubs to be defined (this is the same approach as
    7678f28).
    
    GH: issue python#13360
    matthewhughes934 authored and Matt Hughes committed Aug 23, 2022
    Configuration menu
    Copy the full SHA
    75ca813 View commit details
    Browse the repository at this point in the history