PyLint: C0104 (disallowed-name)¶

The PyLint Convention message C0104 is shown if you try to give something a name which is blacklisted. There are many reasons why you may want to blacklist certain names, for example, readability in certain fonts or languages. The following code snippet causes PyLint to raise the C0104 Convention message:

def foo():
    """foo method"""

The message raised by this example is:

C0104: Disallowed name “foo” (disallowed-name)

Why does this raise the message? By default, PyLint comes with a list of names which are disallowed because they are commonly used in examples. If you ever copy one of these example, then probably you want to rename things which are within them. This makes the default list a really useful feature in PyLint.

The default list of disallowed names at time of writing is:

  • foo

  • bar

  • baz

  • toto

  • tutu

  • tata

Custom list of bad names¶

PyLint allows you to customise the list of bad names that it uses when checking code. To do this, you must first create a file called .pylintrc at the root of your codebase, and then add the following to its contents:

[BASIC]
 
bad-names=example,nothing

If we rerun the example above we no longer get the Convention message. The list that we provide here replaces the default list that PyLint uses. For this reason, it is much better practice to update the .pylintrc file as:

[BASIC]
 
bad-names=foo,bar,baz,toto,tutu,tata,example,nothing

If we now create a new code snippet

def foo(nothing):
    example = nothing
    return example

then we will get several warning about the names we have used, based on the list of bad-names that we provided:

C0104: Disallowed name “foo” (disallowed-name)
C0104: Disallowed name “nothing” (disallowed-name)
C0104: Disallowed name “example” (disallowed-name)

Custom patterns of bad names¶

Adding to the list of bad-names that PyLint disallows is a great way to exclude a small number of explicit names. For example, perhaps we could exclude all names that match our project name, because they could be confusing.

There are instances though where we want to disallow all names which match some naming pattern. For example, a common bad style used when people start to program is to suffix variables with numbers. This is not only very unreadable, but also extremely difficult for team members to maintain. In this case, we can use PyLint to guide better coding styles by disallowing such variable names.

Consider the following snippet:

def trapezoid_area(bottom, top, height) -> int:
    calc_1 = bottom + top
    calc_2 = calc_1 / 2
    calc_3 = calc_2 * height
    return calc_3

Here we have the variables calc_1, calc_2, and calc_3 which are very tricky to read and maintain. To disallow such naming, we can update our .pylintrc file to the following:

[BASIC]
 
bad-names=foo,bar,baz,toto,tutu,tata,example,something,nothing
 
bad-names-rgxs=calc_[0-9]*,comp_[0-9]*

The comma-separated list of regular expressions for bad-names-rgxs allows us to describe naming patterns that are disallowed, in this case, anything starting either calc_ or comp_, and ending with a number.

When we re-run PyLint now, we get the following new Convention messages:

C0104: Disallowed name “calc_1” (disallowed-name)
C0104: Disallowed name “calc_2” (disallowed-name)
C0104: Disallowed name “calc_3” (disallowed-name)

Final thoughts¶

PyLint C0104 (disallowed-name) is a really helpful tool to help make code more meaningful and understandable. It is super easy to add both lists of disallowed names, and patterns. These are powerful tools to help more junior developers avoid common mistakes, and drive consistency and maintainability across a codebase.