Python Forum
Python linter Pylance: What does "(variable): List | Unbound" mean?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python linter Pylance: What does "(variable): List | Unbound" mean?
#1
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 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 
Reply
#2
It's warning you that a static analysis can't guarantee that new_list will be properly initialized (or bound) before reaching that line. I think you have an indentation error starting on line 37.

In this case new_list is only assigned to inside a conditional. If words is set, then the if condition fails and the block is skipped. Execution resumes at line 43. The return will fail because new_list has no value.

I suspect the if was only supposed to be for the following line to load the dictionary. But the indentation as written keeps the rest of the function inside until the final return.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string using variable found in a list japo85 2 1,235 Jul-11-2022, 08:52 AM
Last Post: japo85
  An IF statement with a List variable dedesssse 3 7,943 Jul-08-2021, 05:58 PM
Last Post: perfringo
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,594 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  Create variable and list dynamically quest_ 12 4,285 Jan-26-2021, 07:14 PM
Last Post: quest_
Question Matching variable to a list index Gilush 17 5,695 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  Print variable values from a list of variables xnightwingx 3 2,569 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  "parameter"is possibly unbound CJ_JOYCE 0 4,802 Aug-23-2020, 10:17 AM
Last Post: CJ_JOYCE
  Multiplication between a list and a variable doug2019 2 2,120 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Sub: Python-3: Better Avoid Naming A Variable As list ? adt 9 3,936 Aug-29-2019, 08:15 AM
Last Post: adt
  Score each word from list variable pythonias2019 6 3,297 Jun-13-2019, 05:44 PM
Last Post: gontajones

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020