Skip to content

Add type annotations and mypy CI to flatdata-py and flatdata-generator#262

Open
VeaaC wants to merge 12 commits intoheremaps:masterfrom
VeaaC:type_annotations
Open

Add type annotations and mypy CI to flatdata-py and flatdata-generator#262
VeaaC wants to merge 12 commits intoheremaps:masterfrom
VeaaC:type_annotations

Conversation

@VeaaC
Copy link
Copy Markdown
Collaborator

@VeaaC VeaaC commented Apr 24, 2026

Adds type annotations to all source functions/methods in both Python projects and
enforces them via mypy in CI.

What changed

Type annotations: All 415 functions across 62 source files are annotated.
This includes closure/nested functions (Jinja2 filters, data access readers,
validation helpers) and all properties.

mypy configuration (both pyproject.toml): 9 strictness flags enabled —
disallow_untyped_defs, check_untyped_defs, disallow_any_generics,
warn_return_any, warn_unused_configs, warn_redundant_casts,
warn_unused_ignores, no_implicit_optional, strict_equality. Namespace
package settings for the shared flatdata/ namespace.

CI: mypy steps added to generator.yml and py.yml, running after tests.

PEP 561: py.typed markers in flatdata/lib/ and flatdata/generator/.

Notable type improvements

  • Node.iterate(), children_like(), first_parent_like() made generic via
    @overload/TypeVar — eliminates ~20 potential # type: ignore comments
    throughout the tree and builder code.
  • Traversal namedtuples promoted to module-level typed NamedTuple classes
    (BfsAttr, DfsAttr).
  • .doc properties typed as str (were Any). referenced_structures typed
    as list[BuiltinStructureReference | StructureReference] (was list[Any]).
  • _raise correctly typed as NoReturn.
  • Dead code removed (enumeration.py:_bits_required).

Remaining type: ignore (22 total)

All have specific error codes and justification comments. The majority (15) are
attr-defined on Reference.node — the node accessor returns Node but callers
access subclass attributes. Fixing this properly requires a Reference type
hierarchy refactor. The rest are standard mypy limitations (property narrowing,
ABC isinstance, set.add idiom, __repr__ override).

Not done

  • strict = true evaluated, not adopted — adds ~100 errors from cross-package
    no-untyped-call and missing __all__ exports. Cost exceeds benefit.
  • ReadableBuffer Protocol for data/mem parameters — blocked by Python 3.10
    support (collections.abc.Buffer requires 3.12).
  • Storage/writer ABCs — structural refactor, out of scope.
  • Test files remain unannotated (by design).

@VeaaC VeaaC force-pushed the type_annotations branch 2 times, most recently from 68dedef to 76e1c61 Compare April 24, 2026 09:58
VeaaC and others added 12 commits April 24, 2026 12:00
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
…ping

When archive_name is a str, the method returns tuple[ModuleType, Any].
When archive_name is None (default), it returns just ModuleType.
Without overloads, mypy sees the union return type and rejects tuple
unpacking at call sites (attr-defined: Module has no __iter__).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
Signed-off-by: Christian Vetter <christian.vetter@here.com>
@VeaaC VeaaC force-pushed the type_annotations branch from 76e1c61 to 4268fd0 Compare April 24, 2026 10:00
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.

1 participant