8000 APE 25ish: An initial roadmap for static type checking by namurphy · Pull Request #94 · astropy/astropy-APEs · GitHub
[go: up one dir, main page]

Skip to content

APE 25ish: An initial roadmap for static type checking#94

Open
namurphy wants to merge 24 commits intoastropy:mainfrom
namurphy:ape-25-static-type-checking
Open

APE 25ish: An initial roadmap for static type checking#94
namurphy wants to merge 24 commits intoastropy:mainfrom
namurphy:ape-25-static-type-checking

Conversation

@namurphy
Copy link
Contributor

This PR will add an APE that proposes a path forward for enabling static type checking in Astropy's continuous integration suite. If anyone is interested in being a co-author, please let me know!

The process would be more or less to:

  • Enable mypy in CI, but only check a single file at first
  • Gradually enable static type checking in one or two subpackages (astropy.cosmology and astropy.units?)
  • Add a section to the developer docs on type hint annotations and static type checking
  • Allow subpackage maintainers to gradually enable static type checking in more subpackages

It's currently in an early draft state, and might be best though of as being in the brainstorming stage 🧠 🌩️. Big picture feedback would be especially appreciated! In particular, we will need to address the really valid concerns and considerations that have been brought up in astropy/astropy#15170.

I'm also hoping to avoid being over-prescriptive, so as to give us flexibility and avoid the need to amend this APE when ruff implements a full static type checker. 🙃

Closes #92.

Copy link
@neutrinoceros neutrinoceros 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 for starting this ! I feel that two important points may be missing at the moment:

  • we ought to acknowledge and emphasis the expected gains (and costs ?) of having type-hints, and of static checking, from a downstream developers/maintainers perspective
  • some tools (including mypy, last time I checked) will not attempt to discover type hints in a package unless it contains a py.typed marker file. This means that as long as we don't include it anywhere, our type annotations, while not statically checked, are also invisible from the perspective of someone checking downstream code. I think we'll have to include it to make our effort visible and useful, and that running a type checker ourselves is a necessary step that needs to happen before it. Meanwhile, as long as we don't include it, we can treat type hints as private details, which I assume will help with early implementation.

APE25.rst Outdated
Comment on lines +77 to +80
1. Requiring type hint annotations that mypy doesn't complain about
adds an extra step to contributing to Astropy...

1. An additional step is required to contribute to Astropy.

Choose a reason for hiding this comment

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

aren't those the same thing ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! This is still an early draft so I was trying out different wordings and ways to organize the doc.


1. An additional step is required to contribute to Astropy.

1. Type hint annotations may add a barrier to entry for contributing

Choose a reason for hiding this comment

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

This also feels similar to the first two points, but I guess maybe you mean to say that existing type hints may make the code harder to read from a newcomer's perspective ? This is something I've heard in similar contexts, coming from seasoned devs, and I'm honestly not convinced it's a valid point, but it's nonetheless an opinion that exists.

Comment on lines +68 to +70
#. Helps introspection and tab completion in Jupyter notebooks
#. Helps IDEs to make things easier for humans
#. Helpful for downstream package maintainers
Copy link
Member 10BC0

Choose a reason for hiding this comment

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

These are benefits of adding type annotations, and I enthusiastically agree with these benefits. Static type checking is not required to see these benefits for humans.

Comment on lines +66 to +67
#. Can find programming errors that would be difficult to find
otherwise
Copy link
Member

Choose a reason for hiding this comment

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

It would be useful to know what level of mypy checking is required to realize this benefit, along with an example of a module that is typed to that level.

One of my initial bad experiences with enabling type checking was using VS Code pylance/pyright on a small module at the "basic" setting. After several hours trying and failing to get to zero errors I gave up. Maybe mypy is better and the technology has advanced.

APE25.rst Outdated

1. **Do not enable static type checking of Astropy.** Type hint
annotations have begun to be added to Astropy. At present, there is
no way to check the validity or accuracy of type hint annotations.
Copy link
Member

Choose a reason for hiding this comment

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

Some of the comments and type hints in astropy/astropy#15914 seem to highlight that checking validity of type annotations is sometimes not possible or useful. I'm thinking of object or UnitLike or PhysicalTypeLike annotations. Basically we have some protocol that is documented in a long paragraph and implemented in code, but is difficult/impossible to express as a type annotation.

