Search code examples
pythonif-statementtypestypeerrorpylance

Argument of type "SupportsRichComparison" cannot be assigned to parameter "__x" of type "str | ReadableBuffer | SupportsInt | SupportsIndex


I have a list of numbers with some elements being characters instead. I make a function so that it filter out any character, it works but if I use the max() function on the list and assign it to a variable, then compare it with another integer it says:

Argument of type "SupportsRichComparison" cannot be assigned to parameter "__x" of type "str | > > ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc" in function > "new"PylancereportGeneralTypeIssues

Here is the code:

def numOnlyDeck():
    """
    Generates a new deck containing only the numeric values from the player's deck.

    Returns:
        numDeck (list): A list containing only the numeric values from the player's deck.
    """
    numDeck = []
    for v in PlayerDeck:
        strV = str(v)
        if strV.isnumeric():
            numDeck.append(v)
    return numDeck

a = 6
ndeck = numOnlyDeck()
nax = max(ndeck)
if int(nax) >= a:
     pass

vscode applies the error red under the word nax in the if statement.

I want to know what happens and how to fix it.


Solution

  • As per explanation here https://github.com/microsoft/pyright/discussions/5660, you may:

    1. Disable type checking or
    2. Provide explicit type annotation for ndeck such as ndeck: list = numOnlyDeck()