Open-source News

PyLint: The good, the bad, and the ugly

opensource.com - Mon, 09/19/2022 - 15:00
PyLint: The good, the bad, and the ugly Moshe Zadka Mon, 09/19/2022 - 03:00

Hot take: PyLint is actually good!

"PyLint can save your life" is an exaggeration, but not as much as you might think! PyLint can keep you from really really hard to find and complicated bugs. At worst, it can save you the time of a test run. At best, it can help you avoid complicated production mistakes.

The good

I'm embarrassed to say how common this can be. Naming tests is perpetually weird: Nothing cares about the name, and there's often not a natural name to be found. For instance, look at this code:

def test_add_small():
    # Math, am I right?
    assert 1 + 1 == 3
   
def test_add_large():
    assert 5 + 6 == 11
   
def test_add_small():
    assert 1 + 10 == 11

The test works:

collected 2 items                                                                        
test.py ..
2 passed

More Python resources What is an IDE? Cheat sheet: Python 3.7 for beginners Top Python GUI frameworks Download: 7 essential PyPI libraries Red Hat Developers Latest Python articles But here's the kicker: If you override a name, the testing infrastructure happily skips over the test!

In reality, these files can be hundreds of lines long, and the person adding the new test might not be aware of all the names. Unless someone is looking at test output carefully, everything looks fine.

Worst of all, the addition of the overriding test, the breakage of the overridden test, and the problem that results in prod might be separated by days, months, or even years.

PyLint finds it

But like a good friend, PyLint is there for you.

test.py:8:0: E0102: function already defined line 1
     (function-redefined)The bad

Like a 90s sitcom, the more you get into PyLint, the more it becomes problematic. This is completely reasonable code for an inventory modeling program:

"""Inventory abstractions"""

import attrs

@attrs.define
class Laptop:
    """A laptop"""
    ident: str
    cpu: str

It seems that PyLint has opinions (probably formed in the 90s) and is not afraid to state them as facts:

$ pylint laptop.py | sed -n '/^laptop/s/[^ ]*: //p'
R0903: Too few public methods (0/2) (too-few-public-methods)The ugly

Ever wanted to add your own unvetted opinion to a tool used by millions? PyLint has 12 million monthly downloads.

"People will just disable the whole check if it's too picky." —PyLint issue 6987, July 3rd, 2022

The attitude it takes towards adding a test with potentially many false positives is..."eh."

Making it work for you

PyLint is fine, but you need to interact with it carefully. Here are the three things I recommend to make PyLint work for you.

1. Pin it

Pin the PyLint version you use to avoid any surprises!

In your .toml file:

[project.optional-dependencies]
pylint = ["pylint"]

In your code:

from unittest import mock

This corresponds with code like this:

# noxfile.py
...
@nox.session(python=VERSIONS[-1])
def refresh_deps(session):
    """Refresh the requirements-*.txt files"""
    session.install("pip-tools")
    for deps in [..., "pylint"]:
        session.run(
            "pip-compile",
            "--extra",
            deps,
            "pyproject.toml",
            "--output-file",
            f"requirements-{deps}.txt",
        )2. Default deny

Disable all checks. Then enable ones that you think have a high value-to-false-positive ratio. (Not just false-negative-to-false-positive ratio!)

# noxfile.py
...
@nox.session(python="3.10")
def lint(session):
    files = ["src/", "noxfile.py"]
    session.install("-r", "requirements-pylint.txt")
    session.install("-e", ".")
    session.run(
        "pylint",
        "--disable=all",
        *(f"--enable={checker}" for checker in checkers)
        "src",
    )3. Checkers

These are some of the ones I like. Enforce consistency in the project, avoid some obvious mistakes.

checkers = [
    "missing-class-docstring",
    "missing-function-docstring",
    "missing-module-docstring",
    "function-redefined",
]Using PyLint

You can take just the good parts of PyLint. Run it in CI to keep consistency, and use the highest value checkers.

Lose the bad parts: Default deny checkers.

Avoid the ugly parts: Pin the version to avoid surprises.

Get the most out of PyLint.

Image by:

Opensource.com

Python What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

10 Most Dangerous Commands – You Should Never Execute on Linux

Tecmint - Mon, 09/19/2022 - 13:22
The post 10 Most Dangerous Commands – You Should Never Execute on Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides .

The command-line interface is a powerful and handy utility for administering a Linux system. It provides a fast and versatile way of running the system, especially when managing headless systems which do not have

The post 10 Most Dangerous Commands – You Should Never Execute on Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides.

Red Hat Customer Portal recognized by the Association of Support Professionals as one the Best Support Websites of 2022

Red Hat News - Mon, 09/19/2022 - 12:00

The Association of Support Professionals (ASP) announced the Red Hat Customer Portal as a winner of the Best Support Websites of 2022 for the 12th consecutive year. Our Customer Portal delivers comprehensive product documentation, intelligent troubleshooting tools, security updates, technical support, as well as Red Hat expert and community-powered knowledge—helping customers plan, deploy, maintain and manage their Red Hat solutions. 

Linux 6.0-rc6 Released After Kernel Developers Spent The Week In Dublin

Phoronix - Mon, 09/19/2022 - 05:26
Linus Torvalds just released Linux 6.0-rc6 and it's artificially tiny due to many of the kernel developers being preoccupied for the week...

Intel Begins Working On Linux Support For Data Streaming Accelerator 2.0

Phoronix - Mon, 09/19/2022 - 01:58
Back in 2019 is when Intel first detailed the Data Streaming Accelerator "DSA" and began working on the Linux enablement patches. The Intel DSA is designed to help accelerate streaming and transformation operations for storage, networking, and persistent memory. Finally the DSA is coming to market by way of being found within forthcoming Xeon Scalable "Sapphire Rapids" processors while the DSA 2.0 accelerator is already in the works for future processors...

DDNet Cooperative Platformer Game Adds Vulkan Renderer

Phoronix - Mon, 09/19/2022 - 01:30
DDraceNetwork "DDNet" as an open-source cross-platform game building off the DDRace and Teeworlds games recently introduced a Vulkan renderer...

Linux getrandom() vDSO Patch Updated For ~15x Speedup

Phoronix - Sun, 09/18/2022 - 21:41
Over the summer Jason Donenfeld of WireGuard fame proposed adding getrandom() to the vDSO for better performance to enjoy by user-space developers. This past week he sent out the latest version of this proposed kernel patch where he's seeing around a ~15x speed-up with this change...

MGLRU v15 Published For Last Minute Testing Of This Major Linux Improvement

Phoronix - Sun, 09/18/2022 - 21:24
Google engineer Yu Zhao this morning published MGLRU v15, the latest revision to this patch series dealing with improving the Linux kernel's page reclamation code. Multi-Gen LRU "MGLRU" has proven to offer performance benefits and particularly improve the Linux experience when dealing with low-memory situations...

GStreamer Now Able To Ship Rust-Written Plugins

Phoronix - Sun, 09/18/2022 - 17:50
Along with the Linux kernel preparing for its initial Rust integration, Rusticl landing in Mesa this week as the first major Rust usage within Mesa, and Cloudflare announcing an Nginx HTTP proxy replacement written in Rust, some additional Rust adoption news for the week is that the GStreamer project is now ready to ship Rust-written plug-ins as part of their official binary releases...

Pages