Skip to content

Improve typing consistency in codebase#1311

Merged
SylviaWhittle merged 37 commits into
AFM-SPM:mainfrom
ubdbra001:i1299-improve-typing-consistency
Jun 17, 2026
Merged

Improve typing consistency in codebase#1311
SylviaWhittle merged 37 commits into
AFM-SPM:mainfrom
ubdbra001:i1299-improve-typing-consistency

Conversation

@ubdbra001

Copy link
Copy Markdown
Collaborator

Fix #1299

This PR addresses the inconsistent typing in the codebase.
Given the size of this I'm going to do it incrementally in this PR. The initial stage of this is picking out the "easy wins" e.g. cases where the type hints are missing or incorrect and are easy/obvious to update.

At this stage all the tests still pass.


Before submitting a Pull Request please check the following.

  • Existing tests pass.
  • Documentation has been updated and builds. Remember to update as required...
    • docs/configuration.md
    • docs/usage.md
    • docs/data_dictionary.md
    • docs/advanced.md and new pages it should link to.
  • Pre-commit checks pass.

@ubdbra001 ubdbra001 changed the title I1299 improve typing consistency Improve typing consistency in codebase Mar 5, 2026
@codecov

codecov Bot commented Mar 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.48936% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.11%. Comparing base (6c2ef50) to head (86492cf).
⚠️ Report is 110 commits behind head on main.

Files with missing lines Patch % Lines
topostats/processing.py 33.33% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1311      +/-   ##
==========================================
- Coverage   87.98%   84.11%   -3.88%     
==========================================
  Files          31       31              
  Lines        6027     6447     +420     
==========================================
+ Hits         5303     5423     +120     
- Misses        724     1024     +300     

☔ 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.

@ns-rse ns-rse left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good work tackling this behemoth of a task @ubdbra001 and apologies for it being so messy (typing only came onto my radar way after I'd started).

Some comments in-line from a quick scan, don't have time to investigate more deeply I'm afraid.

Comment thread topostats/tracing/ordered_tracing.py Outdated
Comment thread topostats/tracing/ordered_tracing.py Outdated
Comment thread topostats/processing.py
Comment thread topostats/processing.py Outdated
Comment thread topostats/processing.py
grain_stats_df.index.set_names(["grain_number", "class", "subgrain"], inplace=True)
else:
grain_stats_df = None
return topostats_object.filename, topostats_object, grain_stats_df

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not for this work but I thought I'd removed returning of dataframes as they are instead pulled out of the topostats_objects and collated into a dictionary before converting to pd.DataFrame.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This was the only one I've spotted so far, happy to open an issue to be addressed later

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note it as an issue, its at least then recorded and can be addressed if anyone has time/inclination.

@SylviaWhittle SylviaWhittle left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a thing I noticed skim-reading this.

Comment thread topostats/processing.py Outdated
Comment thread topostats/processing.py Outdated
Comment thread topostats/processing.py Outdated
Comment thread topostats/tracing/ordered_tracing.py Outdated
@@ -214,7 +214,7 @@ def compile_images(
@staticmethod
def remove_common_values(
ordered_array: npt.NDArray, common_value_check_array: npt.NDArray, retain: list = ()

@ubdbra001 ubdbra001 Mar 5, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

On this:

  1. retain is typed as list type but being assigned an empty tuple by default 🤔
  2. Setting the default value to an empty list may cause issues (see here).

I'll probably set it as None and add a check so that it get re-set to an empty list if the arg is not supplied.
Any objections?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a good solution and yeah, default args should not be data structures as you point out.

Comment thread topostats/classes.py
molecule_data : dict[int, Molecule], optional
Dictionary of ``Molecule`` objects indexed by molecule number.
tracing_stats : dict | None
tracing_stats : dict, optional

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I have been going around replacing | None with , optional in the docstrings. Probably doesn't have a real impact but I thought I'd make it all consistent.

Let me know if you'd prefer to retain | None

Comment thread topostats/classes.py
Dictionary, indexed by molecule where the value is the molecules statistics for the given molecule.
"""
if self.molecule_data is None:
raise ValueError("No molecule data found")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Let me know if you'd like me to change this to something else

@ns-rse ns-rse left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some responses in-line...

Comment thread topostats/processing.py Outdated
Comment thread topostats/processing.py
grain_stats_df.index.set_names(["grain_number", "class", "subgrain"], inplace=True)
else:
grain_stats_df = None
return topostats_object.filename, topostats_object, grain_stats_df

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note it as an issue, its at least then recorded and can be addressed if anyone has time/inclination.

@ns-rse

ns-rse commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator

Just a thought but it might be worth adding commits to .git-blame-ignore-revs so that the "blame" resides with the original author rather than yourself @ubdbra001.

@ubdbra001

Copy link
Copy Markdown
Collaborator Author

I think I've got all the low hanging typing fruit, anything else this will cause tests to fail and so I suspect will require a bit more discussion.

@ubdbra001 ubdbra001 force-pushed the i1299-improve-typing-consistency branch from 9328755 to f627bd9 Compare May 27, 2026 10:05
Comment thread topostats/measure/feret.py Outdated
Comment thread topostats/measure/feret.py Outdated
Comment thread topostats/scars.py Outdated
Comment thread topostats/scars.py Outdated
Comment thread topostats/tracing/ordered_tracing.py Outdated
@@ -214,7 +214,7 @@ def compile_images(
@staticmethod
def remove_common_values(
ordered_array: npt.NDArray, common_value_check_array: npt.NDArray, retain: list = ()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a good solution and yeah, default args should not be data structures as you point out.

@SylviaWhittle

SylviaWhittle commented May 28, 2026

Copy link
Copy Markdown
Collaborator

Looked at all changes and have provided answers to queries for docstrings.

This is good to go once the docstrings are updated and provided tests pass.

Thank you very much @ubdbra001.

@SylviaWhittle SylviaWhittle merged commit d07b4e5 into AFM-SPM:main Jun 17, 2026
3 of 7 checks passed
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.

Improve typing consistency in Topostats

3 participants