Python Coding style

Coding style

Black is the tool used to reformat the Python code.

Linting

The Python linting is done byFlake8 andpylintThey are executed by mozlint both at review phase and in the CI.

Indentation

Four spaces in Python code.

Makefile/moz.build practices

  • Changes to makefile and moz.build variables do not requirebuild-config peer review. Any other build system changes, such asadding new scripts or rules, require review from the build-configteam.

  • Suffix longif/endif conditionals with #{ & #}, so editorscan display matched tokens enclosing a block of statements.

    ifdef CHECK_TYPE #{  ifneq ($(flavor var_type),recursive) #{    $(warning var should be expandable but detected var_type=$(flavor var_type))  endif #}endif #}
  • moz.build are python and follow normal Python style.

  • List assignments should be written with one element per line. Alignclosing square brace with start of variable assignment. If orderingis not important, variables should be in alphabetical order.

    var+=['foo','bar']
  • UseCONFIG['TARGET_CPU']{=arm} to test for generic classes ofarchitecture rather thanCONFIG['OS_TEST']{=armv7} (re:bug 886689).

Other advice

  • Install themozextMercurial extension, and address every issue reported on commitor the output ofhgcritic.

  • FollowPEP 8. Please runBlack for this.

  • Do not place statements on the same line asif/elif/elseconditionals to form a one-liner.

  • Global vars, please avoid them at all cost.

  • Exclude outer parenthesis from conditionals.Useifx>5:,rather thanif(x>5):

  • Use string formatters, rather than var + str(val).var='Type%svalueis%d'%('int',5).

  • Testing/Unit tests, please write them and make sure that they are executed in the CI.