Skip to content

Convert typing to PEP604 - #5531

Merged
BradyAJohnston merged 8 commits into
MDAnalysis:developfrom
BradyAJohnston:pep604
Nov 8, 2026
Merged

BradyAJohnston merged 8 commits into
MDAnalysis:developfrom
BradyAJohnston:pep604

Conversation

@BradyAJohnston

@BradyAJohnston BradyAJohnston commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Our minimal supported Python version is now 3.11, so we can adopt PEP604 type syntax:

  • typing.Optional[X] -> X | None
  • typing.Union[A, B] -> A | B

There are lots of regions that are lacking types all together, but ahead of a broader effort to improve formatting and type hinting, this aligns current types with modern standards.

Changes made in this Pull Request:

  • Update type hints (value: int = None -> value: int | None = None)
  • Fix some incorrect type hinting (np.array -> np.ndarray)
  • Docstring cleanup (np.ndarray -> numpy.ndarray)
  • Minor whitespace cleanups

LLM / AI generated code disclosure

LLMs or other AI-powered tools (beyond simple IDE use cases) were used in this contribution: no

(this was quite therapeutic to do by hand, brushing off my regex)

PR Checklist

  • Issue raised/referenced?
  • Tests updated/added?
  • Documentation updated/added?
  • package/CHANGELOG file updated?
  • Is your name in package/AUTHORS? (If it is not, add it!)
  • I have read and understand the current AI Policy
  • LLM/AI disclosure was updated.

Developers Certificate of Origin

I certify that I can submit this code contribution as described in the Developer Certificate of Origin, under the MDAnalysis LICENSE.

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.93%. Comparing base (ae9ff4b) to head (97c9413).
⚠️ Report is 3 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5531   +/-   ##
========================================
  Coverage    93.92%   93.93%           
========================================
  Files          184      184           
  Lines        22661    22662    +1     
  Branches      3223     3223           
========================================
+ Hits         21284    21287    +3     
+ Misses         917      915    -2     
  Partials       460      460           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BradyAJohnston

Copy link
Copy Markdown
Member Author

An example of docstring fixes are the now properly linked vs the current docs which was incorrect (numpy.arrray is a function, not a type) and also didn't link.

@tylerjereddy tylerjereddy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I added a few comments from skimming over--I guess those are mostly things that can be done in follow-ups.

If we decide to get pretty serious about typing, for big decision we should ask Joren H. to weigh in--it is hard to do typing correctly..

Comment thread package/MDAnalysis/analysis/base.py Resolved
Comment thread package/MDAnalysis/analysis/base.py Resolved
Comment thread package/MDAnalysis/auxiliary/EDR.py Resolved
Comment thread package/MDAnalysis/auxiliary/base.py Resolved
Comment thread package/MDAnalysis/coordinates/base.py Outdated
int
Step size for iteration. Always a positive integer greater than 0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

some extra whitespace changes are coming in

Comment thread package/MDAnalysis/lib/distances.py Resolved
@tylerjereddy

Copy link
Copy Markdown
Member

Beyond that, the changes mostly look fine/straightforward, though I only scanned the first 70 % of the diff or so.

@BradyAJohnston

Copy link
Copy Markdown
Member Author

Currently the only thing I am unsure about is "OpenMP" / "openmp". The code is case-agnostic, because it all goes through .lower() anyway. Most of the docstrings specify "OpenMP" but the usage is a mix between both. Currently I have added both to the Literal[] options, but unifying around "openmp" as the typed / docstring version I think is the way to go, but maybe best left for 3.0? Happy to make that change earlier here.

@tylerjereddy tylerjereddy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I read through the full diff manually. I think this is a net improvement so I'll give it an approval. I suggest adding the PR number to the CHANGELOG entry and self-merging.

I do believe there's more we could still modernize, even in this diff, as my comment about forward referenced string typing suggests.

That said, I suspect if there's an appetite for going all in on static typing, it might be good to have some kind of plan and a decision on which static type checkers to add in CI (probably more of them, and also the the primer that checks knock-on effects of typing changes to the rest of the ecosystem). But that's for other issue(s)/PR(s).

Comment thread package/CHANGELOG Resolved
start: Optional[int]=None, stop: Optional[int]=None,
step: Optional[int]=None,
order: Optional[str]='fac') -> np.ndarray:
def timeseries(self, asel: 'AtomGroup | None'=None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm pretty sure that using string typing like this can be completely avoided these days, but maybe best for a follow-up...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think in this case it would be circular? Because we have to import AtomGroup inside of TYPE_CHECKING to avoid it being circular, so we can't use the actual class and have to use the string version. Unless there is a way around this I am unaware of.

if TYPE_CHECKING:
    from ..core.groups import AtomGroup

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Have opened a discussion in #5543

@BradyAJohnston

Copy link
Copy Markdown
Member Author

Thanks for the thorough review @tylerjereddy ! Appreciate it, and welcome any thoughts for improved typing going forward.

@tylerjereddy

Copy link
Copy Markdown
Member

Unless there is a way around this I am unaware of.

Moving the annotations to separate type stubs is one way.

The other is as the PEP on circular and forward references describes: https://peps.python.org/pep-0649/. You used to need to do from __future__ import annotations but that's the default on 3.14+ anyway. We could just run static type checking on 3.14+.

@BradyAJohnston
BradyAJohnston merged commit bd5aa91 into MDAnalysis:develop Nov 8, 2026
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants