Skip to content

Conversation

domendobnikar
Copy link
Collaborator

  • Added mypy to CI file
  • Added mypy to MAKE.file, so it can be run locally with make mypy command
  • Added some hints to utils and RestClient

Copy link
Collaborator

@justinc1 justinc1 left a comment

Choose a reason for hiding this comment

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

Thank you.

Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
# Conflicts:
#	plugins/module_utils/role.py
Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
hypercore_dict = rest_client.get_record(
"/rest/v1/Role/{0}".format(role_uuid), must_exist=must_exist
)
role = cls.from_hypercore(hypercore_dict)
return role
if hypercore_dict:
Copy link
Collaborator

Choose a reason for hiding this comment

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

@domendobnikar This is not needed since None case is already handled in the from_hypercore method

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh I forgot to remove the same check from from_hypercore method.
Do we need to raise an exception if Role is None?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It depends on the case, we have "must_exist" parameter that handles this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

All from_hypercore methods have None check, so I would leave it there. Or is there any case where it is better to have it in other methods that call from_hypercore method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Get methods should handle this. Like you said - we already have must_exist implemented there, might as well have the None check, so we have everything in one place.
It makes a small difference between:
from hypercore(cls, hypercore_data: dict[Any, Any]) vs from hypercore(cls, hypercore_data: Union[dict[Any, Any], None])
And the return statement:
-> Union[Role, None] vs -> Role
We just have to pick one and stick to it. I think right now half the classes use it one way and half the other way.
At least the ones that I made. That's why I put it in here, didn't even check or think about it which is my mistake :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes you are right, not all have the None check in the from_hypercore method :) But since the majority have it, I would stick to this then.

"""
One User is equal to another if it has ALL attributes exactly the same.
This method is used only in tests.
"""
if not isinstance(other, Role):
Copy link
Collaborator

@PolonaM PolonaM Feb 3, 2023

Choose a reason for hiding this comment

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

@domendobnikar why is this needed? Isn't enough just to put "other: Role"? Anyway Role class is part of User module, where I need to add the type annotation, so I am also doing this in parallel with you, thus the comments...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is specifically for __eq__ methods. Since __eq__ is expecting an object of any type and by hinting only Role you are trying to override that supertype so MyPy won't let you override it. Right now this works in code because you know you are only checking Role vs Role, but MyPy doesn't know that hence the NotImplemented part.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Aaa ok ok, thanks for the explanation!

@justinc1
Copy link
Collaborator

justinc1 commented Feb 3, 2023

One note @domendobnikar . There are still errors (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/actions/runs/4082190817/jobs/7036274867)

plugins/module_utils/utils.py:33: in <module>
    input: dict[Any, Any], *field_names: str, ansible_hypercore_map: dict[Any, Any]
E   TypeError: 'type' object is not subscriptable

It should be Dict from typing, not dict, for python 3.8.

@domendobnikar
Copy link
Collaborator Author

domendobnikar commented Feb 3, 2023

It should be Dict from typing, not dict, for python 3.8.

Oh ok will fix.

@PolonaM
Copy link
Collaborator

PolonaM commented Feb 3, 2023

One note @domendobnikar . There are still errors (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/actions/runs/4082190817/jobs/7036274867)

plugins/module_utils/utils.py:33: in <module>
    input: dict[Any, Any], *field_names: str, ansible_hypercore_map: dict[Any, Any]
E   TypeError: 'type' object is not subscriptable

It should be Dict from typing, not dict, for python 3.8.

I think here we should decide if we use "from future import annotations" or using typing. I think the recomended way is to use the first one. What do you think?

@domendobnikar
Copy link
Collaborator Author

I think here we should decide if we use "from future import annotations" or using typing. I think the recomended way is to use the first one. What do you think?

We need to use from **future** import annotations to reference to the same class within the class itself; Example would be the Get methods.
Not sure if it fixes this specific error.

@PolonaM
Copy link
Collaborator

PolonaM commented Feb 3, 2023

I think here we should decide if we use "from future import annotations" or using typing. I think the recomended way is to use the first one. What do you think?

We need to use from **future** import annotations to reference to the same class within the class itself; Example would be the Get methods. Not sure if it fixes this specific error.

Also for the tuple vs Tuple, list vs List, dict vs Dict...

@justinc1 justinc1 merged commit 83943ec into main Feb 6, 2023
@justinc1 justinc1 deleted the mypy-ci-test branch February 6, 2023 06:25
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.

3 participants