Duplicate Python Analyzer feedback for R6201 in generator expression

It seems like the Python Analyzer generates two entries with the same exact feedback when using a tuple for a membership test (instead of a set) inside a generator expression.

My first iteration for the Acronym exercise triggered this bug:

My code
"""Module providing a function that abbreviates phrases."""


def abbreviate(phrase: str) -> str:
    """Returns an abbreviation of the input phrase."""
    phrase = ''.join(' ' if char == '-' else char
                        for char in phrase
                        if char.isalpha() or char in (' ', '-'))

    return ''.join(word[0].upper() for word in phrase.split())

The Analyzer’s feedback:

Analyzer output

Recommended

Line 8 [R6201 use-set-for-membership] was reported by Pylint:

Consider using set for membership test.

This code is emitting a code smell, and may be in need of a re-write or refactor. This doesn’t mean the code is incorrect or buggy in a technical sense, only that it appears to have one or more patterns that could lead to future bugs or maintenance issues. Consider taking a closer look at it.

Instead of:

def fruit_is_dangerous_for_cat(fruit: str) -> bool:
    """This list is only a silly example, don't make decision regarding your cat diet based on it."""
    return fruit in ["cherry", "grapes"]  # [use-set-for-membership]

Try:

def fruit_is_dangerous_for_cat(fruit: str) -> bool:
    """This list is only a silly example, don't make decision regarding your cat diet based on it."""
    return fruit in {"cherry", "grapes"}

Recommended

Line 8 [R6201 use-set-for-membership] was reported by Pylint:

Consider using set for membership test.

This code is emitting a code smell, and may be in need of a re-write or refactor. This doesn’t mean the code is incorrect or buggy in a technical sense, only that it appears to have one or more patterns that could lead to future bugs or maintenance issues. Consider taking a closer look at it.

Instead of:

def fruit_is_dangerous_for_cat(fruit: str) -> bool:
    """This list is only a silly example, don't make decision regarding your cat diet based on it."""
    return fruit in ["cherry", "grapes"]  # [use-set-for-membership]

Try:

def fruit_is_dangerous_for_cat(fruit: str) -> bool:
    """This list is only a silly example, don't make decision regarding your cat diet based on it."""
    return fruit in {"cherry", "grapes"}
1 Like

I think what the analyzer is complaining about is this part:

if char.isalpha() or char in (' ', '-')

What the analyzer is saying is that tuple should be a set:

if char.isalpha() or char in {' ', '-'}

A simpler way to do this could be this:

if char.isalpha() or char in " -"

@rzuckerm, thanks for the suggestion, but this forum thread is about the fact that the Analyzer’s feedback was duplicated, not about getting help on understanding or applying said feedback.
Any efforts to keep the thread on-topic would be appreciated :slight_smile:

@Yrahcaz7 -

I’ve got a potential fix for this and some other analyzer issues queued up. I want to let it sit for a little bit and go back with fresh eyes, so I’ll ping you tomorrow with it when I PR.

2 Likes