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

script_defs: Remove None assertion #1244

Merged
merged 5 commits into from Mar 10, 2020

Conversation

adisbladis
Copy link
Member

r being None is peferctly valid when the resource does not yet exist.

Without this fix a resource not yet deployed fails with AssertionError.

Copy link
Member

@grahamc grahamc left a comment

Choose a reason for hiding this comment

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

I was going to write some feedback about being more explicit, like adding types to r and d, but mypy is smart enough already!

[nix-shell:~/projects/github.com/NixOS/nixops]$ git diff
diff --git a/nixops/script_defs.py b/nixops/script_defs.py
index b0c283a..adb5e81 100644
--- a/nixops/script_defs.py
+++ b/nixops/script_defs.py
@@ -234,8 +234,8 @@ def op_info(args):
         )

         for name in names:
-            d = definitions.get(name)
-            r = depl.resources.get(name)
+            d: str = definitions.get(name)
+            r: str = depl.resources.get(name)
             assert r is not None
             if deployment.is_machine(r):
                 resource_state = "{0} / {1}".format(

[nix-shell:~/projects/github.com/NixOS/nixops]$ mypy nixops
nixops/script_defs.py:237: error: Incompatible types in assignment (expression has type "Optional[ResourceDefinition]", variable has type "str")
nixops/script_defs.py:238: error: Incompatible types in assignment (expression has type "Optional[ResourceState]", variable has type "str")

I was looking at the mypy html report, and if you'd like to help "clean up the campground" a bit, there is a trivial additional thing you could do:

image

adding -> str to resources/init.py's show_type()

and also adding types to def state(depl, d, m): in script_defs.py.

`r` being `None` is peferctly valid when the resource does not yet exist.

Without this fix a resource not yet deployed fails with AssertionError.
@adisbladis
Copy link
Member Author

@grahamc Updated PR with more typing.

nixops/script_defs.py Outdated Show resolved Hide resolved
@grahamc
Copy link
Member

grahamc commented Mar 10, 2020

resources: 45.58% imprecise ->41.50% imprecise
script_defs: 58.85% imprecise -> 57.95% imprecise

@grahamc grahamc merged commit b4c38df into NixOS:master Mar 10, 2020
@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

2 participants