tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

coding_style_python.rst (2224B)


      1 ===================
      2 Python Coding style
      3 ===================
      4 
      5 Coding style
      6 ~~~~~~~~~~~~
      7 
      8 :ref:`black` is the tool used to reformat the Python code.
      9 
     10 Linting
     11 ~~~~~~~
     12 
     13 The Python linting is done by :ref:`Flake8` and :ref:`pylint`
     14 They are executed by mozlint both at review phase and in the CI.
     15 
     16 Indentation
     17 ~~~~~~~~~~~
     18 
     19 Four spaces in Python code.
     20 
     21 
     22 Makefile/moz.build practices
     23 ----------------------------
     24 
     25 -  Changes to makefile and moz.build variables do not require
     26   build-config peer review. Any other build system changes, such as
     27   adding new scripts or rules, require review from the build-config
     28   team.
     29 -  Suffix long ``if``/``endif`` conditionals with #{ & #}, so editors
     30   can display matched tokens enclosing a block of statements.
     31 
     32   ::
     33 
     34      ifdef CHECK_TYPE #{
     35        ifneq ($(flavor var_type),recursive) #{
     36          $(warning var should be expandable but detected var_type=$(flavor var_type))
     37        endif #}
     38      endif #}
     39 
     40 -  moz.build are python and follow normal Python style.
     41 -  List assignments should be written with one element per line. Align
     42   closing square brace with start of variable assignment. If ordering
     43   is not important, variables should be in alphabetical order.
     44 
     45   .. code-block:: python
     46 
     47      var += [
     48          'foo',
     49          'bar'
     50      ]
     51 
     52 -  Use ``CONFIG['TARGET_CPU'] {=arm}`` to test for generic classes of
     53   architecture rather than ``CONFIG['OS_TEST'] {=armv7}`` (re: bug 886689).
     54 
     55 
     56 Other advice
     57 ~~~~~~~~~~~~
     58 
     59 -  Install the
     60   `mozext <https://hg.mozilla.org/hgcustom/version-control-tools/file/default/hgext/mozext>`__
     61   Mercurial extension, and address every issue reported on commit
     62   or the output of ``hg critic``.
     63 -  Follow `PEP 8 <https://www.python.org/dev/peps/pep-0008/>`__. Please run :ref:`black` for this.
     64 -  Do not place statements on the same line as ``if/elif/else``
     65   conditionals to form a one-liner.
     66 -  Global vars, please avoid them at all cost.
     67 -  Exclude outer parenthesis from conditionals.Use
     68   ``if x > 5:,``\ rather than ``if (x > 5):``
     69 -  Use string formatters, rather than var + str(val).
     70   ``var = 'Type %s value is %d'% ('int', 5).``
     71 -  Testing/Unit tests, please write them and make sure that they are executed in the CI.