Repository: PrefectHQ/prefect (Python)

Issue:
Prefect Cloud: `prefect run flow` creates a flow run with no labels
## Description

It seems that `prefect` 0.14.13 or 0.14.14 may have introduced a regression in `prefect run flow`. In previous versions, `prefect run flow` would trigger a flow run with the labels on the registered flow. As of `0.14.13`, it seems that `prefect run flow` creates flow runs with no labels, which can lead to flows being stranded like this:

![stranded-flow](https://user-images.githubusercontent.com/7608904/112901355-461bd000-90aa-11eb-8c16-36affa4d1c87.png)

> You have no currently running Agents configured to pick up flow runs without labels; you may need to add labels to your Flow Run.

## Expected Behavior

Given a flow has already been registered with Prefect Cloud and that it has labels set, running something like the following should trigger a flow run that is successfully matched to an agent based on the flow's labels.

```shell
prefect run flow \
    --name ${PREFECT_FLOW_NAME} \
    --project ${PREFECT_PROJECT_NAME}
```

## Reproduction

1. Run a `LocalAgent` 

```shell
export PREFECT_RUNNER_TOKEN=# your runner token #
prefect agent local start -t ${PREFECT_RUNNER_TOKEN} --label "unicorn"
```

2. In a separate shell, authenticate with Prefect Cloud and create a project

```shell
export PREFECT_USER_TOKEN=# your user token #
prefect auth login -t ${PREFECT_USER_TOKEN}

prefect create project dask-iz-gr8
```

3. From a Python session in that shell, register a simple flow called "test-flow" with Prefect Cloud

```python
from prefect import task, Flow

@task
def do_stuff() -> str:
    return "no"

with Flow(name="test-flow") as flow:
    do_stuff()

flow.register(
    project_name="dask-iz-gr8",
    labels=[
        "unicorn"
    ]
)
```

In the Prefect Cloud UI, you should now see that this flow has been registered and that it has the label "unicorn".

![image](https://user-images.githubusercontent.com/7608904/112902447-c1ca4c80-90ab-11eb-9e5e-c8c2d2c82443.png)

4. Kick off a flow run with `prefect` 0.14.12. That will result in the flow running successfully on your LocalAgent.

```shell
docker run \
    -t prefecthq/prefect:0.14.12-python3.7 \
    /bin/bash -c "prefect auth login -t ${PREFECT_USER_TOKEN} && prefect run flow --name test-flow --project dask-iz-gr8"
```

In the agent logs:

```text
[2021-03-29 21:22:44,143] INFO - agent | Waiting for flow runs...
[2021-03-29 21:29:59,162] INFO - agent | Found 1 flow run(s) to submit for execution.
[2021-03-29 21:29:59,350] INFO - agent | Deploying flow run d3b26a98-0113-41d2-8116-e39d620f22cb
```

The UI will show one successful run

![image](https://user-images.githubusercontent.com/7608904/112902689-1b327b80-90ac-11eb-9a44-422798bdab5d.png)

5. Try triggering another flow run, this time with `prefect` 0.14.14. You should see that it fails to match your LocalAgent and Prefect Cloud complains about missing labels.

```shell
docker run \
    -t prefecthq/prefect:0.14.14-python3.7 \
    /bin/bash -c "prefect auth login -t ${PREFECT_USER_TOKEN} && prefect run flow --name test-flow --project dask-iz-gr8"
```

Notice the "labels: None" in this screenshot. And I don't see any logs in the `LocalAgent` that indicate that it has received a flow run.

![image](https://user-images.githubusercontent.com/7608904/112902870-65b3f800-90ac-11eb-8100-c23a2ecdbd94.png)

## Environment

<details><summary>output of 'conda info'</summary>

```shell
     active environment : None
       user config file : /home/jlamb/.condarc
 populated config files : 
          conda version : 4.9.2
    conda-build version : not installed
         python version : 3.7.6.final.0
       virtual packages : __glibc=2.27=0
                          __unix=0=0
                          __archspec=1=x86_64
       base environment : /home/jlamb/miniconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/jlamb/miniconda3/pkgs
                          /home/jlamb/.conda/pkgs
       envs directories : /home/jlamb/miniconda3/envs
                          /home/jlamb/.conda/envs
               platform : linux-64
             user-agent : conda/4.9.2 requests/2.22.0 CPython/3.7.6 Linux/5.4.0-70-generic ubuntu/18.04.4 glibc/2.27
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False
```

</details>

<details><summary>output of 'conda list'</summary>

```shell
# packages in environment at /home/jlamb/miniconda3:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
adal                      1.2.5                    pypi_0    pypi
aiobotocore               1.2.0                    pypi_0    pypi
aiohttp                   3.6.3                    pypi_0    pypi
aioitertools              0.7.0                    pypi_0    pypi
alabaster                 0.7.12                   pypi_0    pypi
appdirs                   1.4.4                    pypi_0    pypi
applicationinsights       0.11.9                   pypi_0    pypi
argon2-cffi               20.1.0                   pypi_0    pypi
asn1crypto                1.3.0                    py37_0  
astroid                   2.5                      pypi_0    pypi
async-timeout             3.0.1                    pypi_0    pypi
attrs                     19.3.0                   pypi_0    pypi
awscli                    1.18.222                 pypi_0    pypi
azure-common              1.1.25                   pypi_0    pypi
azure-core                1.8.0                    pypi_0    pypi
azure-graphrbac           0.61.1                   pypi_0    pypi
azure-identity            1.4.1                    pypi_0    pypi
azure-mgmt-authorization  0.61.0                   pypi_0    pypi
azure-mgmt-containerregistry 2.8.0                    pypi_0    pypi
azure-mgmt-keyvault       2.2.0                    pypi_0    pypi
azure-mgmt-resource       10.2.0                   pypi_0    pypi
azure-mgmt-storage        11.2.0                   pypi_0    pypi
azure-storage-blob        12.4.0                   pypi_0    pypi
azureml-automl-core       1.16.0                   pypi_0    pypi
azureml-core              1.16.0.post1             pypi_0    pypi
azureml-dataprep          2.3.4                    pypi_0    pypi
azureml-dataprep-native   23.0.0                   pypi_0    pypi
azureml-dataprep-rslex    1.1.3                    pypi_0    pypi
azureml-dataset-runtime   1.16.0                   pypi_0    pypi
azureml-pipeline          1.16.0                   pypi_0    pypi
azureml-pipeline-core     1.16.0                   pypi_0    pypi
azureml-pipeline-steps    1.16.0                   pypi_0    pypi
azureml-sdk               1.16.0                   pypi_0    pypi
azureml-telemetry         1.16.0                   pypi_0    pypi
azureml-train             1.16.0                   pypi_0    pypi
azureml-train-automl-client 1.16.0                   pypi_0    pypi
azureml-train-core        1.16.0                   pypi_0    pypi
azureml-train-restclients-hyperdrive 1.16.0                   pypi_0    pypi
babel                     2.8.0                    pypi_0    pypi
backcall                  0.2.0                    pypi_0    pypi
backports-tempfile        1.0                      pypi_0    pypi
backports-weakref         1.0.post1                pypi_0    pypi
bcrypt                    3.2.0                    pypi_0    pypi
beautifulsoup4            4.9.3                    pypi_0    pypi
black                     20.8b1                     py_0    conda-forge
bleach                    3.1.5                    pypi_0    pypi
blosc                     1.9.2                    pypi_0    pypi
bokeh                     2.2.3                    pypi_0    pypi
boto3                     1.16.62                  pypi_0    pypi
botocore                  1.19.52                  pypi_0    pypi
breathe                   4.24.1                   pypi_0    pypi
brotlipy                  0.7.0           py37hb5d75c8_1001    conda-forge
ca-certificates           2021.1.19            h06a4308_0  
cached-property           1.5.2                    pypi_0    pypi
cachetools                4.1.1                    pypi_0    pypi
cachey                    0.2.1                    pypi_0    pypi
certifi                   2020.12.5        py37h06a4308_0  
cffi                      1.14.0           py37h2e261b9_0  
chardet                   3.0.4                 py37_1003  
click                     7.1.2                    pypi_0    pypi
cloudpickle               1.6.0                    pypi_0    pypi
conda                     4.9.2            py37h06a4308_0  
conda-package-handling    1.6.0            py37h7b6447c_0  
contextlib2               0.6.0.post1              pypi_0    pypi
coverage                  5.2.1                    pypi_0    pypi
cpplint                   1.5.4                    pypi_0    pypi
croniter                  0.3.34                   pypi_0    pypi
cryptography              2.8              py37h1ba5d50_0  
cycler                    0.10.0                   pypi_0    pypi
dask                      2021.3.0                 pypi_0    pypi
dask-glm                  0.2.0                    pypi_0    pypi
dask-ml                   1.7.0                    pypi_0    pypi
dask-saturn               0.2.2                    pypi_0    pypi
dask-sphinx-theme         1.3.5                    pypi_0    pypi
dataclasses               0.6                      pypi_0    pypi
decorator                 4.4.2                    pypi_0    pypi
defusedxml                0.6.0                    pypi_0    pypi
distributed               2021.3.1                 pypi_0    pypi
distro                    1.5.0                    pypi_0    pypi
docker                    4.3.1                    pypi_0    pypi
docker-compose            1.27.4                   pypi_0    pypi
dockerpty                 0.4.1                    pypi_0    pypi
docopt                    0.6.2                    pypi_0    pypi
dotnetcore2               2.1.17                   pypi_0    pypi
entrypoints               0.3                      pypi_0    pypi
flake8                    3.8.4                    pypi_0    pypi
fsspec                    0.8.4                    pypi_0    pypi
fusepy                    3.0.1                    pypi_0    pypi
future                    0.18.2                   pypi_0    pypi
google-auth               1.20.1                   pypi_0    pypi
heapdict                  1.0.1                    pypi_0    pypi
idna                      2.8                      py37_0  
imagesize                 1.2.0                    pypi_0    pypi
importlib-metadata        1.7.0                    pypi_0    pypi
importlib-resources       3.0.0                    pypi_0    pypi
iniconfig                 1.0.1                    pypi_0    pypi
ipykernel                 5.3.4                    pypi_0    pypi
ipython                   7.17.0                   pypi_0    pypi
ipython-genutils          0.2.0                    pypi_0    pypi
ipywidgets                7.5.1                    pypi_0    pypi
isodate                   0.6.0                    pypi_0    pypi
isort                     5.7.0              pyhd3eb1b0_0  
jedi                      0.17.2                   pypi_0    pypi
jeepney                   0.4.3                    pypi_0    pypi
jinja2                    2.11.2                   pypi_0    pypi
joblib                    0.17.0                   pypi_0    pypi
json5                     0.9.5                    pypi_0    pypi
jsonpickle                1.4.1                    pypi_0    pypi
jsonschema                3.2.0                    pypi_0    pypi
jupyter                   1.0.0                    pypi_0    pypi
jupyter-client            6.1.6                    pypi_0    pypi
jupyter-console           6.1.0                    pypi_0    pypi
jupyter-core              4.6.3                    pypi_0    pypi
jupyterlab                2.2.4                    pypi_0    pypi
jupyterlab-server         1.2.0                    pypi_0    pypi
kiwisolver                1.3.1                    pypi_0    pypi
krb5                      1.16.4               h173b8e3_0  
kubernetes                12.0.0a1                 pypi_0    pypi
kubernetes-asyncio        12.0.1                   pypi_0    pypi
lazy-object-proxy         1.4.3                    pypi_0    pypi
ld_impl_linux-64          2.33.1               h53a641e_7  
libedit                   3.1.20181209         hc058e9b_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 9.1.0                hdf63c60_0  
libpq                     11.2                 h20c2e04_0  
libstdcxx-ng              9.1.0                hdf63c60_0  
lightgbm                  3.2.0.99                 pypi_0    pypi
llvmlite                  0.34.0                   pypi_0    pypi
locket                    0.2.0                    pypi_0    pypi
lz4                       3.1.0                    pypi_0    pypi
markupsafe                1.1.1                    pypi_0    pypi
marshmallow               3.7.1                    pypi_0    pypi
marshmallow-oneofschema   2.0.1                    pypi_0    pypi
matplotlib                3.3.3                    pypi_0    pypi
mccabe                    0.6.1                    pypi_0    pypi
mistune                   0.8.4                    pypi_0    pypi
more-itertools            8.4.0                    pypi_0    pypi
msal                      1.5.1                    pypi_0    pypi
msal-extensions           0.2.2                    pypi_0    pypi
msgpack                   1.0.0                    pypi_0    pypi
msrest                    0.6.18                   pypi_0    pypi
msrestazure               0.6.4                    pypi_0    pypi
multidict                 4.7.6                    pypi_0    pypi
multipledispatch          0.6.0                    pypi_0    pypi
mypy                      0.800                    pypi_0    pypi
mypy-extensions           0.4.3                    pypi_0    pypi
mypy_extensions           0.4.3            py37hc8dfbb8_1    conda-forge
natsort                   7.0.1                    pypi_0    pypi
nbconvert                 5.6.1                    pypi_0    pypi
nbformat                  5.0.7                    pypi_0    pypi
nbqa                      0.5.6                    pypi_0    pypi
ncurses                   6.2                  he6710b0_0  
ndg-httpsclient           0.5.1                    pypi_0    pypi
notebook                  6.1.3                    pypi_0    pypi
numba                     0.51.2                   pypi_0    pypi
numpy                     1.18.1                   pypi_0    pypi
numpydoc                  1.1.0                    pypi_0    pypi
oauthlib                  3.1.0                    pypi_0    pypi
openssl                   1.1.1i               h27cfd23_0  
oscrypto                  1.2.1                    pypi_0    pypi
packaging                 20.4                     pypi_0    pypi
pandas                    1.2.3                    pypi_0    pypi
pandocfilters             1.4.2                    pypi_0    pypi
paramiko                  2.7.2                    pypi_0    pypi
parso                     0.7.1                    pypi_0    pypi
partd                     1.1.0                    pypi_0    pypi
pathspec                  0.8.0              pyh9f0ad1d_0    conda-forge
pendulum                  2.1.2                    pypi_0    pypi
pexpect                   4.8.0                    pypi_0    pypi
pickleshare               0.7.5                    pypi_0    pypi
pillow                    8.0.0                    pypi_0    pypi
pip                       20.2.2                   py37_0    anaconda
pluggy                    0.13.1                   pypi_0    pypi
portalocker               1.7.1                    pypi_0    pypi
prefect                   0.14.14                  pypi_0    pypi
prefect-saturn            0.4.4                    pypi_0    pypi
prometheus-client         0.8.0                    pypi_0    pypi
prompt-toolkit            3.0.6                    pypi_0    pypi
psutil                    5.7.2                    pypi_0    pypi
psycopg2                  2.8.2            py37h72c5cf5_0    conda-forge
ptyprocess                0.6.0                    pypi_0    pypi
py                        1.9.0                    pypi_0    pypi
pyarrow                   1.0.1                    pypi_0    pypi
pyasn1-modules            0.2.8                    pypi_0    pypi
pycodestyle               2.6.0                      py_0  
pycosat                   0.6.3            py37h7b6447c_0  
pycparser                 2.19                     py37_0  
pycryptodomex             3.9.8                    pypi_0    pypi
pydocstyle                5.1.1                      py_0  
pyflakes                  2.2.0                    pypi_0    pypi
pygments                  2.6.1                    pypi_0    pypi
pylint                    2.7.0                    pypi_0    pypi
pynacl                    1.4.0                    pypi_0    pypi
pyopenssl                 19.1.0                   py37_0  
pyparsing                 2.4.7                    pypi_0    pypi
pyrsistent                0.16.0                   pypi_0    pypi
pysocks                   1.7.1                    py37_0  
pytest                    6.2.2                    pypi_0    pypi
pytest-cov                2.10.0                   pypi_0    pypi
python                    3.7.6                h0371630_2  
python-box                5.1.1                    pypi_0    pypi
python-digitalocean       1.15.0                   pypi_0    pypi
python-dotenv             0.14.0                   pypi_0    pypi
python-graphviz           0.15                     pypi_0    pypi
python_abi                3.7                     1_cp37m    conda-forge
pytz                      2020.1                   pypi_0    pypi
pytzdata                  2020.1                   pypi_0    pypi
pyyaml                    5.3.1                    pypi_0    pypi
pyzmq                     19.0.2                   pypi_0    pypi
qtconsole                 4.7.5                    pypi_0    pypi
qtpy                      1.9.0                    pypi_0    pypi
readline                  7.0                  h7b6447c_5  
regex                     2020.7.14        py37h8f50634_0    conda-forge
requests                  2.22.0                   py37_1  
requests-oauthlib         1.3.0                    pypi_0    pypi
responses                 0.10.16                  pypi_0    pypi
ruamel-yaml               0.16.10                  pypi_0    pypi
ruamel-yaml-clib          0.2.0                    pypi_0    pypi
ruamel_yaml               0.15.87          py37h7b6447c_0  
s3fs                      0.5.1                    pypi_0    pypi
saturn-prefect-agent      0.1.0                    pypi_0    pypi
scikit-learn              0.23.2                   pypi_0    pypi
scipy                     1.5.3                    pypi_0    pypi
secretstorage             3.1.2                    pypi_0    pypi
send2trash                1.5.0                    pypi_0    pypi
setuptools                45.2.0                   py37_0  
shellcheck                0.7.1                         0    conda-forge
six                       1.14.0                   py37_0  
snowballstemmer           2.0.0                      py_0  
sortedcontainers          2.2.2                    pypi_0    pypi
soupsieve                 2.1                      pypi_0    pypi
sparse                    0.11.2                   pypi_0    pypi
sphinx                    3.3.0                    pypi_0    pypi
sphinx-click              2.7.1                    pypi_0    pypi
sphinx-rtd-theme          0.5.0                    pypi_0    pypi
sphinxcontrib-applehelp   1.0.2                    pypi_0    pypi
sphinxcontrib-devhelp     1.0.2                    pypi_0    pypi
sphinxcontrib-htmlhelp    1.0.3                    pypi_0    pypi
sphinxcontrib-jsmath      1.0.1                    pypi_0    pypi
sphinxcontrib-qthelp      1.0.3                    pypi_0    pypi
sphinxcontrib-serializinghtml 1.1.4                    pypi_0    pypi
spongebobcase             0.4.0                    pypi_0    pypi
sqlalchemy                1.3.22                   pypi_0    pypi
sqlite                    3.31.1               h7b6447c_0  
stripe                    2.55.1                   pypi_0    pypi
tabulate                  0.8.7                    pypi_0    pypi
tblib                     1.7.0                    pypi_0    pypi
terminado                 0.8.3                    pypi_0    pypi
testpath                  0.4.4                    pypi_0    pypi
text-unidecode            1.3                      pypi_0    pypi
texttable                 1.6.3                    pypi_0    pypi
threadpoolctl             2.1.0                    pypi_0    pypi
tk                        8.6.8                hbc83047_0  
toml                      0.10.1                   pypi_0    pypi
toolz                     0.10.0                   pypi_0    pypi
torch                     1.7.0                    pypi_0    pypi
tornado                   6.0.4                    pypi_0    pypi
tqdm                      4.42.1                     py_0  
traitlets                 4.3.3                    pypi_0    pypi
typed-ast                 1.4.1            py37h516909a_0    conda-forge
typing_extensions         3.7.4.2                    py_0    conda-forge
urllib3                   1.25.10                  pypi_0    pypi
wcwidth                   0.2.5                    pypi_0    pypi
webencodings              0.5.1                    pypi_0    pypi
websocket-client          0.57.0                   pypi_0    pypi
wheel                     0.34.2                   py37_0  
widgetsnbextension        3.5.1                    pypi_0    pypi
wrapt                     1.12.1                   pypi_0    pypi
xarray                    0.16.2                   pypi_0    pypi
xgboost                   1.2.1                    pypi_0    pypi
xz                        5.2.4                h14c3975_4  
yaml                      0.1.7                had09818_2  
yarl                      1.5.1                    pypi_0    pypi
zict                      2.0.0                    pypi_0    pypi
zipp                      3.1.0                    pypi_0    pypi
zlib                      1.2.11               h7b6447c_3  

```

</details>



Discussion:
I wanted to keep speculation out of the initial issue description, so adding a comment here. This change seems relevant: https://github.com/PrefectHQ/prefect/pull/4266.

I wonder if the default value of `labels` coming from `click` is now `labels=[]`, which means Prefect Cloud is getting `labels=[]` when it previously would have received `labels=None`, and maybe that is leading Prefect Cloud to think "oh, this flow run is overriding the labels stored on the flow".
Yeah, I think that's the issue here. Thanks for raising this - would you want to submit a fix?
Sure! I'd be happy to.



Patch:
diff --git a/changes/pr4316.yaml b/changes/pr4316.yaml
new file mode 100644
index 000000000000..fb7f684d471e
--- /dev/null
+++ b/changes/pr4316.yaml
@@ -0,0 +1,5 @@
+fix:
+  - "Use default flow labels when triggering flow runs from CLI - [#4316](https://github.com/PrefectHQ/prefect/pull/4316)"
+
+contributor:
+  - "[James Lamb](https://github.com/jameslamb)"
diff --git a/src/prefect/cli/run.py b/src/prefect/cli/run.py
index 7d2aa88d963b..917d56d279e8 100644
--- a/src/prefect/cli/run.py
+++ b/src/prefect/cli/run.py
@@ -170,6 +170,9 @@ def flow(
         )
         return
 
+    if labels == ():
+        labels = None
+
     client = Client()
     flow_id = id
     if not flow_id and not version_group_id:


Test patch:
diff --git a/tests/cli/test_run.py b/tests/cli/test_run.py
index 9a4649d36e43..9aed2d1ba79a 100644
--- a/tests/cli/test_run.py
+++ b/tests/cli/test_run.py
@@ -244,6 +244,39 @@ def test_run_flow_param_file(monkeypatch, cloud_api):
         assert create_flow_run_mock.call_args[1]["parameters"] == {"test": 42}
 
 
+def test_run_flow_no_labels_provided(monkeypatch, cloud_api):
+    post = MagicMock(
+        return_value=MagicMock(
+            json=MagicMock(return_value=dict(data=dict(flow=[{"id": "flow"}])))
+        )
+    )
+    session = MagicMock()
+    session.return_value.post = post
+    monkeypatch.setattr("requests.Session", session)
+
+    create_flow_run_mock = MagicMock(return_value="id")
+    monkeypatch.setattr("prefect.client.Client.create_flow_run", create_flow_run_mock)
+    monkeypatch.setattr(
+        "prefect.client.Client.get_default_tenant_slug", MagicMock(return_value="tslug")
+    )
+
+    runner = CliRunner()
+    result = runner.invoke(
+        run,
+        [
+            "flow",
+            "--name",
+            "flow",
+            "--project",
+            "project",
+        ],
+    )
+    assert result.exit_code == 0
+    assert "Flow Run" in result.output
+    assert create_flow_run_mock.called
+    assert create_flow_run_mock.call_args[1]["labels"] is None
+
+
 def test_run_flow_param_string(monkeypatch, cloud_api):
     post = MagicMock(
         return_value=MagicMock(