pystac icon indicating copy to clipboard operation
pystac copied to clipboard

Attribute `code` has type `str` but is used as type `None`

Open luca-digrazia opened this issue 2 years ago • 3 comments

Related Issue(s): # Type error found with Pyre

Description: "filename": "tests/extensions/test_grid.py" "warning_type": "Incompatible attribute type [8]" "warning_message": " Attribute code declared in class GridExtension has type str but is used as type None." "warning_line": 89 "fix": None to ""

luca-digrazia avatar Aug 18 '22 08:08 luca-digrazia

Codecov Report

Merging #878 (4bb5c53) into main (25cdf9d) will decrease coverage by 0.01%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #878      +/-   ##
==========================================
- Coverage   94.32%   94.30%   -0.02%     
==========================================
  Files          83       83              
  Lines       11963    11963              
  Branches     1403     1403              
==========================================
- Hits        11284    11282       -2     
- Misses        496      497       +1     
- Partials      183      184       +1     
Impacted Files Coverage Δ
tests/extensions/test_grid.py 100.00% <100.00%> (ø)
pystac/extensions/grid.py 95.65% <0.00%> (-4.35%) :arrow_down:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov-commenter avatar Aug 18 '22 12:08 codecov-commenter

This seems like a false positive -- the code property returns Optional[str]:

https://github.com/stac-utils/pystac/blob/69fcf3f2137045bd45ac5cf6cbd2fdad869eb169/pystac/extensions/grid.py#L68-L71

Thanks, for the answer, but I think it is not the method call in line 89.

luca-digrazia avatar Aug 19 '22 07:08 luca-digrazia

Thanks, I misunderstood what was going on. It looks like the line you're trying to fix is intentionally triggering a ValueError, so I think the correct fix might be to change the signature of the setter and the underlying helper function, e.g.:

diff --git a/pystac/extensions/grid.py b/pystac/extensions/grid.py
index 928897e..3b0bd6f 100644
--- a/pystac/extensions/grid.py
+++ b/pystac/extensions/grid.py
@@ -17,7 +17,7 @@ CODE_REGEX: str = r"[A-Z]+-[-_.A-Za-z0-9]+"
 CODE_PATTERN: Pattern[str] = re.compile(CODE_REGEX)
 
 
-def validated_code(v: str) -> str:
+def validated_code(v: Optional[str]) -> str:
     if not isinstance(v, str):
         raise ValueError("Invalid Grid code: must be str")
     if not CODE_PATTERN.fullmatch(v):
@@ -71,7 +71,7 @@ class GridExtension(
         return self._get_property(CODE_PROP, str)
 
     @code.setter
-    def code(self, v: str) -> None:
+    def code(self, v: Optional[str]) -> None:
         self._set_property(CODE_PROP, validated_code(v), pop_if_none=False)
 
     @classmethod

Requesting Phil's review since he added this in #799.

gadomski avatar Aug 19 '22 12:08 gadomski