This is certainly common in my subpackages table, time, io.ascii. To be sure there are many places where the types are reasonably narrow, but often they are not.

@namurphy namurphy changed the title APE 25: An initial roadmap for static type checking APE 25ish: An initial roadmap for static type checking Feb 23, 2024
@namurphy
Copy link
Contributor Author
namurphy commented Mar 1, 2024

It's going to be a while before I can get back to this PR, so if anyone else wants to work on this, please let me know!

My main goal here has been to encapsulate the advice given in the mypy docs on enabling static type checking on an existing code base.

@kbwestfall
Copy link
Contributor

With the acceptance of the revisions to APE 1, we're going to start following the new APE proposal process. While all of the APE editors (for now exactly the same as the CoCo) will contribute to the process, one of the APE editors will be the point-person (chief APE editor) shepherding your team through the process.

Your chief APE editor is Clara Brasseur (@ceb8).

You can review the next steps in the APE process here, but just to briefly summarize them here:

  • Please finalize the draft of your APE and then ping @astropy/ape-editor-team. This will prompt the APE Editorial team to read the proposed APE and provide any basic editing comments (led by your chief editor).
  • Following APE 1, this editing process will take no more than a month, but we actually expect this to go much faster for this APE.
  • Once the basic editing is finished, APE 1 provides a detailed list of the next steps. The main thing to be aware of is that the APE status will be set to Discussion, the PR will be merged, and the APE editor will send an e-mail to astropy-dev to start the discussion period. All discussion should happen on the astropy-dev list.
  • Throughout the discussion period, it is up to the APE authors to track the discussion and make updates to the APE. These changes can be committed in a series of new PRs or kept in a branch that only leads to a single PR after the discussion has reached consensus. Please keep your APE editor contact updated on your preferred approach.
  • There is no set timeframe for the discussion period, but it should be at least 2 weeks.
  • After this minimum discussion period but otherwise at their discretion, the APE authors must then notify the CoCo that the APE proposal is ready for a final decision.
  • There is no set period for the CoCo review, but we expect it will take no more than 2-3 weeks.
  • The CoCo will decide to accept, reject, or request more discussion on specific items in the proposal, which will restart the process from the start of the discussion period.

Please let us (either your chief APE editor or the full CoCo) know if you have any questions!

@namurphy
Copy link
Contributor Author

Thank you for the update, Kyle! It's still likely that I won't be able to work on this for a while, so I plan to close this pull request unless anyone else wants to take the lead on this. It's also possible that this may be obsolete at this point, since I haven't been following the discussions about this that closely.

@namurphy
Copy link
Contributor Author

An additional note on this topic — for PlasmaPy, I'm hoping to switch us over to using ty for static type checking once it becomes a bit more mature, both because of performance and also some capabilities that will likely make it more contributor-friendly than mypy.

@kbwestfall
Copy link
Contributor

Thank you for the update, Kyle! It's still likely that I won't be able to work on this for a while, so I plan to close this pull request unless anyone else wants to take the lead on this. It's also possible that this may be obsolete at this point, since I haven't been following the discussions about this that closely.

No worries. Thanks, Nick! I'll leave it to you to decide when/if to close the PR.

@neutrinoceros
Copy link

With the new editorial process I think merging this APE (even as rejected or undecided) would be useful; this topic has been controversial and the effort needed to move it forward is certainly big enough to warrant fundings. So, anytime this is brought up again in any serious capacity, having this APE/discussion as a precedent will be relevant.

@taldcroft
Copy link
Member

With the new editorial process I think merging this APE (even as rejected or undecided) would be useful;

I somewhat disagree, based in particular on the current language in APE 1:

Note that there is not much point to submitting new APEs unless someone or some
group has signed up to implement it should the APE be accepted
(typically this would involve the author or authors of the APE). Just issuing
an APE in order to spur others to do work is not going to be received
well, due to the open-source nature of the Astropy Project.

IMO, merging the APE constitutes the action of actively seeking approval with the intent (and resources) to implement the APE.

@namurphy
Copy link
Contributor Author

