Skip to content

Use mypy to check type hints #489

@sgillies

Description

@sgillies

We're using type hints in a few places. If we don't check or test them they could be or become erroneous.

Here are the results of running mypy on our code today:

$ mypy planet
planet/reporting.py:17: error: Skipping analyzing "tqdm.asyncio": module is installed, but missing library stubs or py.typed marker
planet/reporting.py:94: error: "None" has no attribute "postfix"
planet/reporting.py:102: error: "None" has no attribute "set_description_str"
planet/reporting.py:104: error: "None" has no attribute "refresh"
planet/geojson.py:18: error: Skipping analyzing "geojson": module is installed, but missing library stubs or py.typed marker
planet/geojson.py:18: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
planet/models.py:23: error: Skipping analyzing "tqdm.asyncio": module is installed, but missing library stubs or py.typed marker
planet/order_request.py:73: error: Incompatible types in assignment (expression has type "int", target has type "Sequence[Collection[Any]]")
planet/order_request.py:76: error: Incompatible types in assignment (expression has type "Dict[Any, Any]", target has type "Sequence[Collection[Any]]")
planet/order_request.py:79: error: Incompatible types in assignment (expression has type "Dict[Any, Any]", target has type "Sequence[Collection[Any]]")
planet/order_request.py:83: error: Incompatible types in assignment (expression has type "Optional[str]", target has type "Sequence[Collection[Any]]")
planet/order_request.py:117: error: List item 1 has incompatible type "Optional[str]"; expected "str"
planet/auth.py:85: error: Argument 1 to "APIKeyAuth" has incompatible type "Optional[str]"; expected "str"
planet/auth.py:132: error: "Auth" has no attribute "to_dict"
planet/http.py:123: error: Argument "auth" to "AsyncClient" has incompatible type "planet.auth.Auth"; expected "Union[Tuple[Union[str, bytes], Union[str, bytes]], Callable[[Request], Request], httpx._auth.Auth, None]"
planet/clients/data.py:182: error: Incompatible types in assignment (expression has type "bool", target has type "Collection[Any]")
planet/clients/__init__.py:17: error: Type of __all__ must be "Sequence[str]", not "List[Type[object]]"
planet/__init__.py:20: error: Type of __all__ must be "Sequence[str]", not "List[object]"
Found 17 errors in 9 files (checked 21 source files)

I'm going to see about adding mypy to our nox configuration and look into some of those errors. Some of them look like easily fixable hinting bugs or nits. In planet/reporting.py it looks like mypy has found a potential bug that our tests don't trigger. Static analysis for the win?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions