E52C Add basic mypy configuration by eerovaher · Pull Request #19044 · astropy/astropy · GitHub
[go: up one dir, main page]

Skip to content

Add basic mypy configuration#19044

Open
eerovaher wants to merge 2 commits intoastropy:mainfrom
eerovaher:mypy-in-ci
Open

Add basic mypy configuration#19044
eerovaher wants to merge 2 commits intoastropy:mainfrom
eerovaher:mypy-in-ci

Conversation

@eerovaher
Copy link
Member

Description

In the December developer telecon speeding up parts of astropy by compiling them with mypyc was discussed (see also #19011). The conclusion was that right now providing wheels with mypyc-compiled code is not worthwhile, one of the main reasons being that mypyc will only compile code that mypy approves and the fraction of code with type annotations is still very low. However, mypyc compilation might eventually become worthwhile if more code gets annotated, so there should be a CI job that prevents typing errors from being introduced to modules mypy already approves.

There have been several attempts to configure type checkers for astropy (#12971, #15794, #15815, #16312, #17918). Those PRs attempted to nominally turn on type checking for all or large fractions of astropy, but that necessitates listing a large number of modules and rules to be ignored. This PR takes the opposite approach where mypy only checks modules it is explicitly told to check, but none of the default rules are ignored.

During local development I recommend running mypy directly instead of running it through tox, but as far as I know the tox environment is required for CI.

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

Configuring a default list of files for Mypy to check helps ensure that
modules without typing errors stay without typing errors. Configuring
`follow_imports = "silent"` ensures that Mypy does not report errors
from modules it is not instructed to check. In the long term type
checking with Mypy can allow compiling (parts of) `astropy` with Mypyc.
The new CI job helps ensure that typing errors are not introduced to
modules Mypy already approves. Mypy approval is a requirement for
compiling anything with Mypyc.
@github-actions
Copy link
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

Copy link
Member
@nstarman nstarman left a comment

Choose a reason for hiding this comment

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

I think this a great first step! 🚀

Copy link
Contributor
@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

This looks good!

One question about the best way to approach this: is there any way to run mypy only when one of the files it guards is actually changed? I recall we discussed adding it to pre-commit earlier - would that ensure it? (Note that unless there is a clear good answer to this, I'm fine with going ahead and merge -- we can always revisit how things are tested later.)

@eerovaher
Copy link
Member Author

Let's take as a starting point f40ba98 (which is where #19011 is at right now). Mypy reports

$ mypy --follow-imports silent -- astropy/units/utils.py 
Success: no issues found in 1 source file

Suppose for the sake of example that we now edit astropy/units/typing.py

@@ -77,7 +77,7 @@ For more examples see the :mod:`numpy.typing` definition of
 """
 
 
-UnitPower: TypeAlias = int | float | Fraction
+UnitPower: TypeAlias = int | float | Fraction | str
 """Alias for types that can be powers of the components of a
 `~astropy.units.UnitBase` instance"""
 UnitPowerLike: TypeAlias = UnitPower | np.integer | np.floating

After that single change mypy reports

$ mypy --follow-imports silent -- astropy/units/utils.py 
astropy/units/utils.py:76: error: Unsupported operand types for / ("int" and "str")  [operator]
astropy/units/utils.py:76: note: Right operand is of type "int | float | str | Any | floating[Any]"
astropy/units/utils.py:108: error: Item "str" of "int | Fraction | str" has no attribute "denominator"  [union-attr]
astropy/units/utils.py:111: error: Item "str" of "int | Fraction | str" has no attribute "numerator"  [union-attr]
Found 3 errors in 1 file (checked 1 source file)

You can see that changes in one file can cause typing errors in another, so it is not helpful to only run mypy in CI if a module it guards has been changed.

Copy link
Contributor
@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Thanks for that example of why a guard-by-file doesn't work. With that, approving, though I'd like someone more versed in CI/tox to review before merging.

@pllim
Copy link
Member
pllim commented Dec 11, 2025

The log https://github.com/astropy/astropy/actions/runs/20110655410/job/57706767249?pr=19044 seems straight forward. But now people who don't care about typing won't touch that file because the CI would fail unless everything in it in typed?

@mhvk
Copy link
Contributor
mhvk commented Dec 11, 2025

But now people who don't care about typing won't touch that file because the CI would fail unless everything in it in typed?

That seems a fair worry longer term, though my sense is that those interested in typing are willing to help out. Maybe a failure of this run can lead to an (automatic?) ping to a new "astropy-typing" group? Though I'm personally happy to just wait and see how it goes. If really worried, maybe have the CI run not be required for merging for now?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0