Oct-23-2020, 08:13 PM
Hello Pythonistas!
I’m running VS Code. The default Python linter helps. I’m exploring an enhanced Python linter called Pylance. It’s highlighting the
What does that mean? What is the linter trying to say?
When I Google, ‘(variable) : List | Unbound python’, one of the first links is an official Python doc written in a foreign language, written for senior veteran programmers with experience in other languages which also uses unhelpful meta-syntactic variable names which attempts to explain UnboundLocalErrors. That is of little practical use to me (or to anyone, really).
When I Google ‘Pylance unbound variable’, the first search results are Issues on Microsoft’s official pylance-release GitHub repo with other users reporting bugs/false positives.
So you can see the error for yourselves, here is a screenshot of my text editor with my mouse hovering over the underlined variable (with a green mark to indicate the problem area): https://imgur.com/1Til4at
Here is the script I am working with in full:
I’m running VS Code. The default Python linter helps. I’m exploring an enhanced Python linter called Pylance. It’s highlighting the
max()
function with new_list
as the first parameter at line 43 of my script (copied below). When I hover over the underlined variable, a tool-tip appears which reads: Quote:“(variable) new_list: List | Unbound”
“New_list” is possibly unbound Pylance
What does that mean? What is the linter trying to say?
When I Google, ‘(variable) : List | Unbound python’, one of the first links is an official Python doc written in a foreign language, written for senior veteran programmers with experience in other languages which also uses unhelpful meta-syntactic variable names which attempts to explain UnboundLocalErrors. That is of little practical use to me (or to anyone, really).
When I Google ‘Pylance unbound variable’, the first search results are Issues on Microsoft’s official pylance-release GitHub repo with other users reporting bugs/false positives.
So you can see the error for yourselves, here is a screenshot of my text editor with my mouse hovering over the underlined variable (with a green mark to indicate the problem area): https://imgur.com/1Til4at
Here is the script I am working with in full:
"""A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward""" import os import urllib.request import string DICTIONARY = os.path.join('/tmp', 'dictionary_m_words.txt') urllib.request.urlretrieve('http://bit.ly/2Cbj6zn', DICTIONARY) def load_dictionary(): """Load dictionary (sample) and return as generator (done)""" with open(DICTIONARY) as f: return (word.lower().strip() for word in f.readlines()) def is_palindrome(word): """Return if word is palindrome, 'madam' would be one. Case insensitive, so Madam is valid too. It should work for phrases too so strip all but alphanumeric chars. So "No 'x' in 'Nixon'" should pass (see tests for more)""" purged = word.translate(str.maketrans('', '', string.punctuation)) #remove punctuation purged = purged.replace(" ", "") # remove spaces purged = purged.lower().strip() # case insenstive with stripped spaces on either side if purged == purged[::-1]: return True else: return False def get_longest_palindrome(words=None): """ Given a list of words return the longest palindrome If called without argument use the load_dictionary helper to populate the words list """ if words == None: word_list = load_dictionary() new_list = [] # initialize empty list for iter in word_list: if is_palindrome(words): new_list.append(iter) else: continue return max(new_list, key = len) # pull largest word in list of palindromes