From 426efc1d1975ef9691b4ab0765ef0e6400fe308a Mon Sep 17 00:00:00 2001 From: Anonymous Googler Date: Sun, 28 Feb 2021 17:29:06 -0800 Subject: [PATCH] Suggest using os.path.basename or dirname instead of split Compared than discarding a variable (or indexing a temporary tuple) from split, the `basename` and `dirname` functions have names that are descriptive and easier to read. This isn't exactly a huge win for readability but it should be a slight improvement. PiperOrigin-RevId: 360083589 --- refex/fix/fixers/idiom_fixers.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/refex/fix/fixers/idiom_fixers.py b/refex/fix/fixers/idiom_fixers.py index d36f4a1..d7c3d95 100644 --- a/refex/fix/fixers/idiom_fixers.py +++ b/refex/fix/fixers/idiom_fixers.py @@ -236,6 +236,7 @@ def f(x): _NEGATION_CATEGORY = 'pylint.g-comparison-negation' _UNNECESSARY_COMPREHENSION_CATEGORY = 'idioms.uncessary-comprehension' +_OS_PATH_CATEGORY = 'idioms.os-path' _Try = getattr(ast_matchers, 'Try', getattr(ast_matchers, 'TryExcept', None)) # type alias; pylint: disable=invalid-name @@ -396,6 +397,22 @@ def _in_exception_handler(identifier, on_conflict): '{$a for $a in $b}', 'set($b)', category=_UNNECESSARY_COMPREHENSION_CATEGORY), + idiom_fixer( + '$a = os.path.split($b)[0]', + '$a = os.path.dirname($b)', + category=_OS_PATH_CATEGORY), + idiom_fixer( + '$a = os.path.split($b)[1]', + '$a = os.path.basename($b)', + category=_OS_PATH_CATEGORY), + idiom_fixer( + '$a, _ = os.path.split($b)', + '$a = os.path.dirname($b)', + category=_OS_PATH_CATEGORY), + idiom_fixer( + '_ , $a = os.path.split($b)', + '$a = os.path.basename($b)', + category=_OS_PATH_CATEGORY), ] + list(_MUTABLE_CONSTANT_FIXERS) + list(_LOGGING_FIXERS) # TODO(b/152805392): + _NONE_RETURNS_FIXERS