Skip to content

Conversation

@Pennycook
Copy link
Contributor

Related issues

Proposed changes

  • Configure mypy to check for missing type hints in addition to type errors.
  • Add type hints to (almost) all functions, with one exception due to complexity.
  • Make minimal code changes to satisfy mypy.

I'm sorry that this is so big, but I realized early on that doing this file by file didn't really work (even though the commit history suggests it did). Adding type hints to one file would often cause mypy to go back and re-examine the code that called those functions, potentially introducing a bunch of new errors.

But I think this was worth it, and the code is now much improved. There were quite a few places where adding the type information actually uncovered some bugs (or at least, assumptions) such as assuming that a function returns a value even though it's technically capable of returning None. For the most part, I've satisfied mypy by declaring which functions can sometimes return None, and then adding checks that they don't.

In the long-term, I would like to revisit some of the problems that this change has highlighted, including:

  • Rewriting code to avoid | None, by preventing invalid look-ups entirely.
  • Rewriting functions to avoid complex nested type definitions (e.g., dicts of lists of tuples of lists...)
  • Rewriting file_source (see Rewrite file_source and file_parser #102) to avoid all the complexity related to the file-source Callables.
  • Move Platform into the Preprocessor so that we can use strong-typing without introducing a circular import.

--disallow-untyped-defs will warn if any function is missing type hints.

These checks are currently skipped for tests/* because fixing codebasin/*
should be the priority.

Signed-off-by: John Pennycook <[email protected]>
Also fixes a bug where two calls to abspath and join had somehow been flipped.

Signed-off-by: John Pennycook <[email protected]>
Adding the correct type hints here highlighted that there are two functions
with surprising behavior; I've added FIXME and TODO comments to ensure we do
not lose track of these.

Signed-off-by: John Pennycook <[email protected]>
Adding type hints has helped to make the behavior of the file_source classes
clearer, but also highlights how complicated they are.

Signed-off-by: John Pennycook <[email protected]>
Fixing the type hints here required a small modification to one of the tests,
because we were returning a list when we should have been returning a set.

Signed-off-by: John Pennycook <[email protected]>
Adding these type hints highlighted a few places where the code is quite
confusing, and I've added FIXME comments to remind us of those.

There was one block of code that relied on dynamic typing to such an extent
that I couldn't find a simple way to restructure it. I've added comments
there to disable mypy for now, but we should come back and rewrite that
function when we have a better idea of how it works.

Signed-off-by: John Pennycook <[email protected]>
@Pennycook Pennycook added this to the 2.x milestone Aug 5, 2025
@Pennycook Pennycook requested review from Copilot and swright87 August 5, 2025 13:56
@Pennycook Pennycook added documentation Improvements or additions to documentation ci/cd GitHub Actions, pre-commit hooks, etc labels Aug 5, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR configures mypy to check for missing type hints and adds comprehensive type annotations throughout the codebase to improve type safety and code quality.

  • Configures mypy with --disallow-untyped-defs to enforce type annotations
  • Adds type hints to almost all functions across the codebase
  • Makes minimal code changes to satisfy mypy requirements

Reviewed Changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
.pre-commit-config.yaml Enables stricter mypy checking and excludes tests from type checking
tests/files/test_filetree_node.py Updates test assertion to match change from list to set for platforms property
codebasin/tree.py Adds return type annotations for CLI functions
codebasin/report.py Comprehensive type hints for reporting functions and classes
codebasin/preprocessor.py Extensive type annotations for preprocessor tokens, nodes, and parsing classes
codebasin/platform.py Type hints for platform management functionality
codebasin/language.py Simple type annotations for file language detection
codebasin/finder.py Type hints for code analysis and parsing state management
codebasin/file_source.py Type annotations for file source processing classes
codebasin/file_parser.py Type hints for file parsing functionality
codebasin/config.py Type annotations for configuration management
codebasin/_detail/logging.py Type hints for logging utilities
codebasin/main.py Type annotations for main entry point functions
codebasin/init.py Type hints for core data structures

Signed-off-by: John Pennycook <[email protected]>
@Pennycook
Copy link
Contributor Author

I'm choosing to ignore the coverage failures here. A lot of the new lines are actually code that we expect to be unreachable, and designing a test for such cases seems impossible. Additionally, most (and possibly all) of the new lines are part of functionality that needs to be rewritten, and I'd rather focus on providing more robust tests (#96) to guide those efforts.

Copy link

@swright87 swright87 left a comment

Choose a reason for hiding this comment

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

I don't see any issues here.

@Pennycook Pennycook merged commit f8a1d06 into P3HPC:main Aug 6, 2025
3 of 4 checks passed
@Pennycook Pennycook deleted the ci/type-hints branch August 6, 2025 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci/cd GitHub Actions, pre-commit hooks, etc documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add missing type hints

2 participants