- Flake8 is used to validate code style.
- Black is used to enforce code formatting conventions.
- ESLint is used to validate code style for the UI.
- Prettier is used to enforce code formatting conventions for the UI.
- Pylint is used for Python static code analysis.
- Hadolint is used to lint and validate Docker best practices in the Dockerfile.
- MarkdownLint-cli is used to lint and validate Markdown (documentation) files.
Nautobot-specific configuration of these tools is maintained in the files
pyproject.toml as appropriate to the individual tool.
It is strongly recommended to include all of the above tools as part of your commit process before opening any pull request. A Git commit hook is provided in the source at
scripts/git-hooks/pre-commit. Linking to this script from
.git/hooks/ will invoke these tools prior to every commit attempt and abort if the validation fails.
You can also invoke these utilities manually against the development Docker containers by running:
Introducing New Dependencies¶
The introduction of a new dependency is best avoided unless it is absolutely necessary. For small features, it's generally preferable to replicate functionality within the Nautobot code base rather than to introduce reliance on an external project. This reduces both the burden of tracking new releases and our exposure to outside bugs and attacks.
If there's a strong case for introducing a new dependency, it must meet the following criteria:
- Its complete source code must be published and freely accessible without registration.
- Its license must be conducive to inclusion in an open source project.
- It must be actively maintained, with no longer than one year between releases.
- It must be available via the Python Package Index (PyPI).
New dependencies can be added to the project via the
poetry add command. This will correctly add the dependency to
pyproject.toml as well as the
poetry.lock file. You should then update the
pyproject.toml with a comment providing a short description of the package and/or how Nautobot is making use of it.
When in doubt, remain consistent: It is better to be consistently incorrect than inconsistently correct. If you notice in the course of unrelated work a pattern that should be corrected, continue to follow the pattern for now and open a bug so that the entire code base can be evaluated at a later point.
Prioritize readability over concision. Python is a very flexible language that typically offers several options for expressing a given piece of logic, but some may be more friendly to the reader than others. (List comprehensions are particularly vulnerable to over-optimization.) Always remain considerate of the future reader who may need to interpret your code without the benefit of the context within which you are writing it.
No easter eggs. While they can be fun, Nautobot must be considered as a business-critical tool. The potential, however minor, for introducing a bug caused by unnecessary logic is best avoided entirely.
Constants (variables which generally do not change) should be declared in
constants.pywithin each app.
Every model should have a docstring. Every custom method should include an explanation of its function.
The combination of
nautobot.extras.filters.RelationshipModelFilterSetMixinis such a common use case throughout the code base that they have a helper class which combines all of these at
nautobot.extras.NautobotFilterSet. Use this helper class if you need the functionality from these classes.
The combination of
nautobot.extras.forms.NoteModelFormMixinis such a common use case throughout the code base that they have a helper class which combines all of these at
nautobot.extras.forms.NautobotModelForm. Use this helper class if you need the functionality from these classes.
Added in version 1.4.0
Similarly, for filter forms,
nautobot.extras.forms.RelationshipModelFilterFormMixin, and should be used where appropriate.
Similarly, for bulk-edit forms,
nautobot.extras.forms.NoteModelBulkEditFormMixin, and should be used where appropriate.
API serializers for most models should inherit from
nautobot.extras.api.serializers.NautobotModelSerializerand any appropriate mixins. Only use more abstract base classes such as ValidatedModelSerializer where absolutely required.
NautobotModelSerializerwill automatically add serializer fields for
relationships, so there's generally no need to explicitly declare these fields in
.Meta.fieldsof each serializer class. Similarly,
` will automatically add thetags
andstatus` fields when included in a serializer class.
API Views for most models should inherit from
nautobot.extras.api.views.NautobotModelViewSet. Only use more abstract base classes such as
ModelViewSetwhere absolutely required.
- When referring to Nautobot in writing, use the proper form "Nautobot," with the letter N. The lowercase form "nautobot" should be used in code, filenames, etc.
- There is an SVG form of the Nautobot logo at nautobot/docs/nautobot_logo.svg. It is preferred to use this logo for all purposes as it scales to arbitrary sizes without loss of resolution. If a raster image is required, the SVG logo should be converted to a PNG image of the prescribed size.
Importing Python Packages¶
To prevent circular dependency errors and improve code readability, the following standards should be followed when importing from other python modules.
PEP8 Style Guide¶
Nautobot follows the PEP8 style guide's standard for importing modules. Libraries should be imported in these groups: standard library, third party libraries, then
nautobot packages and finally try/except imports. The groups should be separated by a single blank line. Within these groups,import lines should be sorted alphanumerically by the package name. Lists of of names imported from packages should also be sorted alphanumerically.
Wildcard imports (
from foo import *) should only be used in
__init__.py files to import names from submodules that have a
__all__ variable defined.
Importing from External Packages¶
Individual names may be imported from external packages (
from foo import some_function, SomeClass). This differs from the standard for importing from the
Importing Nautobot Packages¶
Module Name Imports¶
Whenever possible, imports from the
nautobot package should use module level imports, not individual names from a module.
Always use absolute imports instead of relative imports.
Import Style Conventions¶
To import modules from other apps under the
nautobot namespace, use the convention
from nautobot.<app_name> import <module> as <app_name>_<module>. If importing from within the same app do not alias the imported namespace.
Resolving Name Conflicts¶
When using external libraries you may need to import multiple different modules with the same name. In this case, the namespace from the external package should be aliased. For aliasing external libraries, use
Nautobot uses convenience imports in the same way that django implements them. These should be leveraged whenever possible.