Thinking more about this, it wouldn't take me that long to get this to a point where it would be worth merging, including adding something like a rationale section for why I'm withdrawing it.

With respect to whether or not to merge this as a withdrawn APE, the main questions I have are:

  • Are these ideas worth keeping as part of the historical record?
  • Is there a reasonable chance that a future APE might want to cite the ideas here?

It's pretty common for a PEP to be written that proposes an idea and gets declined or withdrawn but merged, and another PEP to come along with improvements to the original idea.

There have already been a few PRs submitted that largely take the approach described here. My favorite is astropy/astropy#16312 by @nstarman, and there were also astropy/astropy#12971 and astropy/astropy#15815. (I also submitted astropy/astropy#15794, but that took an approach I used in PlasmaPy that I have since come to regret. 😅)

@taldcroft
Copy link
Member

@namurphy - regardless of whether this PR gets merged into the formal list of APE's, the work you've put into it will always remain in this PR. This is citable and won't be forgotten any time soon. Nevertheless I do appreciate the two questions you posed as being entirely reasonable.

I think this is really a question that should go up to the CoCo, namely whether an APE PR should be merged already withdrawn.

cc: @eteq @kbwestfall @nstarman @aaryapatil @ceb8

@nstarman
Copy link
Member
nstarman commented Jan 29, 2026

I think this PEP is worth merging, withdrawn or otherwise. Some of these considerations are being addressed outside of the APE mechanism as we decided that some sub-package type checking can happen without an APE, but if (🤞
when) we need a more package-wide solution this APE will be a valuable resource!
Personally, I would be very happy to see this APE submitted not withdrawn :).

@taldcroft
Copy link
Member

@nstarman - Sounds OK, assuming you are speaking for the CoCo with a focus on process rather than the technical merits of the APE. In that case I might suggest making the type be Informational to convey the intent here.

This will match the usage in the Python docs and be shorter.
This carefully considered and not completely pseudorandomly selected sequence of emojis accurately conveys my true thoughts and feelings on enabling static type checking in Astropy.
@namurphy
Copy link
Contributor Author

Personally, I would be very happy to see this APE submitted not withdrawn :).

Thank you for the encouragement! I started working on this again, and am reconsidering the decision to withdraw it.

One change is that I'm switching this to proposing to use ty instead of mypy. I've used mypy for a few years now and don't think it's the best tool at present, and I've been wanting to try out ty for PlasmaPy. The performance of ty is amazing, and has a helpful feature that ty check --add-ignore will add ty: ignore comments to all lines that have errors, which can make it much easier and quicker to enable for an existing code base. I've found the error messages in mypy to be quite confusing at times. However, ty is in beta, so it may be premature to add. We'll see what happens! 🌱

@neutrinoceros
Copy link

well it's already in much better shape than early alphas, where checking astropy would hit a runaway memory leak. I'm also much more enhusiast about ty than mypy, especially for a large code base. The performance is one thing (as you noted, it's quite a feat), but what I think takes the cake are much better gradual promises: ty is supposed to be much more resilient than mypy when checking code that contains little to no type info.

@taldcroft
Copy link
Member
taldcroft commented Feb 1, 2026

I started working on this again, and am reconsidering the decision to withdraw it.

@namurphy - without a prototype implementation or identified funding, this will may well earn you the distinction of writing the first APE to get rejected. Just saying... 😄.

A limitation of static type checking is that the analysis is performed
statically (i.e., without running the code being analyzed). Dynamically
generated objects (e.g., ``astropy.units.zettajansky``) therefore cannot
be directly analyzed with a static type checker.
Copy link
Member
@nstarman nstarman Feb 5, 2026

Choose a reason for hiding this comment

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

Suggesting (as units maintainer, not CoCo) to say that 'directly' is strictly true, but there are ways to ameliorate this, e.g. by generating type stubs. We have mechanisms for this now in astropy.units, so it would be good to distinguish between code that:

  1. is static so can easily be typed
  2. is dynamic but fixed at installation, so can also be typed, though this is more complex
  3. is dynamic at runtime, is much more challenging / impossible to statically type.

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.

Create an APE that describes the extent to which Astropy should be typed and the process for doing so

5 participants

0