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

More type annotations #38

Merged

Conversation

phlogistonjohn
Copy link
Collaborator

To improve the type annotations usage in sambacc and hopefully catch more errors early the first step is to enable the option "disallow_incomplete_defs" in mypy. This disallows mixing typed and untyped parameters and return types on a single function call. It does not affect a function that has no annotations at all.

The first set of commits adds annotations to many parts of the code base. Then we enable the option in mypy to enforce it in the future (via pyproject.toml). Finally, we update a few straggling uses of typing.List and typing.Dict to the 'list' and 'dict' equivalents since our minimum version is python3.9.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
These types are "consumed" by the lib but annoying to remember
and type (with a keyboard) over and over again.


Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Generally, this would come from argparse but I don't want to
pull in argparse all over the place. Rather, use a new Protocol
type with just the methods we use in our CLI construction
functions.


Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
I suppose mypy sees that block outside of the context manager
as a path with no return statement and throws an error. I don't
think that is quite right, but I am probably wrong and it isn't
a huge change needed to quiet the error.


Signed-off-by: John Mulligan <jmulligan@redhat.com>
Setting disallow_incomplete_defs means that if we have an type
annotation on one arg or the return type, all args and the return
type must be annotated.
I think this is a good default we can apply across all modules
in the sambacc codebase.


Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
@phlogistonjohn phlogistonjohn marked this pull request as ready for review May 11, 2022 19:06
Copy link
Collaborator

@synarete synarete left a comment

Choose a reason for hiding this comment

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

A general comment: PEP484, in The meaning of annotations requires __init__ to have -> None type hinting. Not all cases follow this rule.

@@ -298,7 +312,9 @@ def _node_check(pnn: int, nodes_json: str, real_path: str) -> bool:
return True


def _node_update_check(json_data, nodes_json: str, real_path: str):
def _node_update_check(
json_data: dict[str, typing.Any], nodes_json: str, real_path: str
Copy link
Collaborator

Choose a reason for hiding this comment

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

nodes_json is not used in this function. With type hinting, functions signatures become longer; maybe it would be better to avoid used arguments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Function is private so it should be straightforward.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I started to try and remove this but it cascades through other layers. I am going to leave it for now with consistency with other function signatures. Next time I may need to make better use of keyword-only arguments for something like this.

@phlogistonjohn
Copy link
Collaborator Author

A general comment: PEP484, in The meaning of annotations requires __init__ to have -> None type hinting. Not all cases follow this rule.

Right. This was (is?) tricky for me when I started using annotations. My plan is to enable annotations required for sambacc but not sambacc.commands (or tests). This PR adds the first set of configuration params for mypy to the toml file. A future PR will add per-directory configs.

@phlogistonjohn
Copy link
Collaborator Author

Rebased.

@phlogistonjohn
Copy link
Collaborator Author

@synarete @spuiuk PTAL. Since this PR has been open for an extended period of time already, I'm asking for reviews within 1 week. I'll just merge it if no reviews on the current version by that time.

Copy link
Collaborator

@synarete synarete left a comment

Choose a reason for hiding this comment

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

LGTM

@phlogistonjohn
Copy link
Collaborator Author

Thanks!!

@phlogistonjohn phlogistonjohn merged commit aa9da22 into samba-in-kubernetes:master Jun 16, 2022
@phlogistonjohn phlogistonjohn deleted the jjm-better-typing branch November 8, 2022 18:22
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.

2 participants