prod_code_style

single same line for single arg

For any function signature of any function call with a single argument, use the same line without a trailing comma.

Instead of this:

def some_func(
    single_arg,
):
    pass

some_func(
    "single_arg",
)

use this:

def some_func(single_arg):
    pass

some_func("single_arg")

multiple lines for multiple args

For any function signature of any function call with multiple arguments, use separate lines with the terminating trailing comma.

Instead of this:

def some_func(arg_one, arg_two):
    pass

some_func("arg_one", "arg_two")

use this (with exceptions):

def some_func(
    arg_one,
    arg_two,
):
    pass

some_func(
    "arg_one",
    "arg_two",
)

except for these built-in functions, for example:

  • isinstance(object, type)

  • open(path, "w")

  • getattr(object, name)

except for dict operations, for example:

  • os.environ.get(key, default)

  • some_dict.pop(key, default)

single list item per line

Try to split any list items on a separate line as much as possible.

trailing commas

Use terminating comma , for the last item in the list.

multi-word ids

Try to name every variable with at least 2 words embedded into it.

For example:

word1_word2: int = 0

This simplifies refactoring and search.

above comments

Instead of trailing comment:

some_variable: int = 0 # some comment

use above comment:

# some comment:
some_variable: int = 0

use constants

Instead of direct strings, try to use existing constants.

It is easier to refactor and inspect relationships in the code.

use lower case for constant

No need to emphasize constants with UPPER_CASE, use lower_case.

multiline """ comments

Instead of single-line """ comments:

""" comment """

use multi-line """ comments:

"""
comment
"""

AssertionError with message

Instead of assert with message:

assert a == b, "message"

use AssertionError with message:

if a != b:
    raise AssertionError("message")

assert without message

Use assert without message for simple invariants:

assert a == b