If importing objects from submodules, the __init__ file should use a relative import. This is required for type checkers to understand the exposed interface.
Generally, submodules should not be imported in the __init__ file. Submodules should only be exposed when the module is designed to be imported and used as a namespaced object.
For example, we do this for our schema and model modules because it is important to know if you are working with an API schema or database model, both of which may have similar names.
importprefect.server.schemasasschemas# The full module is accessible nowschemas.core.FlowRun
If exposing a submodule, use a relative import as you would when exposing an object.
The from syntax should be reserved for importing objects from modules. Modules should not be imported using the from syntax.
# Correctimportprefect.server.schemas# use with the full nameimportprefect.server.schemasasschemas# use the shorter name
# Wrongfromprefect.serverimportschemas
Unless in an __init__.py file, relative imports should not be used.
# Correctfromprefect.utilities.fooimportbar
# Wrongfrom.utilities.fooimportbar
Imports dependent on file location should never be used without explicit indication it is relative. This avoids confusion about the source of a module.
Sometimes, we must defer an import and perform it within a function to avoid a circular dependency.
## This function in `settings.py` requires a method from the global `context` but the context## uses settingsdeffrom_context():fromprefect.contextimportget_profile_context...
Attempt to avoid circular dependencies. This often reveals overentanglement in the design.
When performing deferred imports, they should all be placed at the top of the function.
Sometimes, imports are slow. We'd like to keep the prefect module import times fast. In these cases, we can lazily import the slow module by deferring import to the relevant function body. For modules that are consumed by many functions, the pattern used for optional requirements may be used instead.
Upon executing a command that creates an object, the output message should offer:
- A short description of what the command just did.
- A bullet point list, rehashing user inputs, if possible.
- Next steps, like the next command to run, if applicable.
- Other relevant, pre-formatted commands that can be copied and pasted, if applicable.
- A new line before the first line and after the last line.
Wrap generated arguments in apostrophes (') to ensure validity by using suffixing formats with !r.
Indent example commands, instead of wrapping in backticks (`).
Use placeholders if the example cannot be pre-formatted completely.
Capitalize placeholder labels and wrap them in less than (<) and greater than (>) signs.
Utilize textwrap.dedent to remove extraneous spacing for strings that are written with triple quotes (""").
Placeholder Example:
Createaworkqueuewithtags:
prefectwork-queuecreate'<WORK QUEUE NAME>'-t'<OPTIONAL TAG 1>'-t'<OPTIONAL TAG 2>'
Dedent Example:
fromtextwrapimportdedent...output_msg=dedent(f""" Created work queue with properties: name - {name!r} uuid - {result} tags - {tagsorNone} deployment_ids - {deployment_idsorNone} Start an agent to pick up flows from the created work queue: prefect agent start -q {name!r} Inspect the created work queue: prefect work-queue inspect {name!r} """)
The Prefect client can be run separately from the Prefect orchestration server and communicate entirely via an API. Among other things, the Prefect client includes anything that runs task or flow code, (e.g. agents, and the Python client) or any consumer of Prefect metadata, (e.g. the Prefect UI, and CLI). The Prefect server stores this metadata and serves it via the REST API.
Sometimes, we make breaking changes to the API (for good reasons). In order to check that a Prefect client is compatible with the API it's making requests to, every API call the client makes includes a three-component API_VERSION header with major, minor, and patch versions.
For example, a request with the X-PREFECT-API-VERSION=3.2.1 header has a major version of 3, minor version 2, and patch version 1.
This version header can be changed by modifying the API_VERSION constant in prefect.server.api.server.
When making a breaking change to the API, we should consider if the change might be backwards compatible for clients, meaning that the previous version of the client would still be able to make calls against the updated version of the server code. This might happen if the changes are purely additive: such as adding a non-critical API route. In these cases, we should make sure to bump the patch version.
In almost all other cases, we should bump the minor version, which denotes a non-backwards-compatible API change. We have reserved the major version chanes to denote also-backwards compatible change that might be significant in some way, such as a major release milestone.