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

Add Nox session that uses autotyping to automatically add type hint annotations #2696

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

namurphy
Copy link
Member

@namurphy namurphy commented May 17, 2024

This PR adds the autotyping session to noxfile.py ✨, along with some mypy troubleshooting information that gets printed out when mypy runs.

This session provides two autotyping options so far:

  • safe: which will add type hints that have a very high probability of being correct
  • aggressive: which will add type hints that will usually be correct but sometimes may not be. Since using this option will necessitate a careful code review, it should never be used in CI.

📌 In the not-too-distant future, I'd like to either add this to our CI, or provide better instructions for contributors on how to use this. Alternatively, we could run this with the safe option as a pre-commit hook, which would let contributors add type hints to PRs with pre-commit.ci autofix comments. I'm hoping this will make adding type hints easier.

🌠 We could also add rules where if a variable is named B, then a type hint of u.Quantity[u.B] gets automatically added, but I'll save that for a standalone PR.

📝 A limitation of autotyping is that it will only add simple type hints rather than more complicated ones. We could potentially also add a Nox session for other tools like monkeytype which will run pytest and then apply type hints based on the type hints found at runtime. We wouldn't be able to include that in CI because changes from those tools will require careful code review.

@github-actions github-actions bot added testing CI Related to continuous integration maintenance General updates to package infrastructure python Pull requests that update Python code nox Related to the nox automation software labels May 17, 2024
@github-actions github-actions bot added plasmapy.particles Related to the plasmapy.particles subpackage plasmapy.plasma Related to the plasmapy.plasma subpackage plasmapy.formulary Related to the plasmapy.formulary subpackage plasmapy.diagnostics Related to the plasmapy.diagnostics subpackage plasmapy.utils Related to the plasmapy.utils subpackage GitHub Actions A continuous integration platform for automating tests and other tasks (see .github/ directory) plasmapy.simulation Related to the plasmapy.simulation subpackage tools Tools & scripts used in PlasmaPy development but are not importable from within PlasmaPy labels May 17, 2024
@namurphy namurphy marked this pull request as ready for review May 17, 2024 19:46
@namurphy namurphy requested a review from a team as a code owner May 17, 2024 19:46
@namurphy namurphy requested review from pheuer and removed request for a team May 17, 2024 19:46
Copy link

codecov bot commented May 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.14%. Comparing base (c39ccee) to head (991f441).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2696   +/-   ##
=======================================
  Coverage   95.14%   95.14%           
=======================================
  Files         107      107           
  Lines        9411     9411           
  Branches     2168     2168           
=======================================
  Hits         8954     8954           
  Misses        276      276           
  Partials      181      181           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -203,10 +203,25 @@ def linkcheck(session: nox.Session):
session.run(*sphinx_commands, *check_hyperlinks, *session.posargs)


MYPY_TROUBLESHOOTING = """
🦺 To learn more about type hints, check out mypy's cheat sheet at:
Copy link
Member Author

Choose a reason for hiding this comment

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

Fortunately this particular emoji shows up nicely in the output of GitHub Actions! I expect that it'll be useful to draw people's attention to the troubleshooting information.

@namurphy namurphy requested a review from ejohnson-96 June 3, 2024 17:51
@@ -542,9 +542,6 @@ def create_tracker_obj(**kwargs) -> cpr.Tracker:
return sim


tracker_obj_simulated = create_tracker_obj(field_weighting="nearest neighbor").run()
Copy link
Member Author

Choose a reason for hiding this comment

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

After the changes from autotyping, mypy found that the run method here was returning None, which ended up being the value passed to tracker_obj_simulated. This was one example of how static type checking can be helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Related to continuous integration GitHub Actions A continuous integration platform for automating tests and other tasks (see .github/ directory) maintenance General updates to package infrastructure nox Related to the nox automation software plasmapy.diagnostics Related to the plasmapy.diagnostics subpackage plasmapy.formulary Related to the plasmapy.formulary subpackage plasmapy.particles Related to the plasmapy.particles subpackage plasmapy.plasma Related to the plasmapy.plasma subpackage plasmapy.simulation Related to the plasmapy.simulation subpackage plasmapy.utils Related to the plasmapy.utils subpackage python Pull requests that update Python code static type checking testing tools Tools & scripts used in PlasmaPy development but are not importable from within PlasmaPy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant