Pylance-release: Walrus operator not recognised

Created on 3 Jul 2020  路  3Comments  路  Source: microsoft/pylance-release

Thank you!!!

Thank you so much for producing Pylance - it's amazingly fast and I really love the grayed out unused imports! :+1: :1st_place_medal:

Environment data

  • Language Server version: v2020.6.1
  • OS and version: Windows 7 :(
  • Python version : 3.8.3

Expected behaviour

Pylance recognises walrus operator and correctly parses code containing it

Actual behaviour

Pylance doesn't recognise walrus operator and throws up errors/warnings

Code Snippet / Additional information

This is a little helper class that's useful for getting the right version of metadata that change over time.

It's got a couple of walrus operators in it but it works without issues in Python 3.8.

class NearestKeyDict(collections.UserDict):
    """Dictionary for looking up nearest key to a given key.
    Useful for 'nearest date' lookups but can work for any keys implementing <= >=

    Based on https://stackoverflow.com/a/3387975/13088500
    """

    def __init__(self, data={}, how="max_before"):
        self.data = data
        self.how = how

    def __getitem__(self, key):
        return self.data[self._keytransform(key)]

    def __setitem__(self, key, value):
        self.data[self._keytransform(key)] = value

    def __delitem__(self, key):
        del self.data[self._keytransform(key)]

    def _keytransform(self, key):
        if self.how != "min_after":
            if len(candidate_keys := [k for k in sorted(self.data) if k <= key]):
                return max(candidate_keys)
            else:
                raise KeyError(f"No data key found before {key}")
        else:
            if len(candidate_keys := [k for k in sorted(self.data) if k >= key]):
                return min(candidate_keys)
            else:
                raise KeyError(f"No data key found after {key}")

walrus_pylance_issue_1

bug fixed in next version

Most helpful comment

Thanks for the bug report. I've fixed the bug, and the fix will be in an upcoming version of Pylance.

All 3 comments

Thanks for the bug report. I've fixed the bug, and the fix will be in an upcoming version of Pylance.

Thank you so much - that was amazingly fast!!

This issue has been fixed in version 2020.7.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202070-9-july-2020

Was this page helpful?
0 / 5 - 0 ratings