Skip to content

Fix mypy type checks#144

Merged
svenrdz merged 10 commits intoESGF:mainfrom
abought:fix/mypy-types
Feb 19, 2026
Merged

Fix mypy type checks#144
svenrdz merged 10 commits intoESGF:mainfrom
abought:fix/mypy-types

Conversation

@abought
Copy link
Contributor

@abought abought commented Feb 19, 2026

Purpose

This project includes type annotations. However, pytest-mypy only inspects files specified in the pytest testpaths configuration (per pyproject.toml). (see discussion) This caused mypy to skip project code when run in CI (it was only linting test files).

If run directly (mypy esgpull/, pytest ., etc), ~30 type errors were returned.

This PR fixes mypy to run when intended, and produces clean error output.

Summary of changes

This PR has been broken down into conceptually related commits, and in cases where errors are ignored, commit messages link directly to underlying rationale.

AFAICT, the correct return type for search functions is DatasetRecord rather than Dataset, and this annotation does seem to resolve many of the warnings. Let me know if that is incorrect.

As this is my first PR to this repo, I'm happy to revise on request. I like to have a clean slate for tests before I start larger edits :)

abought and others added 9 commits February 18, 2026 18:50
 This occurred because mypy respects the testpaths/ setting of pytest.
This is viable insofar as TOMLDocument does inherit from dict internally, and covers the parts of interface that are used here. Ref: `https://github.com/python-poetry/tomlkit/issues/326`
ClassVar solution had an unexpected and breaking interaction with internals of dataclasses / custom sqlalchemy usage
This lead to various mypy errors because search function returns DatasetRecord, not Datasets

Once corrected, an old search path revealed additional errors due to unimplemented `asdict` on the corrected class
* Call init method on exact class, not potential subclass
* Don't reuse variable names because the old ones have inferred types
@svenrdz
Copy link
Collaborator

svenrdz commented Feb 19, 2026

Thanks for the thorough PR! You are correct for the DatasetRecord return type, I must have missed it when changing the way datasets are handled (Dataset used to be the name for the current DatasetRecord, but is now an sqlalchemy model).

I ran the tests locally and now that mypy runs on the full code, it complains about a few missing type stubs, could you add them to the PR to make it complete ? Running uv add --dev types-PyYAML types-aiofiles should be sufficient.

@abought
Copy link
Contributor Author

abought commented Feb 19, 2026

Thanks for the review!

Fun fact: those types are installed in the existing config under pyproject.toml tool.rye -> dev-dependencies. They are thus skipped if you're using uv. (like I was at first)

I notice that the codebase is in the process of moving over to uv.

I'll make the changes here for just these two things, but it might be worth reviewing that section of pyproject.toml to see if any other dependencies need to be carried over.

@svenrdz
Copy link
Collaborator

svenrdz commented Feb 19, 2026

Oh right, I ran pytest through uv. I'll get the pyproject.toml sorted out, thanks for pointing it out!

(I've passed a note about this to the person managing the broader rye -> uv switch)
@svenrdz svenrdz merged commit 60e985f into ESGF:main Feb 19, 2026
3 checks passed
@abought
Copy link
Contributor Author

abought commented Feb 19, 2026

Updated this PR with the dependencies needed to get mypy working in uv environments.

I'll consider your changes to be the final source of truth. I'm changing it here only so that my code will pass as-written, even if the switch to uv lands first.

@svenrdz
Copy link
Collaborator

svenrdz commented Feb 19, 2026

I merged yours before other changes, so I'll only bother myself with potential conflicts :)

@abought
Copy link
Contributor Author

abought commented Feb 19, 2026

I merged yours before other changes, so I'll only bother myself with potential conflicts :)

async def thank_you()

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.

2 participants