Skip to content

Recognize charsets in different orders (FURB156) #21144

@lypwig

Description

@lypwig

Summary

Instead of:

if c in "abcdefghijklmnopqrstuvwxyz":
    print(c)

Use:

from string import ascii_lowercase
if c in ascii_lowercase:
    print(c)

And more generally:

  • abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -> string.ascii_letters
  • abcdefghijklmnopqrstuvwxyz -> string.ascii_lowercase
  • ABCDEFGHIJKLMNOPQRSTUVWXYZ -> string.ascii_uppercase
  • 0123456789 -> string.digits
  • 0123456789abcdefABCDEF -> string.hexdigits
  • 01234567 -> string.octdigits

The main reason is, when using a string literal like abcdefghijklmopqrstuvwxyz it's not abvious when a letter is missing (oops, just did it, did you see it?), so it's a potential source of errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions