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

2-to-3: Start with types #1232

Merged
merged 92 commits into from Mar 3, 2020
Merged

2-to-3: Start with types #1232

merged 92 commits into from Mar 3, 2020

Conversation

grahamc
Copy link
Member

@grahamc grahamc commented Feb 28, 2020

A rethinking of #1227 which starts by adding types where there are problems. Adds types and corrects issues where mypy was unhappy, after asking it to check untyped defs.

(Don't merge.)

Comment on lines +173 to +187
def _definition_for_required(
self, name: str
) -> nixops.resources.ResourceDefinition:
defn = self._definition_for(name)
if defn is None:
raise Exception("Bug: Deployment.definitions['{}'] is None.".format(name))
return defn

def _machine_definition_for_required(
self, name: str
) -> nixops.backends.MachineDefinition:
defn = self._definition_for_required(name)
if not isinstance(defn, nixops.backends.MachineDefinition):
raise Exception("Definition named '{}' is not a machine.".format(name))
return defn
Copy link
Member Author

Choose a reason for hiding this comment

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

Under the old behavior:

  1. self.definitions was None, causing self.definititions[name] to be an Exception -- no change
  2. self.definitions was a Dict, causing self.definititions[name] to be possibly None -- generally not causing an Exception, but instead propogating a None around
  3. self.definitions was a Dict, self.definititions[name] was something, but not a Machine -- instead a different kind of resource. I believe this would cause an exception.

In other words, the only case I'm somewhat unsure about is the None case.

@grahamc
Copy link
Member Author

grahamc commented Feb 28, 2020

Also: there are commits from the other PR that I'm going to bring over here.

@grahamc
Copy link
Member Author

grahamc commented Feb 29, 2020

Now improved to:

+--------------------------------+-------------------+----------+
| Module                         | Imprecision       | Lines    |
+--------------------------------+-------------------+----------+
| nixops                         |   0.00% imprecise |    0 LOC |
| nixops.ansi                    |   0.00% imprecise |   18 LOC |
| nixops.backends                |  55.25% imprecise |  514 LOC |
| nixops.backends.none           |  59.83% imprecise |  117 LOC |
| nixops.deployment              |  49.53% imprecise | 1712 LOC |
| nixops.diff                    |  18.27% imprecise |  208 LOC |
| nixops.known_hosts             |  11.43% imprecise |   70 LOC |
| nixops.logger                  |  60.90% imprecise |  156 LOC |
| nixops.nix_expr                |  57.88% imprecise |  349 LOC |
| nixops.parallel                |  28.45% imprecise |  116 LOC |
| nixops.plugins                 |  58.33% imprecise |   12 LOC |
| nixops.plugins.hookspecs       |  32.00% imprecise |   25 LOC |
| nixops.resources               |  49.15% imprecise |  293 LOC |
| nixops.resources.commandOutput |  19.47% imprecise |  113 LOC |
| nixops.resources.ssh_keypair   |  48.33% imprecise |   60 LOC |
| nixops.script_defs             |  62.32% imprecise | 1128 LOC |
| nixops.ssh_util                |  46.33% imprecise |  341 LOC |
| nixops.state                   |  22.37% imprecise |   76 LOC |
| nixops.statefile               |  47.35% imprecise |  264 LOC |
| nixops.util                    |  23.87% imprecise |  507 LOC |
+--------------------------------+-------------------+----------+
| Total                          |  47.87% imprecise | 6083 LOC |
+--------------------------------+-------------------+----------+

In particular, known_hosts 51.39% -> 11.43%, state 60% -> 22%!

@aanderse
Copy link
Member

aanderse commented Mar 2, 2020

I found no issues with my usual workflow on this branch, using the none backend 👍

@talyz
Copy link
Contributor

talyz commented Mar 2, 2020

I didn't run into any issues when running without plugins, trying out the following commands: create, deploy, info, check, ssh, list, delete, export, modify and list-plugins.

doshitan and others added 3 commits March 2, 2020 12:43
An alternative implementation of #1238 from GitHub user doshitan which
used Literal[...] as a sort of dependent type:

    +    @overload
    +    def run_command(
    +        self,
    +        command: Command,
    +        flags: List[str] = ...,
    +        timeout: Optional[int] = ...,
    +        logged: bool = ...,
    +        allow_ssh_args: bool = ...,
    +        user: Optional[str] = ...,
    +        *,
    +        capture_stdout: Literal[False],
    +        **kwargs: Any
    +    ) -> int:
    +        ...
    +
    +    @overload
    +    def run_command(
    +        self,
    +        command: Command,
    +        flags: List[str] = ...,
    +        timeout: Optional[int] = ...,
    +        logged: bool = ...,
    +        allow_ssh_args: bool = ...,
    +        user: Optional[str] = ...,
    +        *,
    +        capture_stdout: Literal[True],
    +        **kwargs: Any
    +    ) -> str:
    +        ...
    +
    +    @overload
    +    def run_command(
    +        self,
    +        command: Command,
    +        flags: List[str] = [],
    +        timeout: Optional[int] = None,
    +        logged: bool = True,
    +        allow_ssh_args: bool = False,
    +        user: Optional[str] = None,
    +        **kwargs: Any
    +    ) -> Union[str, int]:
    +        ...

however, I think this option is much simpler and improves the API.
Possibly in the future we will add this more complex type.
@grahamc grahamc merged commit 8237088 into master Mar 3, 2020
@worldofpeace
Copy link

💕 💕 🌈

@grahamc grahamc added this to the 2.0 milestone Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants