Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Problematic Type Union #694

Merged

Conversation

MattToast
Copy link
Member

In the shell launcher, an unintended and unused type union sunk into the ShellLauncherCommand type for the command_tuple type, allowing the attr to be of type Sequence[str] or tuple[str, tuple[str, ...]]. The former works as expected, but the latter would error at runtime when sent to the ShellLauncher when opening a subprocess.

class ShellLauncher:
    ...
    def start(self, shell_command: ShellLauncherCommand) -> LaunchedJobID:
        ...
        exe, *rest = shell_command.command_tuple
        #     ^^^^ Mypy thinks this is `list[str] | list[tuple[str]]`
        expanded_exe = helpers.expand_exe_path(exe)
        # pylint: disable-next=consider-using-with
        self._launched[id_] = sp.Popen(
            (expanded_exe, *rest),
       #    ^^^^^^^^^^^^^^^^^^^^^
       #    And inadvertently casts this to `tuple[Any]` which errors
       #    at runtime
            cwd=shell_command.path,
            env={k: v for k, v in shell_command.env.items() if v is not None},
            stdout=shell_command.stdout,
            stderr=shell_command.stderr,
        )
        ...

Because this type was not being used, it can simply be removed from the union.

In the shell launcher, an unintended and unused type union sunk into the
``ShellLauncherCommand`` type for the ``command_tuple`` type, allowing
the attr to be of type ``Sequence[str]`` or ``tuple[str, tuple[str,
...]]``. The former works as expected, but the latter would error at
runtime when sent to the ``ShellLauncher`` when opening a subprocess.

```py
class ShellLauncher:
    ...
    def start(self, shell_command: ShellLauncherCommand) -> LaunchedJobID:
        ...
        exe, *rest = shell_command.command_tuple
        #     ^^^^ Mypy thinks this is `list[str] | list[tuple[str]]`
        expanded_exe = helpers.expand_exe_path(exe)
        # pylint: disable-next=consider-using-with
        self._launched[id_] = sp.Popen(
            (expanded_exe, *rest),
       #    ^^^^^^^^^^^^^^^^^^^^^
       #    And inadvertently casts this to `tuple[Any]` which errors
       #    at runtime
            cwd=shell_command.path,
            env={k: v for k, v in shell_command.env.items() if v is not None},
            stdout=shell_command.stdout,
            stderr=shell_command.stderr,
        )
        return id_
```

Because this type was not being used, it can simply be removed from the
union.
@MattToast MattToast self-assigned this Sep 5, 2024
Copy link

codecov bot commented Sep 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 43.95%. Comparing base (cce16e6) to head (1b57749).
Report is 19 commits behind head on smartsim-refactor.

Additional details and impacted files

Impacted file tree graph

@@                  Coverage Diff                  @@
##           smartsim-refactor     #694      +/-   ##
=====================================================
+ Coverage              40.45%   43.95%   +3.49%     
=====================================================
  Files                    110      110              
  Lines                   7326     7134     -192     
=====================================================
+ Hits                    2964     3136     +172     
+ Misses                  4362     3998     -364     
Files with missing lines Coverage Δ
smartsim/_core/shell/shellLauncher.py 95.06% <100.00%> (+34.19%) ⬆️

... and 6 files with indirect coverage changes

Copy link
Contributor

@amandarichardsonn amandarichardsonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -57,7 +57,7 @@ class ShellLauncherCommand(t.NamedTuple):
path: pathlib.Path
stdout: io.TextIOWrapper | int
stderr: io.TextIOWrapper | int
command_tuple: tuple[str, tuple[str, ...]] | t.Sequence[str]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for catching that.

@MattToast MattToast merged commit c169878 into CrayLabs:smartsim-refactor Sep 5, 2024
34 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants