Skip to content

Commit

Permalink
Use sys.version_info to guard imports
Browse files Browse the repository at this point in the history
Use `if sys.version_info >= (3, minor):` for Python-version-dependent imports. Unlike `try/except ImportError`, this is natively supported by typecheckers, allowing removal of typechecker error suppression and consequently better typechecking validation.

Signed-off-by: Alice Purcell <[email protected]>
  • Loading branch information
alicederyn committed Aug 27, 2024
1 parent 523a9b9 commit 2dc6f22
Show file tree
Hide file tree
Showing 62 changed files with 417 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ This example will reuse the outputs volume across script steps.
=== "Hera"

```python linenums="1"
from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
import sys

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import (
Expand All @@ -28,6 +26,8 @@ This example will reuse the outputs volume across script steps.
models as m,
script,
)
from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

global_config.experimental_features["script_annotations"] = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

```python linenums="1"
import json
import sys
from pathlib import Path
from typing import Dict

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import Artifact, ArtifactLoader, Parameter, Steps, Workflow, script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ This example will reuse the outputs volume across script steps.
=== "Hera"

```python linenums="1"
from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
import sys

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import (
Artifact,
Parameter,
Steps,
Volume,
Workflow,
script,
)
from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

global_config.experimental_features["script_annotations"] = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ This example will reuse the outputs volume across script steps.
=== "Hera"

```python linenums="1"
try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore


import sys
from pathlib import Path

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import (
Artifact,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
=== "Hera"

```python linenums="1"
import sys
from typing import Dict

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import Artifact, ArtifactLoader, Parameter, Steps, Workflow, script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
=== "Hera"

```python linenums="1"
try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore

import sys
from pathlib import Path
from typing import Tuple

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import Artifact, Parameter, RunnerScriptConstructor, Steps, Workflow, script

Expand Down
12 changes: 7 additions & 5 deletions docs/examples/workflows/experimental/script_runner_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
=== "Hera"

```python linenums="1"
import sys

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

try:
from pydantic.v1 import BaseModel
except ImportError:
Expand All @@ -18,11 +25,6 @@
from hera.workflows.archive import NoneArchiveStrategy
from hera.workflows.io import Input, Output

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore

global_config.experimental_features["script_pydantic_io"] = True


Expand Down
12 changes: 6 additions & 6 deletions docs/examples/workflows/scripts/callable_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

```python linenums="1"
from typing import List, Union
import sys

from hera.shared.serialization import serialize

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from pydantic import BaseModel

from hera.shared import global_config
from hera.shared.serialization import serialize
from hera.workflows import Parameter, Script, Steps, Workflow, script

# Note, setting constructor to runner is only possible if the source code is available
Expand Down
12 changes: 6 additions & 6 deletions docs/examples/workflows/scripts/callable_script_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
=== "Hera"

```python linenums="1"
import sys
from typing import List, Union

from hera.shared.serialization import serialize
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
try:
from pydantic.v1 import BaseModel
except (ImportError, ModuleNotFoundError):
from pydantic import BaseModel


from hera.shared import global_config
from hera.shared.serialization import serialize
from hera.workflows import Parameter, RunnerScriptConstructor, Script, Steps, Workflow, script

# Note, setting constructor to runner is only possible if the source code is available
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""This example will reuse the outputs volume across script steps."""

from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
import sys

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import (
Expand All @@ -20,6 +18,8 @@
models as m,
script,
)
from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

global_config.experimental_features["script_annotations"] = True

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import json
import sys
from pathlib import Path
from typing import Dict

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import Artifact, ArtifactLoader, Parameter, Steps, Workflow, script
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""This example will reuse the outputs volume across script steps."""

from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
import sys

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import (
Expand All @@ -17,6 +15,8 @@
Workflow,
script,
)
from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

global_config.experimental_features["script_annotations"] = True

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""This example will reuse the outputs volume across script steps."""

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore


import sys
from pathlib import Path

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import (
Artifact,
Expand Down
9 changes: 5 additions & 4 deletions examples/workflows/experimental/script_annotations_inputs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import sys
from typing import Dict

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import Artifact, ArtifactLoader, Parameter, Steps, Workflow, script
Expand Down
11 changes: 6 additions & 5 deletions examples/workflows/experimental/script_annotations_outputs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore

import sys
from pathlib import Path
from typing import Tuple

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from hera.shared import global_config
from hera.workflows import Artifact, Parameter, RunnerScriptConstructor, Steps, Workflow, script

Expand Down
12 changes: 7 additions & 5 deletions examples/workflows/experimental/script_runner_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import sys

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

try:
from pydantic.v1 import BaseModel
except ImportError:
Expand All @@ -8,11 +15,6 @@
from hera.workflows.archive import NoneArchiveStrategy
from hera.workflows.io import Input, Output

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore

global_config.experimental_features["script_pydantic_io"] = True


Expand Down
11 changes: 6 additions & 5 deletions examples/workflows/scripts/callable_script.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import sys
from typing import List, Union

from hera.shared.serialization import serialize
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore

from pydantic import BaseModel

from hera.shared import global_config
from hera.shared.serialization import serialize
from hera.workflows import Parameter, Script, Steps, Workflow, script

# Note, setting constructor to runner is only possible if the source code is available
Expand Down
Loading

0 comments on commit 2dc6f22

Please sign in to comment.