(I wasn’t sure if this should go here or in the general Help category, so I don’t mind if the category mods think it should be moved over)
I’m switching some JSON-related code over to running mypy in strict mode and ran into trouble with the following snippet (_hash_file
is just a convenience wrapper around hashlib.file_digest
):
class ArchiveHashes(TypedDict):
sha256: str
# Only SHA256 hashes for now.
# Mark old hash fields with `typing.NotRequired`
# to migrate to a different hashing function in the future.
def _hash_archive(archive_path: Path) -> ArchiveHashes:
hashes:dict[str, str] = {}
for algorithm in ArchiveHashes.__required_keys__:
hashes[algorithm] = _hash_file(archive_path, algorithm)
# The required keys have been set, but mypy doesn't know that, and
# there's no `assert` that can be used to remedy the lack...
return hashes # type: ignore[return-value]
The comment in the code summarises my question: is there a way to avoid hitting mypy over the head with the # type: ignore
directive and instead satisfy it that the required keys in ArchiveHashes
have been set without having to hardcode them?
(the assert isinstance(hashes, ArchiveHashes)
approach I’d otherwise use to work around type inference failures doesn’t apply for TypedDict
)