github dosisod/refurb v1.22.0

latest releases: v2.0.0, v1.28.0, v1.27.0...
13 months ago

This version adds 2 new checks, some bug fixes, and documentation fixes.

Improved Error Messages

Previously Refurb would assume certain identifiers such as Path where imported as Path, not pathlib.Path. This made the context of error messages less helpful since they did not reflect what was actually in the source. Now more checks will use the more accurate pathlib.Path identifier if needed.

Add use-chain-from-iterable check (FURB179)

When flattening a list of lists, use the chain.from_iterable function from the itertools stdlib package. This function is faster than native list/generator comprehensions or using sum() with a list default.

Bad:

from itertools import chain

rows = [[1, 2], [3, 4]]

# using list comprehension
flat = [col for row in rows for col in row]

# using sum()
flat = sum(rows, [])

# using chain(*x)
flat = chain(*rows)

Good:

from itertools import chain

rows = [[1, 2], [3, 4]]

flat = chain.from_iterable(rows)

Note: chain(*x) may be marginally faster/slower depending on the length of x. Since * might potentially expand to a lot of arguments, it is better to use chain.from_iterable() when you are unsure.

Add use-abc-shorthand check (FURB180)

Instead of setting metaclass directly, inherit from the ABC wrapper class. This is semantically the same thing, but more succinct.

Bad:

class C(metaclass=ABCMeta):
    pass

Good:

class C(ABC):
    pass

What's Changed

Full Changelog: v1.21.0...v1.22.0

Don't miss a new refurb release

NewReleases is sending notifications on new releases.