Skip to content

fix mypy unused-ignore flakiness on optional imports (serializer.py, document_base.py) #3481

Description

@margaretjgu

# type: ignore[assignment] on the except ImportError: x = None fallback in serializer.py (pyarrow) and document_base.py (annotationlib, UnionType) flips between needed/unused depending on mypy/pyarrow-stubs/Python version. Hit twice already: generates_9.5_code (6b0aa4e, 408d9c9) and main (#3475). #3475 suppressed it via a module-wide warn_unused_ignores = false override, which also hides any real unused-ignore added to those files later.

Fix: split type-checking and runtime import paths, mypy always resolves the if branch, the else is never type-checked (verified: no error with pyarrow installed, uninstalled, or a garbage assignment in the except).

if TYPE_CHECKING:
    import pyarrow as pa
else:
    try:
        import pyarrow as pa
        ...
        __all__.append("PyArrowSerializer")
    except ImportError:
        pa = None

Same shape for annotationlib and UnionType. Remove the two overrides from pyproject.toml.

Rejected: annotate pa: Optional[ModuleType] upfront, skip the split. Fails with no-redef, mypy treats the annotation and the import as conflicting.

Effort: ~30-60 min, 2 files, verify across the Python version matrix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions