-
Notifications
You must be signed in to change notification settings - Fork 7
Code Style Guidelines
Gustavo Silva edited this page Feb 10, 2023
·
2 revisions
In this page, you can find the coding guidelines to contribute to the project. Even though some of these may seem difficult and not worthy it, making the contributions look-a-like and predictable does wonders in the code review tasks.
- Use Black so we don't have to discuss code style.
- Multiple text editors / IDEs support Black already.
- Run
pre-commit install
after you install developer requirements to use pre-commit (it runs black, ruff and two other pre-commit hooks for you on every new commit).
- Use Python 3's f-strings to format strings all the time. Prettier and nicer to understand.
-
Except when using the
logging
framework to log errors or exceptions. In those cases, keep the old printf ("%s", foo
) style, passing the variables as arguments. This helps grouping errors in application performance monitoring tools. - This applies specifically to
self.log_exception()
andself.log_error()
calls in commands. - The other log calls (
self.log
,self.log_warning
and the same withlogging
) are optional because these print to stdout only.
-
Except when using the
- Never use bare
except:
. Always use a specific exception or as last resortexcept Exception:
- Inside an
exception
block, uselogger.exception()
orself.log_exception()
. Avoidlog_error
because the full stack trace will not be used. - There's no need to catch exceptions with
exception Exception as e
when callinglog_exception
because the exception context is automatically sent.
- Inside an
-
Avoid
import *
. Follow PEP 8 and/or use isort. -
Never push code or files that contain sensitive data in them, whether that's passwords, tokens, API keys, personal identifiable information and so on. Case in point, never push your
local.env
to GitHub, nor store secrets in GitHub code (maybe in GitHUb's native secret's manager, if that's your choice). - Write code comments to explain the why or explain what is not obvious. Do not overuse them.
- Write unit tests. We use pytest.
- Long term goal: have high coverages in the future and start writing some integration and end-to-end tests.
- Use type hints if you are feeling adventurous.
- Long term goal: run
mypy
on Surface code and enforce no errors.
- Long term goal: run