You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The objective is to resolve 51 static type checking errors flagged by `mypy` across the `geospatial_tools` codebase. These errors primarily consist of implicit `Optional` types, incorrect path handling (mixing `str` and `pathlib.Path`), invariant list types (e.g., `list[Path]` instead of `Sequence[Path]`), and missing type annotations for dictionaries/lists. Addressing these issues will enforce strict typing, improving the reliability and maintainability of the project.
4
+
5
+
# 🏗️ Architectural Approach
6
+
7
+
The solution will strictly adhere to modern Python typing standards as outlined in the project's Python skill instructions:
-**Path Handling:** Ensure consistent use of `pathlib.Path` for all file system operations. Where functions accept both `str` and `Path`, standardize types or ensure safe casting.
11
+
-**Covariant Sequences:** Replace `list[T]` with `Sequence[T]` (from `typing` or `collections.abc`) in function arguments where variance causes type checking failures.
12
+
-**Precise Annotations:** Add exact type annotations for class attributes and local variables that Mypy cannot infer.
13
+
14
+
This approach aligns with the principle of "Easier To Change" by clearly documenting interfaces through types without altering runtime behavior.
15
+
16
+
# 🧪 Verification & Failure Modes
17
+
18
+
-**Verification:** The primary verification method is running `make mypy`. The task is complete when `make mypy` exits with code 0 (no errors found). Additionally, `make test`, `make precommit` and `make pylint` must pass to ensure type changes did not introduce runtime regressions.
19
+
-**Failure Modes:**
20
+
- Overly broad type annotations (`Any`) masking true issues.
21
+
- Incorrectly handling `None` checks, leading to runtime `AttributeError`s.
22
+
- Breaking external scripts that depend on functions previously accepting less strict types. This will be mitigated by ensuring changes are backward-compatible (e.g., keeping union types where necessary).
23
+
24
+
# 📋 Implementation Steps
25
+
26
+
1.**Fix Implicit Optionals:** Update all function signatures across `raster.py`, `vector.py`, and `nimrod.py` to explicitly type parameters defaulting to `None` as `T | None`.
27
+
2.**Resolve Path vs String Mismatches:** Update variable types and division operators in `resample_tiff_raster.py`, `product_search.py`, `download_and_process.py`, and `planetary_computer/sentinel_2.py` to properly handle `pathlib.Path`.
28
+
3.**Fix Sequence and List Variance:** Update function arguments in `stac.py`, `utils.py`, and `planetary_computer/sentinel_2.py` to use `Sequence` instead of `list` where covariant types are expected.
29
+
4.**Add Missing Annotations and Fix Dictionary/List Initialization:** Add explicit types to class variables in `planetary_computer/sentinel_2.py` and local variables in `utils.py` and `raster.py`.
30
+
5.**Address Specific Edge Cases:** Fix the `logging_level` type assignment in `utils.py`, the `zip` argument in `raster.py`, the return statement in `planetary_computer/sentinel_2.py`, and the `sortby` parameter type in `stac.py`.
31
+
6.**Fix Missed Errors:** Address type and annotation issues in `download.py`, `vector.py` (lines 113, 141, 338), and `test_copernicus.py` (line 144) not covered by initial tasks.
32
+
33
+
# 🤝 Next Step
34
+
35
+
Do you approve Step 1 of the implementation plan to fix the implicit optionals?
1.**Modify `raster.py`:** Update `merge_raster_bands` parameters `merged_band_names` and `merged_metadata` to use `| None`.
19
+
2.**Modify `vector.py`:** Update `create_grid_from_bbox`, `create_grid_from_polygon`, and `save_grid_to_file` parameters (`crs`, `num_of_workers`) to use `| None`.
20
+
3.**Modify `nimrod.py`:** Update `extract_nimrod_from_archive` parameter `output_directory` to use `| None`.
21
+
22
+
## Requirements & Constraints
23
+
24
+
- Use modern Python union syntax (`T | None`) instead of `Optional[T]`.
25
+
- Ensure `None` checks are properly handled in the function bodies if they aren't already.
26
+
27
+
## Acceptance Criteria (AC)
28
+
29
+
-[x]`make mypy` no longer reports "implicit Optional" errors for these files.
30
+
-[x] Code remains functional and passes existing tests.
31
+
32
+
## Testing & Validation
33
+
34
+
- Run `make mypy` to verify fix.
35
+
- Run `make test` to ensure no regressions.
36
+
37
+
## Completion Protocol
38
+
39
+
-[x] Verify ACs.
40
+
-[x]`git add` changed files and `git commit -m "refactor(typing): fix implicit optionals in raster, vector, and nimrod"`
-**Error Pattern:**`Incompatible types in assignment`, `Unsupported left operand type for / ("str")`, `Argument X to "Y" has incompatible type "str"; expected "Path"`
1.**Modify `stac.py`:** Update function signatures (`merge_raster_bands`, etc.) to accept `Sequence[Path | str]` instead of `list`.
20
+
2.**Modify `utils.py`:** Update return type or internal typing of `unzip_file` / `extract_nimrod_from_archive` (or related) to use `Sequence`.
21
+
3.**Modify `planetary_computer/sentinel_2.py`:** Update `date_ranges` parameter to use `Sequence`.
22
+
4.**Modify `test_copernicus.py`:** Ensure test data types match expected sequences.
23
+
24
+
## Requirements & Constraints
25
+
26
+
- Import `Sequence` from `collections.abc` (Python 3.9+).
27
+
- Do not use `Sequence` for return types unless the function truly returns an immutable sequence; prefer `list` for returns if caller expects mutability, but handle the variance in the *argument* of the receiving function.
28
+
29
+
## Acceptance Criteria (AC)
30
+
31
+
-[x]`make mypy` no longer reports variance errors for these files.
32
+
-[x] Code functionality remains unchanged.
33
+
34
+
## Testing & Validation
35
+
36
+
- Run `make mypy`.
37
+
- Run `make test`.
38
+
39
+
## Completion Protocol
40
+
41
+
-[x] Verify ACs.
42
+
-[x]`git add` changed files and `git commit -m "refactor(typing): fix sequence and list variance issues"`
-**Error Patterns:**`Need type annotation for "params"`, `Missing return statement`, `Incompatible return value type`, `Unsupported right operand type for in`.
1.**Fix `utils.py`:** Add annotation for `params`, fix `logging_level` assignment, and handle `dataset_crs` type checks properly.
20
+
2.**Fix `raster.py`:** Fix `gdf` indexing (ensure it's a GeoDataFrame), fix `zip` argument type, and correct return type of `merge_raster_bands`.
21
+
3.**Fix `planetary_computer/sentinel_2.py`:** Add annotations for result attributes, add missing return statement in `sentinel_2_complete_tile_search`, and fix unpacking error in `future.result()`.
22
+
4.**Fix `stac.py`:** Handle `self.bands` being `None` in membership checks and fix `sortby` parameter type.
23
+
24
+
## Requirements & Constraints
25
+
26
+
- Avoid `Any` where possible; use specific types or `TypeVar` if needed.
27
+
- Ensure `future.result()` unpacking matches the actual return type of the submitted function.
28
+
29
+
## Acceptance Criteria (AC)
30
+
31
+
-[x]`make mypy` passes with 0 errors.
32
+
-[x] All logical fixes are verified by tests.
33
+
34
+
## Testing & Validation
35
+
36
+
- Run `make mypy`.
37
+
- Run `make test`.
38
+
- Run `make precommit`.
39
+
40
+
## Completion Protocol
41
+
42
+
-[x] Verify ACs.
43
+
-[x]`git add` changed files and `git commit -m "refactor(typing): fix missing annotations and specific edge cases"`
1.**Fix `download.py`:** Update type checking or argument for `unzip_file` (handle `Path | None`).
18
+
2.**Fix `vector.py`:** Add correct type annotations for `properties` dict, narrow type of `bounding_box` in `create_grid_from_bbox`, and fix return type for `save_grid_to_file`.
19
+
3.**Fix `test_copernicus.py`:** Update `bbox` argument to use a tuple `(float, float, float, float)`.
20
+
21
+
## Requirements & Constraints
22
+
23
+
- Follow modern typing conventions (`T | None`, `pathlib.Path`, etc.).
24
+
- Ensure no runtime regressions.
25
+
26
+
## Acceptance Criteria (AC)
27
+
28
+
-[x]`make mypy` passes with 0 errors.
29
+
30
+
## Testing & Validation
31
+
32
+
- Run `make mypy`.
33
+
- Run `make test`.
34
+
35
+
## Completion Protocol
36
+
37
+
-[x] Verify ACs.
38
+
-[x]`git add` changed files and `git commit -m "refactor(typing): fix missed mypy errors across various modules"`
0 commit comments