Repository: PrefectHQ/prefect

Issue:
Ignore calls to flow.register when parsing a flow using file based storage
## Current behavior
<!-- Please describe how the feature works today -->
If a flow script contains a reference to the `.register` method, it'll fail with this error:
```
Failed to load and execute Flow's environment: RuntimeError('Attempting to call `flow.register` during execution of flow file will lead to unexpected results.')
```


## Proposed behavior
<!-- Please describe your proposed change to the current behavior -->
Flow runner ignores calls to the `.register` method.


## Example
<!-- Please give an example of how the enhancement would be useful -->


Discussion:
Hello @znicholasbrown , I would like to contribute on this issue. I tried to reproduce the issue without any success, probably, due to my limited experience with prefect. Could you provide a minimal example of this case?
Hey @sp1thas you could reproduce this behavior with something like this:


```python
# my_flow.py
from prefect import Flow

f = Flow("my-flow")

f.register()
```

```bash
prefect register flow -f my_flow.py
```

Here is where the error is raised:
https://github.com/PrefectHQ/prefect/blob/35afc328a8273251f70cc4a48874df2c1b9cdb53/src/prefect/core/flow.py#L1555-L1559

Resolving patch:
diff --git a/src/prefect/core/flow.py b/src/prefect/core/flow.py
--- a/src/prefect/core/flow.py
+++ b/src/prefect/core/flow.py
@@ -22,6 +22,7 @@
     Set,
     Tuple,
     cast,
+    Union,
 )
 
 import cloudpickle
@@ -1129,7 +1130,7 @@ def run(
         run_on_schedule: bool = None,
         runner_cls: type = None,
         **kwargs: Any,
-    ) -> "prefect.engine.state.State":
+    ) -> Union["prefect.engine.state.State", None]:
         """
         Run the flow on its schedule using an instance of a FlowRunner.  If the Flow has no
         schedule, a single stateful run will occur (including retries).
@@ -1156,10 +1157,11 @@ def run(
             - State: the state of the flow after its final run
         """
         if prefect.context.get("loading_flow", False):
-            raise RuntimeError(
+            warnings.warn(
                 "Attempting to call `flow.run` during execution of flow file will lead to "
                 "unexpected results."
             )
+            return None
 
         # protect against old behavior
         if "return_tasks" in kwargs:
@@ -1525,7 +1527,7 @@ def register(
         version_group_id: str = None,
         no_url: bool = False,
         **kwargs: Any,
-    ) -> str:
+    ) -> Union[str, None]:
         """
         Register the flow with Prefect Cloud; if no storage is present on the Flow, the default
         value from your config will be used and initialized with `**kwargs`.
@@ -1553,10 +1555,11 @@ def register(
             - str: the ID of the flow that was registered
         """
         if prefect.context.get("loading_flow", False):
-            raise RuntimeError(
+            warnings.warn(
                 "Attempting to call `flow.register` during execution of flow file will lead "
                 "to unexpected results."
             )
+            return None
 
         if self.storage is None:
             self.storage = get_default_storage_class()(**kwargs)


Test patch:
diff --git a/tests/utilities/test_storage.py b/tests/utilities/test_storage.py
--- a/tests/utilities/test_storage.py
+++ b/tests/utilities/test_storage.py
@@ -72,7 +72,7 @@ def test_extract_flow_from_file_raises_on_run_register(tmpdir):
         f.write(contents)
 
     with prefect.context({"loading_flow": True}):
-        with pytest.raises(RuntimeError):
+        with pytest.warns(Warning):
             extract_flow_from_file(file_path=full_path)
 
     contents = """from prefect import Flow\nf=Flow('test-flow')\nf.register()"""
@@ -83,5 +83,5 @@ def test_extract_flow_from_file_raises_on_run_register(tmpdir):
         f.write(contents)
 
     with prefect.context({"loading_flow": True}):
-        with pytest.raises(RuntimeError):
+        with pytest.warns(Warning):
             extract_flow_from_file(file_path=full_path)