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

Clean documentation and add type hints #228

Merged
merged 6 commits into from
Jan 10, 2025
Merged

Clean documentation and add type hints #228

merged 6 commits into from
Jan 10, 2025

Conversation

Daval-G
Copy link
Collaborator

@Daval-G Daval-G commented Dec 18, 2024

Just cleaning the documentation a bit and adding type hints

@Daval-G Daval-G added the documentation Improvements or additions to documentation label Dec 18, 2024
@Daval-G Daval-G requested a review from paquiteau December 18, 2024 20:08
@Daval-G Daval-G self-assigned this Dec 18, 2024
@paquiteau
Copy link
Member

paquiteau commented Dec 18, 2024

I am not against typing, however we should do it properly have you considered using mypy and /or ruff (the ANN* errors are disabled for now).

The trajectory module is a great starting point for its usage, and would likely complement the docstring to help people play with them.

A few comments:

  • try to avoid Any
  • str type are most of the time limited to named values , you can use Literal instead
  • *args and **kwargs should be typed with the type of the elements, not list
  • typing np array is not as good as it could be the best future prood solution is to use np.typing.NDArray

@Daval-G
Copy link
Collaborator Author

Daval-G commented Dec 19, 2024

Good point. I followed ruff's ANN rule, and the current code is compliant (except for bad usage of Literal apparently)

try to avoid Any

Done, but some cases like open kwargs are still relevant. I tried to reduce them to a minimum though.

str type are most of the time limited to named values , you can use Literal instead

Make sense. The CI tells me I underestimated Literal, I will fix that soon.

*args and **kwargs should be typed with the type of the elements, not list

Make sense.

typing np array is not as good as it could be the best future prood solution is to use np.typing.NDArray

Done, but I have to check also how it renders in docstrings

Copy link
Member

@paquiteau paquiteau left a comment

Choose a reason for hiding this comment

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

We are on a good path, thanks for the tedious work.

Do not hesitate to check your modification with mypy locally. I am also pretty sure an LLM could help you adding annotation in bulk if needed...

"""Restore the display configuration."""
for key, value in self._old_values.items():
setattr(displayConfig, key, value)
delattr(self, "_old_values")

def __enter__(self):
def __enter__(self) -> "displayConfig":
Copy link
Member

Choose a reason for hiding this comment

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

if you use from __future__ import annotations you should be able to annotate with the Class directly, not a string :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Delayed annotation checks helped in several cases actually, thx !

constraints_order=None,
**constraints_kwargs,
):
trajectory: np.typing.NDArray,
Copy link
Member

Choose a reason for hiding this comment

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

I think you can just from numpy.typing import NDArray and use it everywhere

Copy link
Collaborator Author

@Daval-G Daval-G Dec 25, 2024

Choose a reason for hiding this comment

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

Done, much better indeed

*,
diagonals: bool = True,
pseudo_random: bool = True,
**sampling_kwargs: Literal | bool,
Copy link
Member

Choose a reason for hiding this comment

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

Literal has to be used as Literal["A string"] or Literal[1] for instance

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Most Literal uses were actually unjustified because of the way we handle Enum for everything

diagonals: bool = True,
pseudo_random: bool = True,
**sampling_kwargs: Literal | bool,
) -> np.ndarray:
Copy link
Member

Choose a reason for hiding this comment

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

should be NDArray

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes I missed a lot of them for some reason, thx

@Daval-G
Copy link
Collaborator Author

Daval-G commented Dec 25, 2024

mypy is still showing some issues but I reduced them from 120 to 25, with half of them outside the trajectory submodule for some reason. The current code is compliant with ruff's ANN rules

Comment on lines 102 to 104
first_cluster_by: Literal["x", "y", "z", "r", "phi", "theta"] | None = None,
second_cluster_by: Literal["x", "y", "z", "r", "phi", "theta"] | None = None,
sort_by: Literal["x", "y", "z", "r", "phi", "theta"] | None = None,
Copy link
Member

Choose a reason for hiding this comment

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

I think you can:

  • Use a type alias (declared below the imports), for brievety
  • Put the None inside the literal

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • Done for the type alias, thx
  • There is one function where None is not an acceptable option so I kept it out

@Daval-G Daval-G requested a review from paquiteau December 27, 2024 16:25
@paquiteau paquiteau merged commit 3abd8d9 into master Jan 10, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants