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

Refactor the code to make it compliant with Python 3 typing #2404

Closed
nicolargo opened this issue May 18, 2023 · 5 comments
Closed

Refactor the code to make it compliant with Python 3 typing #2404

nicolargo opened this issue May 18, 2023 · 5 comments

Comments

@nicolargo
Copy link
Owner

Glances has been developed for Python 2. The next version (4.0.0) will only be compliant with Python 3.8 or higher.

It's time to refactor the code to provides runtime support for type hints: https://docs.python.org/3/library/typing.html

@RazCrimson any experiences/tips to convert a Python code for Python 3 typing ? Do you think that we must include an external type checker (like mypi or others) ?

Some resources found on the net:

@nicolargo
Copy link
Owner Author

@RazCrimson
Copy link
Collaborator

@nicolargo

@RazCrimson any experiences/tips to convert a Python code for Python 3 typing ? Do you think that we must include an external type checker (like mypi or others)

Yes, mypy would be nice to add it

For glances but since most of the our variables are dicts, typing them directly would not be really useful.

We should start typing after we define the models (pydantic issue mentioned above), otherwise we would have the change the types of all those dicts into their corresponding pydantic equivalents later which would be extra work.

@nicolargo
Copy link
Owner Author

Hi @RazCrimson on the https://github.com/nicolargo/glances/tree/2404-refactor-the-code-to-make-it-compliant-with-python-3-typing branch, i start the refactoring by creating two news dataclasses: GlancesDataItem and GlancesDataPlugin.

Those classes are for the moment only used in the CPU and MEM plugins and simplify the plugins code.

Can you have a first look and tell me if you see others thinks to improve and will simplify the API documentation.

For the moment, typing is limited to the new dataclasses and there is no verification (non pydantic for the moment).

@RazCrimson
Copy link
Collaborator

@nicolargo

Sure, will take a look at them over the weekend!

@nicolargo
Copy link
Owner Author

nicolargo commented Jun 10, 2023

Additional question: How to deal with plugin returning list of dict ?

In this case, how to define the plugin with fields_description ?

The data model (after export) could:

  • be the same than Glances version 3: a list of dict <== better choice, i think
  • change: a dict of dict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants