Skip to content

Commit

Permalink
Use sys.version_info to guard imports (#1177)
Browse files Browse the repository at this point in the history
**Pull Request Checklist**
- [ ] Fixes #<!--issue number goes here-->
- [ ] Tests added
- [ ] Documentation/examples added
- [X] [Good commit messages](https://cbea.ms/git-commit/) and/or PR
title

**Description of PR**
Currently, conditional imports that depend on Python version are using
`try/except ImportError` to import from `typing_extensions` on older
Python versions. This is not natively supported by typecheckers, forcing
use of error suppression, and losing potential validation of downstream
code. (Additionally, pyright is erroneously flagging a lot of code that
uses `Annotated` with "Call expression not allowed in type expression,
making it harder to spot real issues in VSCode.)

This PR refactors these conditional imports to use the recommended `if
sys.version_info >= (3, minor):` pattern instead.

See also: [mypy issue
comment](python/mypy#1393 (comment)),
[pyright issue
comment](microsoft/pyright#4160 (comment))

Signed-off-by: Alice Purcell <alicederyn@gmail.com>
  • Loading branch information
alicederyn authored Aug 28, 2024
1 parent 14c4acc commit 9e9a772
Show file tree
Hide file tree
Showing 62 changed files with 416 additions and 318 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,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 @@ -25,6 +23,8 @@ This example will reuse the outputs volume across script steps.
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 @@ -8,18 +8,18 @@
=== "Hera"

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

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
12 changes: 6 additions & 6 deletions examples/workflows/scripts/callable_script.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import sys
from typing import List, Union

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
Loading

0 comments on commit 9e9a772

Please sign in to comment.