Skip to content

Add support for pre-commit with mypy and Ruff and start adding type hints#173

Merged
valeriupredoi merged 52 commits intomainfrom
add_typehints
Jan 14, 2026
Merged

Add support for pre-commit with mypy and Ruff and start adding type hints#173
valeriupredoi merged 52 commits intomainfrom
add_typehints

Conversation

@valeriupredoi
Copy link
Collaborator

@valeriupredoi valeriupredoi commented Jan 7, 2026

Description

OK @bnlawrence @davidhassell I have added full infrastructure for pre-commit with ruff and mypy

To run pre-commit, check out this branch, make sure you have pre-commit installed (from conda-forge) then:

pre-commit install
pre-commit run -a

Contributes twoards #168
(closing upon reviewer's discretion, not via merge from here)
Closes #121

Before you get started

Checklist

  • This pull request has a descriptive title and labels
  • This pull request has a minimal description (most was discussed in the issue, but a two-liner description is still desirable)
  • Unit tests have been added (if codecov test fails)
  • Any changed dependencies have been added or removed correctly (if need be)
  • If you are working on the documentation, please ensure the current build passes
  • All tests pass

@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

❌ Patch coverage is 84.62929% with 85 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.83%. Comparing base (628f227) to head (732cb57).
⚠️ Report is 53 commits behind head on main.

Files with missing lines Patch % Lines
pyfive/dataobjects.py 88.12% 16 Missing and 3 partials ⚠️
pyfive/inspect.py 71.73% 9 Missing and 4 partials ⚠️
pyfive/misc_low_level.py 83.95% 7 Missing and 6 partials ⚠️
pyfive/h5d.py 81.13% 9 Missing and 1 partial ⚠️
pyfive/indexing.py 25.00% 9 Missing ⚠️
pyfive/btree.py 91.22% 5 Missing ⚠️
pyfive/h5t.py 68.75% 2 Missing and 3 partials ⚠️
pyfive/datatype_msg.py 94.11% 2 Missing and 1 partial ⚠️
pyfive/high_level.py 93.33% 2 Missing and 1 partial ⚠️
pyfive/utilities.py 0.00% 3 Missing ⚠️
... and 2 more

❌ Your patch status has failed because the patch coverage (84.62%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #173   +/-   ##
=======================================
  Coverage   76.83%   76.83%           
=======================================
  Files          15       15           
  Lines        2936     2936           
  Branches      467      467           
=======================================
  Hits         2256     2256           
  Misses        558      558           
  Partials      122      122           

☔ View full report in Codecov by Sentry.
📢 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.

@valeriupredoi valeriupredoi changed the title Add support for pre-commit with Ruff and start adding type hints Add support for pre-commit with mypy and Ruff and start adding type hints Jan 8, 2026
@valeriupredoi
Copy link
Collaborator Author

the failing test is due to netCDF4==1.7.4 and fails in main as well ie nothing to do with this branch - @kmuehlbauer our beloved netCDF4 strikes again 😁

@valeriupredoi valeriupredoi marked this pull request as ready for review January 13, 2026 17:03
@valeriupredoi
Copy link
Collaborator Author

@davidhassell when you have a minute, let's have this reviewed before it reaches absurd sizes 🍺

Copy link
Collaborator

@davidhassell davidhassell left a comment

Choose a reason for hiding this comment

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

Hi V - looks good. A few minor comments, with which you can do what you like :)

""" initalize. """
if mode != 'r':
raise NotImplementedError('pyfive only provides support for reading and treats all reads as binary')
def __init__(self, filename: str | typing.BinaryIO, mode="r") -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does mode need a type?

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 indeed, let me pop it in

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added in 4ae10b1


def _get_object_by_address(self, obj_addr):
""" Return the object pointed to by a given address. """
def _get_object_by_address(self, obj_addr: typing.BinaryIO):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing return type? Or is it OK to omit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch, let me add one

Copy link
Collaborator Author

@valeriupredoi valeriupredoi Jan 14, 2026

Choose a reason for hiding this comment

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

added in 5486e7f with an ignore statement too, since the func has inconsistent returns, but they are OK, and mypy shouldn't complain - common false negative

valeriupredoi and others added 5 commits January 14, 2026 12:30
Co-authored-by: David Hassell <davidhassell@users.noreply.github.com>
Co-authored-by: David Hassell <davidhassell@users.noreply.github.com>
Co-authored-by: David Hassell <davidhassell@users.noreply.github.com>
…unc but they should not be changed, this is a common mypy false negative
pre-commit install
pre-commit run -a
# this will be removed when fully Ruff-ed
- name: Lint with flake8
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@davidhassell I am going to remove linting with flake8 now that we have ruff in, and working, pls holler back if you reckon we should keep it 🍺

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fine by me.

@valeriupredoi
Copy link
Collaborator Author

@davidhassell @bnlawrence @kmuehlbauer I boiled down the issue with netCDF4=1.7.4 to an MRE:

import netCDF4 as nc
import numpy as np

# should work as per https://unidata.github.io/netcdf4-python/#netCDF4.stringtochar
months = np.array(["January", "February", "March", "April"], dtype="S8")
print(nc.stringtochar(months))
# but I get:
# Traceback (most recent call last):
#   File "/home/valeriu/netcdf4_MRE.py", line 5, in <module>
#     print(nc.stringtochar(months))
#           ~~~~~~~~~~~~~~~^^^^^^^^
#   File "src/netCDF4/_netCDF4.pyx", line 6824, in netCDF4._netCDF4.stringtochar
# AttributeError: 'numpy.bytes_' object has no attribute 'encode'. Did you mean: 'decode'?
# with
# Name                     Version          Build                    Channel
# netcdf4                    1.7.4            nompi_py314h4ae7121_101  conda-forge
# everything OK with netCDF4==1.7.3

stringtochar is FUBAR, am going to open an issue at netCDF4

@valeriupredoi
Copy link
Collaborator Author

got this one open Unidata/netcdf4-python#1464 and I am pinning to netCDF4 != 1.7.4 for the moment

@valeriupredoi valeriupredoi merged commit 653181b into main Jan 14, 2026
6 of 7 checks passed
@valeriupredoi valeriupredoi deleted the add_typehints branch January 14, 2026 14:01
@valeriupredoi
Copy link
Collaborator Author

great many thanks for the review @davidhassell - we're all set 🍻

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.

Support pre-commit with Ruff

2 participants