Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute code has type str but is used as type None #878

Merged
merged 7 commits into from
Dec 28, 2022

Conversation

luca-digrazia
Copy link
Contributor

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 ""

"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 ""
Copy link
Member

@gadomski gadomski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@property
def code(self) -> Optional[str]:
"""Get or sets the latitude band of the datasource."""
return self._get_property(CODE_PROP, str)

@codecov-commenter
Copy link

codecov-commenter commented Aug 18, 2022

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%) ⬇️

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

@luca-digrazia
Copy link
Contributor Author

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

@property
def code(self) -> Optional[str]:
"""Get or sets the latitude band of the datasource."""
return self._get_property(CODE_PROP, str)

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

@gadomski
Copy link
Member

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 gadomski requested a review from philvarner August 19, 2022 12:16
Copy link
Collaborator

@philvarner philvarner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment

@gadomski gadomski requested a review from philvarner August 30, 2022 18:41
@gadomski gadomski added the typing Issues or pull requests that relate to Python type checking label Nov 9, 2022
@gadomski gadomski merged commit 63179de into stac-utils:main Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing Issues or pull requests that relate to Python type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants